Benny-Tang commited on
Commit
a6ddd2e
·
verified ·
1 Parent(s): 3506d15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -1,26 +1,28 @@
1
  import os
2
  import uvicorn
3
  from fastapi import FastAPI
 
4
  import gradio as gr
5
 
6
- api = FastAPI()
 
 
7
 
8
- @api.get("/ping")
9
- async def ping():
10
- return {"message": "pong"}
11
 
12
- with gr.Blocks() as demo:
13
- gr.Markdown("## Gradio + FastAPI running in HF Spaces")
14
- msg = gr.Textbox()
15
- out = gr.Textbox()
16
- msg.change(lambda x: f"You typed: {x}", msg, out)
17
 
18
- # Mount gradio at root
19
- api = gr.mount_gradio_app(api, demo, path="/")
 
20
 
21
  if __name__ == "__main__":
22
- port = int(os.environ.get("PORT", 7860)) # 👈 use HF Spaces port
23
- uvicorn.run(api, host="0.0.0.0", port=port)
 
 
24
 
25
 
26
 
 
1
  import os
2
  import uvicorn
3
  from fastapi import FastAPI
4
+ from fastapi.middleware.wsgi import WSGIMiddleware
5
  import gradio as gr
6
 
7
+ # import your separate logic
8
+ from api import api_app # FastAPI app for API endpoints
9
+ from main import build_ui # function that returns a Gradio Blocks/Interface
10
 
11
+ # Create main FastAPI app
12
+ app = FastAPI()
 
13
 
14
+ # Mount API under /api
15
+ app.mount("/api", api_app)
 
 
 
16
 
17
+ # Build and mount Gradio UI under /
18
+ gradio_app = gr.routes.App(build_ui()) # build_ui() returns gr.Blocks/Interface
19
+ app.mount("/", gradio_app)
20
 
21
  if __name__ == "__main__":
22
+ # Use Hugging Face's assigned port, fallback to 7860 locally
23
+ port = int(os.environ.get("PORT", 7860))
24
+ uvicorn.run(app, host="0.0.0.0", port=port)
25
+
26
 
27
 
28