Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import pipeline
|
4 |
+
import os
|
5 |
+
|
6 |
+
# 正确加载模型(从缓存或下载)
|
7 |
+
classifier = pipeline(
|
8 |
+
"text-classification",
|
9 |
+
model="mrm8488/codebert-base-finetuned-detect-insecure-code"
|
10 |
+
)
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
class CodeRequest(BaseModel):
|
15 |
+
code: str # 输入参数定义
|
16 |
+
|
17 |
+
@app.post("/detect")
|
18 |
+
async def detect_insecure_code(request: CodeRequest):
|
19 |
+
try:
|
20 |
+
# 直接传递代码字符串到分类器
|
21 |
+
result = classifier(request.code)
|
22 |
+
return {"status": "success", "result": result[0]}
|
23 |
+
except Exception as e:
|
24 |
+
return {"status": "error", "message": str(e)}
|