Justin-TW commited on
Commit
8c795ae
·
verified ·
1 Parent(s): e792791

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -0
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from fastapi import FastAPI
3
+ from fastapi.responses import JSONResponse
4
+
5
+ # 建立 FastAPI app
6
+ app = FastAPI()
7
+
8
+ # 定義一個簡單的推論函式
9
+ def predict(text: str):
10
+ return {"result": text[::-1]} # 這裡只是示範,把字串反轉
11
+
12
+ # 提供 API 端點
13
+ @app.post("/predict")
14
+ async def predict_api(payload: dict):
15
+ input_text = payload.get("text", "")
16
+ return JSONResponse(content=predict(input_text))
17
+
18
+ # 如果要同時提供 Gradio UI
19
+ with gr.Blocks() as demo:
20
+ inp = gr.Textbox(label="Input")
21
+ out = gr.Textbox(label="Output")
22
+ btn = gr.Button("Run")
23
+ btn.click(fn=lambda x: x[::-1], inputs=inp, outputs=out)
24
+
25
+ # 把 Gradio 挂到 FastAPI
26
+ app = gr.mount_gradio_app(app, demo, path="/")