|
|
""" |
|
|
PyPilot Model Deployer - Production deployment and serving |
|
|
""" |
|
|
import torch |
|
|
from transformers import pipeline |
|
|
import flask |
|
|
from flask import Flask, request, jsonify |
|
|
import fastapi |
|
|
from fastapi import FastAPI, HTTPException |
|
|
import uvicorn |
|
|
import threading |
|
|
import time |
|
|
from datetime import datetime |
|
|
|
|
|
class PyPilotDeployer: |
|
|
def __init__(self, model_path=None): |
|
|
self.model_path = model_path |
|
|
self.model = None |
|
|
self.tokenizer = None |
|
|
self.is_loaded = False |
|
|
|
|
|
def load_model_for_inference(self, quantize=True): |
|
|
"""Load model optimized for inference""" |
|
|
print("π Loading model for inference...") |
|
|
|
|
|
if quantize: |
|
|
|
|
|
self.model = torch.quantization.quantize_dynamic( |
|
|
self.model, {torch.nn.Linear}, dtype=torch.qint8 |
|
|
) |
|
|
print("β
Model quantized for faster inference") |
|
|
|
|
|
self.model.eval() |
|
|
self.is_loaded = True |
|
|
print("β
Model ready for inference!") |
|
|
|
|
|
def create_flask_api(self, host='0.0.0.0', port=5000): |
|
|
"""Create Flask REST API for model serving""" |
|
|
app = Flask(__name__) |
|
|
|
|
|
@app.route('/health', methods=['GET']) |
|
|
def health_check(): |
|
|
return jsonify({'status': 'healthy', 'timestamp': datetime.now().isoformat()}) |
|
|
|
|
|
@app.route('/complete', methods=['POST']) |
|
|
def code_completion(): |
|
|
data = request.get_json() |
|
|
code_prompt = data.get('code', '') |
|
|
max_length = data.get('max_length', 100) |
|
|
|
|
|
if not self.is_loaded: |
|
|
return jsonify({'error': 'Model not loaded'}), 500 |
|
|
|
|
|
try: |
|
|
completion = self.generate_completion(code_prompt, max_length) |
|
|
return jsonify({ |
|
|
'completion': completion, |
|
|
'timestamp': datetime.now().isoformat() |
|
|
}) |
|
|
except Exception as e: |
|
|
return jsonify({'error': str(e)}), 500 |
|
|
|
|
|
@app.route('/analyze', methods=['POST']) |
|
|
def code_analysis(): |
|
|
data = request.get_json() |
|
|
code = data.get('code', '') |
|
|
|
|
|
from code_analyzer import PyPilotCodeAnalyzer |
|
|
analyzer = PyPilotCodeAnalyzer() |
|
|
analysis = analyzer.comprehensive_analysis(code) |
|
|
|
|
|
return jsonify(analysis) |
|
|
|
|
|
print(f"π Starting Flask API on {host}:{port}") |
|
|
return app, host, port |
|
|
|
|
|
def create_fastapi_service(self): |
|
|
"""Create FastAPI service for high-performance serving""" |
|
|
app = FastAPI(title="PyPilot API", version="1.0.0") |
|
|
|
|
|
@app.get("/") |
|
|
async def root(): |
|
|
return {"message": "PyPilot Code Assistant API"} |
|
|
|
|
|
@app.post("/v1/completions") |
|
|
async def create_completion(request: dict): |
|
|
code = request.get("code", "") |
|
|
max_tokens = request.get("max_tokens", 100) |
|
|
|
|
|
if not code: |
|
|
raise HTTPException(status_code=400, detail="Code prompt required") |
|
|
|
|
|
completion = self.generate_completion(code, max_tokens) |
|
|
|
|
|
return { |
|
|
"completion": completion, |
|
|
"model": "PyPilot", |
|
|
"created": datetime.now().isoformat() |
|
|
} |
|
|
|
|
|
@app.post("/v1/analysis") |
|
|
async def analyze_code(request: dict): |
|
|
code = request.get("code", "") |
|
|
|
|
|
from code_analyzer import PyPilotCodeAnalyzer |
|
|
analyzer = PyPilotCodeAnalyzer() |
|
|
analysis = analyzer.comprehensive_analysis(code) |
|
|
|
|
|
return analysis |
|
|
|
|
|
return app |
|
|
|
|
|
def generate_completion(self, prompt, max_length=100): |
|
|
"""Generate code completion""" |
|
|
|
|
|
|
|
|
mock_completions = [ |
|
|
f"# Generated completion for your code\nprint('Hello from PyPilot!')", |
|
|
f"# TODO: Implement this functionality\nreturn result", |
|
|
f"# PyPilot suggestion\nif __name__ == '__main__':\n main()" |
|
|
] |
|
|
|
|
|
import random |
|
|
return random.choice(mock_completions) |
|
|
|
|
|
def start_serving(self, api_type='flask', **kwargs): |
|
|
"""Start the model serving API""" |
|
|
if api_type == 'flask': |
|
|
app, host, port = self.create_flask_api(**kwargs) |
|
|
app.run(host=host, port=port, debug=False) |
|
|
elif api_type == 'fastapi': |
|
|
app = self.create_fastapi_service() |
|
|
uvicorn.run(app, host=kwargs.get('host', '0.0.0.0'), |
|
|
port=kwargs.get('port', 8000)) |
|
|
|
|
|
def create_gradio_interface(self): |
|
|
"""Create Gradio web interface for easy testing""" |
|
|
try: |
|
|
import gradio as gr |
|
|
|
|
|
def gradio_complete(code): |
|
|
return self.generate_completion(code) |
|
|
|
|
|
def gradio_analyze(code): |
|
|
from code_analyzer import PyPilotCodeAnalyzer |
|
|
analyzer = PyPilotCodeAnalyzer() |
|
|
return analyzer.comprehensive_analysis(code) |
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=gradio_complete, |
|
|
inputs=gr.Textbox(lines=10, placeholder="Enter your code here..."), |
|
|
outputs="text", |
|
|
title="PyPilot Code Assistant", |
|
|
description="AI-powered code completion and analysis" |
|
|
) |
|
|
|
|
|
return interface |
|
|
except ImportError: |
|
|
print("Gradio not installed. Install with: pip install gradio") |
|
|
return None |
|
|
|
|
|
if __name__ == "__main__": |
|
|
deployer = PyPilotDeployer() |
|
|
|
|
|
|
|
|
print("π Starting PyPilot deployment...") |
|
|
app, host, port = deployer.create_flask_api(port=5001) |
|
|
|
|
|
|
|
|
def run_flask(): |
|
|
app.run(host=host, port=port, debug=False, use_reloader=False) |
|
|
|
|
|
flask_thread = threading.Thread(target=run_flask) |
|
|
flask_thread.daemon = True |
|
|
flask_thread.start() |
|
|
|
|
|
print(f"β
PyPilot API running on http://{host}:{port}") |
|
|
print("π Endpoints:") |
|
|
print(" GET /health - Health check") |
|
|
print(" POST /complete - Code completion") |
|
|
print(" POST /analyze - Code analysis") |
|
|
|
|
|
|
|
|
try: |
|
|
while True: |
|
|
time.sleep(1) |
|
|
except KeyboardInterrupt: |
|
|
print("\nπ Shutting down PyPilot...") |