Spaces:
Running
Running
Change to Gradio
Browse files- app.py +29 -10
- requirements.txt +3 -3
app.py
CHANGED
|
@@ -1,25 +1,44 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
#from preprocess import preprocess_text
|
|
|
|
| 4 |
|
| 5 |
-
# 初始化 FastAPI 应用
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
#
|
| 9 |
class TextRequest(BaseModel):
|
| 10 |
text: str
|
| 11 |
|
|
|
|
| 12 |
@app.post("/api/preprocess")
|
| 13 |
-
async def
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
"""
|
| 17 |
if not request.text.strip():
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
return result
|
| 21 |
|
| 22 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
if __name__ == "__main__":
|
| 24 |
import uvicorn
|
| 25 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
#from preprocess import preprocess_text # 假设您已经在 preprocess.py 中定义了此函数
|
| 4 |
+
import gradio as gr
|
| 5 |
|
|
|
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# 定义请求模型
|
| 9 |
class TextRequest(BaseModel):
|
| 10 |
text: str
|
| 11 |
|
| 12 |
+
# 定义 API 路由
|
| 13 |
@app.post("/api/preprocess")
|
| 14 |
+
async def preprocess_endpoint(request: TextRequest):
|
| 15 |
"""
|
| 16 |
+
接收文本并返回预处理后的结果
|
| 17 |
"""
|
| 18 |
if not request.text.strip():
|
| 19 |
+
return {"error": "Text cannot be empty."}
|
| 20 |
+
|
| 21 |
+
result = request.text + " (preprocessed)"
|
| 22 |
return result
|
| 23 |
|
| 24 |
+
# 使用 Gradio 创建 Web 界面
|
| 25 |
+
def gradio_interface(text):
|
| 26 |
+
return text + " (preprocessed)"
|
| 27 |
+
|
| 28 |
+
# Gradio Interface 配置
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=gradio_interface,
|
| 31 |
+
inputs="textbox", # 使用新版 Gradio 直接定义输入类型
|
| 32 |
+
outputs="json", # 使用新版 Gradio 直接定义输出类型
|
| 33 |
+
title="文本预处理 API",
|
| 34 |
+
description="发送文本到 /api/preprocess 进行预处理并获取 JSON 格式的响应"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# 将 Gradio 的 Web 界面挂载到 FastAPI 应用的 /gradio 路径下
|
| 38 |
+
app = gr.mount_gradio_app(app, iface, path="/gradio")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# 启动应用
|
| 42 |
if __name__ == "__main__":
|
| 43 |
import uvicorn
|
| 44 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
-
|
| 2 |
transformers
|
| 3 |
akshare
|
| 4 |
blis==0.7.11
|
| 5 |
spacy==3.7.5
|
| 6 |
gensim
|
| 7 |
numpy
|
|
|
|
| 8 |
fastapi
|
| 9 |
-
uvicorn
|
| 10 |
-
gensim
|
|
|
|
| 1 |
+
gradio
|
| 2 |
transformers
|
| 3 |
akshare
|
| 4 |
blis==0.7.11
|
| 5 |
spacy==3.7.5
|
| 6 |
gensim
|
| 7 |
numpy
|
| 8 |
+
gensim
|
| 9 |
fastapi
|
| 10 |
+
uvicorn
|
|
|