Spaces:
Running
Running
Change to Docker
Browse files
app.py
CHANGED
@@ -1,37 +1,31 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
2 |
|
3 |
-
#
|
4 |
-
def api_aaa(text):
|
5 |
-
return text + 'aaa'
|
6 |
|
7 |
-
|
8 |
-
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
description="API endpoint for appending 'aaa' to text"
|
16 |
-
)
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
description="API endpoint for appending 'bbb' to text"
|
23 |
-
)
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
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 |
-
#
|
36 |
if __name__ == "__main__":
|
37 |
-
|
|
|
|
|
|
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)
|