parkerjj commited on
Commit
17f3a9b
·
1 Parent(s): 71e2d55

Change to Docker

Browse files
Files changed (1) hide show
  1. app.py +24 -30
app.py CHANGED
@@ -1,37 +1,31 @@
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)
 
 
 
1
+ import os
2
+ from fastapi import FastAPI
3
+ from pydantic import BaseModel
4
+ from fastapi.middleware.wsgi import WSGIMiddleware
5
 
6
+ app = FastAPI() # 创建 FastAPI 应用
 
 
7
 
8
+ # 定义请求模型
9
+ class TextRequest(BaseModel):
10
+ text: str
11
 
12
+ # 定义两个 API 路由处理函数
13
+ @app.post("/api/aaa")
14
+ async def api_aaa(request: TextRequest):
15
+ result = request.text + 'aaa'
16
+ return {"result": result}
 
 
17
 
18
+ @app.post("/api/bbb")
19
+ async def api_bbb(request: TextRequest):
20
+ result = request.text + 'bbb'
21
+ return {"result": result}
 
 
22
 
23
+ # Gradio 假界面,仅用于通过 Hugging Face Spaces 部署
24
+ def fake_interface():
25
+ return "Gradio Interface Placeholder"
 
 
 
 
 
 
26
 
27
+ # 启动应用,使用环境变量指定的端口
28
  if __name__ == "__main__":
29
+ import uvicorn
30
+ port = int(os.getenv("PORT", 7860)) # 获取 PORT 环境变量
31
+ uvicorn.run(app, host="0.0.0.0", port=port)