[Go to site: main page, start]

0% found this document useful (0 votes)
8 views1 page

FastAPI File Upload and Analysis

This document outlines a FastAPI application that performs static analysis on code snippets in Python or JavaScript. It uses the Salesforce Codet5 model to explain bugs and suggest fixes based on the provided code. The application includes an endpoint that accepts code input, runs static analysis, and returns both the analysis report and AI-generated responses.

Uploaded by

dejilearn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

FastAPI File Upload and Analysis

This document outlines a FastAPI application that performs static analysis on code snippets in Python or JavaScript. It uses the Salesforce Codet5 model to explain bugs and suggest fixes based on the provided code. The application includes an endpoint that accepts code input, runs static analysis, and returns both the analysis report and AI-generated responses.

Uploaded by

dejilearn
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

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
}

You might also like