Spaces:
Running
Running
Change to Gradio
Browse files- app.py +28 -24
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,34 +1,38 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
def process_aaa(text):
|
5 |
return text + 'aaa'
|
6 |
|
7 |
def process_bbb(text):
|
8 |
return text + 'bbb'
|
9 |
|
10 |
-
#
|
11 |
-
iface_aaa = gr.Interface(
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
)
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
description="输入文本并返回文本 + 'bbb'"
|
25 |
-
)
|
26 |
-
|
27 |
-
# 启动 Gradio 应用,包含两个子接口
|
28 |
-
demo = gr.TabbedInterface(
|
29 |
-
[iface_aaa, iface_bbb],
|
30 |
-
["API /api/aaa", "API /api/bbb"]
|
31 |
-
)
|
32 |
|
|
|
33 |
if __name__ == "__main__":
|
34 |
-
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from fastapi import FastAPI
|
3 |
+
from pydantic import BaseModel
|
4 |
import gradio as gr
|
5 |
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
# 定义请求数据模型
|
9 |
+
class TextRequest(BaseModel):
|
10 |
+
text: str
|
11 |
+
|
12 |
+
# 定义 Gradio 处理函数
|
13 |
def process_aaa(text):
|
14 |
return text + 'aaa'
|
15 |
|
16 |
def process_bbb(text):
|
17 |
return text + 'bbb'
|
18 |
|
19 |
+
# 使用 Gradio 的接口函数,但不启动 Web 界面
|
20 |
+
iface_aaa = gr.Interface(fn=process_aaa, inputs="text", outputs="text")
|
21 |
+
iface_bbb = gr.Interface(fn=process_bbb, inputs="text", outputs="text")
|
22 |
+
|
23 |
+
# FastAPI 路由,用于接收和处理请求
|
24 |
+
@app.post("/api/aaa")
|
25 |
+
async def api_aaa(request: TextRequest):
|
26 |
+
result = iface_aaa(request.text)
|
27 |
+
return {"result": result}
|
28 |
+
|
29 |
+
@app.post("/api/bbb")
|
30 |
+
async def api_bbb(request: TextRequest):
|
31 |
+
result = iface_bbb(request.text)
|
32 |
+
return {"result": result}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
# 启动应用,使用环境变量指定的端口
|
35 |
if __name__ == "__main__":
|
36 |
+
import uvicorn
|
37 |
+
port = int(os.getenv("PORT", 7860)) # 获取 PORT 环境变量
|
38 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
requirements.txt
CHANGED
@@ -7,4 +7,4 @@ gensim
|
|
7 |
numpy
|
8 |
gensim
|
9 |
fastapi
|
10 |
-
uvicorn
|
|
|
7 |
numpy
|
8 |
gensim
|
9 |
fastapi
|
10 |
+
uvicorn
|