Spaces:
Sleeping
Sleeping
File size: 1,276 Bytes
08465c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import AutoTokenizer, T5ForConditionalGeneration
import torch
app = FastAPI()
# ε
¨ε±ε 载樑ε
tokenizer = AutoTokenizer.from_pretrained("Salesforce/codet5-small")
model = T5ForConditionalGeneration.from_pretrained("Salesforce/codet5-small")
class CodeRequest(BaseModel):
code: str
max_length: int = 512
@app.post("/v1/analyze")
async def analyze_code(request: CodeRequest):
try:
# ζι ζη€Ίθ―
prompt = f"Analyze security vulnerabilities in this code:\n{request.code}"
# ηζεζη»ζ
inputs = tokenizer(prompt, return_tensors="pt",
max_length=512, truncation=True)
outputs = model.generate(
inputs.input_ids,
max_length=request.max_length,
num_beams=5,
early_stopping=True
)
# 解η η»ζ
analysis = tokenizer.decode(outputs[0], skip_special_tokens=True)
return {
"status": "success",
"analysis": analysis,
"model": "Salesforce/codet5-small"
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) |