File size: 741 Bytes
e6e9cd7
 
7de8bc5
08465c2
7de8bc5
 
 
08465c2
e6e9cd7
7de8bc5
 
 
 
 
 
 
 
 
 
08465c2
e6e9cd7
b025fa1
7de8bc5
 
 
 
 
 
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
import os
from fastapi import FastAPI
from transformers import pipeline

# 必须在所有导入前设置环境变量
os.environ["HF_HOME"] = "/app/.cache/huggingface"
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache/huggingface"

app = FastAPI()

# 模型加载(带错误处理)
try:
    analyzer = pipeline(
        "text2text-generation",
        model="Salesforce/codet5-small",
        tokenizer="Salesforce/codet5-small"
    )
except Exception as e:
    raise RuntimeError(f"模型加载失败: {str(e)}")

@app.post("/analyze")
async def analyze(code: str):
    result = analyzer(
        f"Analyze code vulnerabilities:\n{code}",
        max_length=512,
        num_beams=5
    )[0]['generated_text']
    return {"result": result}