Views
No views yet
v1.1.0 | BERT | FastAPI | Zero-Shot| Label | 类别 |
|---|---|
| 0 | 研究目的 |
| 1 | 研究内容 |
| 2 | 核心技术 |
| 3 | 其他 |
1import torch
2from transformers import AutoModelForSequenceClassification, AutoTokenizer
3from fastapi import FastAPI, HTTPException
4from pydantic import BaseModel
5
6class ZeroShotClassifier:
7 def __init__(self, model_path):
8 """初始化分类器"""
9 self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10 self.model = AutoModelForSequenceClassification.from_pretrained(model_path)
11 self.tokenizer = AutoTokenizer.from_pretrained(model_path)
12 self.model.to(self.device)
13 self.model.eval()
14
15 # 标签映射
16 self.label_map = {
17 0: 0,
18 1: 1,
19 2: 2,
20 3: 3
21 }
22
23 def predict(self, text):
24 """对单个文本进行预测"""
25 # 对文本进行编码
26 inputs = self.tokenizer(
27 text,
28 padding=True,
29 truncation=True,
30 max_length=512,
31 return_tensors="pt"
32 ).to(self.device)
33
34 # 进行预测
35 with torch.no_grad():
36 outputs = self.model(**inputs)
37 predictions = torch.softmax(outputs.logits, dim=-1)
38 predicted_class = torch.argmax(predictions, dim=-1).item()
39
40 # 转换标签
41 predicted_label = self.label_map[predicted_class]
42 confidence = predictions[0][predicted_class].item()
43
44 return {
45 "label": predicted_label,
46 "confidence": confidence
47 }
48
49 def predict_batch(self, texts):
50 """对多个文本进行批量预测"""
51 results = []
52 for text in texts:
53 result = self.predict(text)
54 results.append(result)
55 return results
56
57# FastAPI部分
58app = FastAPI(
59 title="零样本文本分类服务",
60 version="1.0.0",
61 docs_url="/docs"
62)
63
64class PredictRequest(BaseModel):
65 text: str
66
67class PredictResponse(BaseModel):
68 label: int
69 confidence: float
70
71# 加载模型(只加载一次)
72classifier = ZeroShotClassifier("Wuhall/bert-base-chinese-cls")
73
74@app.post("/predict", response_model=PredictResponse, summary="文本分类预测")
75def predict_api(request: PredictRequest):
76 if not request.text:
77 raise HTTPException(status_code=400, detail="请提供text字段")
78 result = classifier.predict(request.text)
79 return result
80
81if __name__ == "__main__":
82 app.run(host="0.0.0.0", port=5000, debug=False)