File size: 2,341 Bytes
4f68ac1
f77b412
3b357b2
 
4f68ac1
 
 
a6ec46c
4f68ac1
 
 
 
a6ec46c
4f68ac1
 
a6ec46c
4f68ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a20aa59
4f68ac1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# app.py (主入口文件)
import os 
os.system("pip uninstall -y gradio") 
os.system("pip install gradio==3.50.2") 
from fastapi import FastAPI, File, UploadFile
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
import gradio as gr
import uvicorn
import aiofiles
from typing import List
import service

# ---------- 原 controller.py 的核心逻辑 ----------
uploadPath = "./data/latents/"

# 初始化 FastAPP
app = FastAPI()

# CORS 配置(保持与 controller.py 一致)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:5173"],  # 前端开发地址
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


# ---------- 原 controller.py 的路由 ----------
@app.get("/dataList/{datatype}")
async def read_data(datatype):
    return service.getALlData() # 示例数据,替换为实际 service 调用

# 定义带路径参数的 GET 请求处理函数
@app.get("/details/{name}")
async def read_item(name: str = None):
    data = service.getListByName("integration_accuracy.csv", name)
    return data

@app.post("/upload")
async def upload_files(files: List[UploadFile] = File(...)):
    for file in files:
        save_path = os.path.join(uploadPath, file.filename)
        try:
            async with aiofiles.open(save_path, 'wb') as out_file:
                content = await file.read()
                await out_file.write(content)
        except Exception as e:
            return {"message": f"Error: {e}"}
        finally:
            await file.close()
    return {"success": True, "message": "Files uploaded"}


# 挂载 Vue3 前端静态文件(部署时需要)
app.mount("/", StaticFiles(directory="static", html=True), name="static")


# ---------- 集成 Gradio 接口 ----------
def gradio_predict(text):
    return f"Processed: {text}"

gradio_ui = gr.Interface(
    fn=gradio_predict,
    inputs=gr.Textbox(label="输入文本"),
    outputs=gr.Textbox(label="处理结果"),
    title="Gradio 交互界面"
)

# 将 Gradio 挂载到 /gradio 路径
app = gr.mount_gradio_app(app, gradio_ui, path="/gradio")

# ---------- 启动配置 ----------
if __name__ == "__main__":
    # Hugging Face Spaces 使用 PORT 环境变量
    port = int(os.getenv("PORT", 7860))
    uvicorn.run(app, host="0.0.0.0", port=port)