Spaces:
Running
Running
Change to Docker
Browse files
app.py
CHANGED
@@ -1,35 +1,37 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
-
from fastapi import FastAPI
|
4 |
-
from pydantic import BaseModel
|
5 |
-
from fastapi.middleware.wsgi import WSGIMiddleware
|
6 |
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
text: str
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
|
24 |
-
#
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
#
|
29 |
-
app = gr.mount_gradio_app(app, gr.Interface(fn=fake_interface, inputs=None, outputs="text"), path="/gradio")
|
30 |
-
|
31 |
-
# 启动应用,使用环境变量指定的端口
|
32 |
if __name__ == "__main__":
|
33 |
-
|
34 |
-
port = int(os.getenv("PORT", 7860)) # 获取 PORT 环境变量
|
35 |
-
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
+
# 定义处理函数
|
4 |
+
def api_aaa(text):
|
5 |
+
return text + 'aaa'
|
6 |
|
7 |
+
def api_bbb(text):
|
8 |
+
return text + 'bbb'
|
|
|
9 |
|
10 |
+
# 创建两个独立的接口,分别对应两个功能
|
11 |
+
iface_aaa = gr.Interface(
|
12 |
+
fn=api_aaa,
|
13 |
+
inputs="text",
|
14 |
+
outputs="text",
|
15 |
+
description="API endpoint for appending 'aaa' to text"
|
16 |
+
)
|
17 |
|
18 |
+
iface_bbb = gr.Interface(
|
19 |
+
fn=api_bbb,
|
20 |
+
inputs="text",
|
21 |
+
outputs="text",
|
22 |
+
description="API endpoint for appending 'bbb' to text"
|
23 |
+
)
|
24 |
|
25 |
+
# 组合成 Blocks 页面
|
26 |
+
with gr.Blocks() as demo:
|
27 |
+
gr.Markdown("# 模拟 API 接口")
|
28 |
+
|
29 |
+
with gr.Tab("API /api/aaa"):
|
30 |
+
iface_aaa.render()
|
31 |
+
|
32 |
+
with gr.Tab("API /api/bbb"):
|
33 |
+
iface_bbb.render()
|
34 |
|
35 |
+
# 启动 Gradio 应用并启用 API 访问
|
|
|
|
|
|
|
36 |
if __name__ == "__main__":
|
37 |
+
demo.launch(enable_api=True)
|
|
|
|