|
|
|
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 |
|
|
|
|
|
uploadPath = "./data/latents/" |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["http://localhost:5173"], |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
|
|
|
|
@app.get("/dataList/{datatype}") |
|
async def read_data(datatype): |
|
return service.getALlData() |
|
|
|
|
|
@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"} |
|
|
|
|
|
|
|
app.mount("/", StaticFiles(directory="static", html=True), name="static") |
|
|
|
|
|
|
|
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 交互界面" |
|
) |
|
|
|
|
|
app = gr.mount_gradio_app(app, gradio_ui, path="/gradio") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
port = int(os.getenv("PORT", 7860)) |
|
uvicorn.run(app, host="0.0.0.0", port=port) |