Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
app = FastAPI()
|
| 5 |
+
|
| 6 |
+
# 加载预训练的文本分类模型(建议替换为你微调后的 BERT 模型路径)
|
| 7 |
+
# 例如:'shibing624/bert4confusing-base-chinese' 或自定义模型
|
| 8 |
+
classifier = pipeline("text-classification", model="bert-base-chinese")
|
| 9 |
+
|
| 10 |
+
@app.get("/")
|
| 11 |
+
def home():
|
| 12 |
+
return {"message": "Text Moderation API is running."}
|
| 13 |
+
|
| 14 |
+
@app.post("/analyze")
|
| 15 |
+
def analyze_text(text: str):
|
| 16 |
+
# 执行推理
|
| 17 |
+
result = classifier(text)
|
| 18 |
+
return {"result": result}
|