parkerjj commited on
Commit
7e2a649
·
1 Parent(s): a803b83

Change to Docker

Browse files
Files changed (1) hide show
  1. app.py +29 -27
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
- app = FastAPI() # 创建 FastAPI 应用
 
 
8
 
9
- # 定义请求模型
10
- class TextRequest(BaseModel):
11
- text: str
12
 
13
- # 定义两个 API 路由处理函数
14
- @app.post("/api/aaa")
15
- async def api_aaa(request: TextRequest):
16
- result = request.text + 'aaa'
17
- return {"result": result}
 
 
18
 
19
- @app.post("/api/bbb")
20
- async def api_bbb(request: TextRequest):
21
- result = request.text + 'bbb'
22
- return {"result": result}
 
 
23
 
24
- # Gradio 假界面,仅用于通过 Hugging Face Spaces 部署
25
- def fake_interface():
26
- return "Gradio Interface Placeholder"
 
 
 
 
 
 
27
 
28
- # Gradio 应用挂载到 "/gradio" 路径
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
- import uvicorn
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)