Create handler.py
Browse files- handler.py +19 -0
handler.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict, Any
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
class EndpointHandler:
|
5 |
+
def __init__(self, path=""):
|
6 |
+
# 初始化对话生成管道
|
7 |
+
self.pipeline = pipeline("conversational", model=path)
|
8 |
+
|
9 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
10 |
+
"""
|
11 |
+
处理请求数据并返回对话生成结果
|
12 |
+
data args:
|
13 |
+
inputs (:obj: `str`)
|
14 |
+
Return:
|
15 |
+
A :obj:`dict`: 返回生成的对话结果
|
16 |
+
"""
|
17 |
+
inputs = data.get("inputs")
|
18 |
+
conversation = self.pipeline(inputs)
|
19 |
+
return {"generated_text": conversation}
|