zeisel commited on
Commit
4f68ac1
·
verified ·
1 Parent(s): 2d061ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -4
app.py CHANGED
@@ -1,7 +1,76 @@
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
2
 
3
- def greet(name):
4
- return f"Hello, {name}!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py (主入口文件)
2
+ import os
3
+ from fastapi import FastAPI, File, UploadFile
4
+ from fastapi.staticfiles import StaticFiles
5
+ from fastapi.middleware.cors import CORSMiddleware
6
  import gradio as gr
7
+ import uvicorn
8
+ import aiofiles
9
+ from typing import List
10
+ import service
11
 
12
+ # ---------- 原 controller.py 的核心逻辑 ----------
13
+ uploadPath = "./data/latents/"
14
 
15
+ # 初始化 FastAPP
16
+ app = FastAPI()
17
+
18
+ # CORS 配置(保持与 controller.py 一致)
19
+ app.add_middleware(
20
+ CORSMiddleware,
21
+ allow_origins=["http://localhost:5173"], # 前端开发地址
22
+ allow_credentials=True,
23
+ allow_methods=["*"],
24
+ allow_headers=["*"],
25
+ )
26
+
27
+
28
+ # ---------- 原 controller.py 的路由 ----------
29
+ @app.get("/dataList/{datatype}")
30
+ async def read_data(datatype):
31
+ return service.getALlData() # 示例数据,替换为实际 service 调用
32
+
33
+ # 定义带路径参数的 GET 请求处理函数
34
+ @app.get("/details/{name}")
35
+ async def read_item(name: str = None):
36
+ data = service.getListByName("integration_accuracy.csv", name)
37
+ return data
38
+
39
+ @app.post("/upload")
40
+ async def upload_files(files: List[UploadFile] = File(...)):
41
+ for file in files:
42
+ save_path = os.path.join(uploadPath, file.filename)
43
+ try:
44
+ async with aiofiles.open(save_path, 'wb') as out_file:
45
+ content = await file.read()
46
+ await out_file.write(content)
47
+ except Exception as e:
48
+ return {"message": f"Error: {e}"}
49
+ finally:
50
+ await file.close()
51
+ return {"success": True, "message": "Files uploaded"}
52
+
53
+
54
+ # 挂载 Vue3 前端静态文件(部署时需要)
55
+ app.mount("/", StaticFiles(directory="static", html=True), name="static")
56
+
57
+
58
+ # ---------- 集成 Gradio 接口 ----------
59
+ def gradio_predict(text):
60
+ return f"Processed: {text}"
61
+
62
+ gradio_ui = gr.Interface(
63
+ fn=gradio_predict,
64
+ inputs=gr.Textbox(label="输入文本"),
65
+ outputs=gr.Textbox(label="处理结果"),
66
+ title="Gradio 交互界面"
67
+ )
68
+
69
+ # 将 Gradio 挂载到 /gradio 路径
70
+ app = gr.mount_gradio_app(app, gradio_ui, path="/gradio")
71
+
72
+ # ---------- 启动配置 ----------
73
+ if __name__ == "__main__":
74
+ # Hugging Face Spaces 使用 PORT 环境变量
75
+ port = int(os.getenv("PORT", 8000))
76
+ uvicorn.run(app, host="0.0.0.0", port=port)