from fastapi import FastAPI, UploadFile, Form
from pydantic import BaseModel
from typing import Literal
import subprocess
import tempfile
import os
from transformers import pipeline
app = FastAPI()
# Load HuggingFace pipelines
bug_explainer = pipeline("text2text-generation", model="Salesforce/codet5-base")
class CodeInput(BaseModel):
code: str
language: Literal["python", "javascript"]
# Util: Save code to temp file and run static analysis
def run_static_analysis(code: str, language: str):
with [Link](delete=False, suffix='.py' if language ==
'python' else '.js') as tmp:
[Link]([Link]())
tmp_path = [Link]
try:
if language == "python":
result = [Link](["pylint", tmp_path], capture_output=True,
text=True, timeout=10)
else:
result = [Link](["eslint", tmp_path], capture_output=True,
text=True, timeout=10)
return [Link]
except Exception as e:
return f"Static analysis error: {str(e)}"
finally:
[Link](tmp_path)
# Endpoint
@[Link]("/analyze")
async def analyze_code(input: CodeInput):
# Static analysis
static_report = run_static_analysis([Link], [Link])
# Prompt for bug explanation and fix
prompt = f"Explain the bugs and suggest fixes in the following {[Link]}
code:\n\n{[Link]}"
response = bug_explainer(prompt, max_length=512, do_sample=True)[0]
['generated_text']
return {
"static_analysis": static_report,
"ai_response": response
}