qqwjq1981 commited on
Commit
4c49cd9
·
verified ·
1 Parent(s): 75e0ec6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -29
app.py CHANGED
@@ -1129,41 +1129,20 @@ def create_gradio_interface(state=None):
1129
 
1130
  # In[21]:
1131
 
 
1132
 
1133
- # Function to launch Gradio
1134
- def launch_gradio():
1135
- demo = create_gradio_interface()
1136
- demo.launch(share=True, inline=False) # Gradio in the foreground
1137
 
1138
- # Function to run FastAPI server using uvicorn in the background
1139
- async def run_fastapi():
1140
- config = uvicorn.Config(app, host="0.0.0.0", port=5000, reload=True, log_level="debug")
1141
- server = uvicorn.Server(config)
1142
- await server.serve()
1143
-
1144
- # FastAPI endpoint to display a message
1145
- @app.get("/", response_class=HTMLResponse)
1146
  async def index():
1147
- return "FastAPI is running. Visit Gradio at the provided public URL."
1148
 
1149
- # Main entry point for the asynchronous execution
1150
- async def main():
1151
- # Run Gradio in the foreground and FastAPI in the background
1152
- loop = asyncio.get_event_loop()
1153
-
1154
- # Run Gradio in a separate thread (non-blocking)
1155
- loop.run_in_executor(None, launch_gradio)
1156
-
1157
- # Run FastAPI in the background (asynchronous)
1158
- await run_fastapi()
1159
 
 
1160
  if __name__ == "__main__":
1161
- import nest_asyncio
1162
- nest_asyncio.apply() # Allow nested use of asyncio event loops in Jupyter notebooks
1163
-
1164
- # Run the main function to launch both services concurrently
1165
- asyncio.run(main())
1166
-
1167
 
1168
  # In[ ]:
1169
 
 
1129
 
1130
  # In[21]:
1131
 
1132
+ demo = create_gradio_interface()
1133
 
1134
+ # Mount the Gradio app at a specific path
1135
+ app.mount("/gradio", demo, name="gradio")
 
 
1136
 
1137
+ # Redirect from the root endpoint to the Gradio app
1138
+ @app.get("/", response_class=RedirectResponse)
 
 
 
 
 
 
1139
  async def index():
1140
+ return "/gradio"
1141
 
 
 
 
 
 
 
 
 
 
 
1142
 
1143
+ # Run the FastAPI server using uvicorn
1144
  if __name__ == "__main__":
1145
+ uvicorn.run(app, host="0.0.0.0", port=5000, reload=True, log_level="debug")
 
 
 
 
 
1146
 
1147
  # In[ ]:
1148