Spaces:
Running
Running
Change to Gradio
Browse files
app.py
CHANGED
@@ -1,44 +1,21 @@
|
|
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 |
-
|
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 |
-
|
|
|
|
|
27 |
|
28 |
-
# Gradio
|
29 |
iface = gr.Interface(
|
30 |
fn=gradio_interface,
|
31 |
-
inputs="
|
32 |
-
outputs="json",
|
33 |
title="文本预处理 API",
|
34 |
-
description="发送文本到
|
35 |
)
|
36 |
|
37 |
-
#
|
38 |
-
app = gr.mount_gradio_app(app, iface, path="/gradio")
|
39 |
-
|
40 |
-
|
41 |
-
# 启动应用
|
42 |
if __name__ == "__main__":
|
43 |
-
|
44 |
-
uvicorn.run(app, host="0.0.0.0", port=63468)
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from pydantic import BaseModel
|
3 |
|
4 |
+
# 定义 Gradio 接口
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
def gradio_interface(text):
|
6 |
+
# 在这里实现实际的预处理逻辑
|
7 |
+
result = text + " (preprocessed)"
|
8 |
+
return {"result": result}
|
9 |
|
10 |
+
# 配置 Gradio 接口
|
11 |
iface = gr.Interface(
|
12 |
fn=gradio_interface,
|
13 |
+
inputs="text",
|
14 |
+
outputs="json",
|
15 |
title="文本预处理 API",
|
16 |
+
description="发送文本到 Gradio 接口进行预处理并获取 JSON 格式的响应"
|
17 |
)
|
18 |
|
19 |
+
# 运行 Gradio 应用
|
|
|
|
|
|
|
|
|
20 |
if __name__ == "__main__":
|
21 |
+
iface.launch()
|
|