Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1160,71 +1160,72 @@ def create_gradio_interface(state=None):
|
|
| 1160 |
# demo.load(load_fn, inputs=None, outputs=[state]) # Initialize the state when the page is loaded
|
| 1161 |
|
| 1162 |
# Function to launch Gradio
|
| 1163 |
-
def launch_gradio():
|
| 1164 |
-
|
| 1165 |
-
|
| 1166 |
-
|
| 1167 |
-
# Function to run FastAPI server using uvicorn in the background
|
| 1168 |
-
async def run_fastapi():
|
| 1169 |
-
|
| 1170 |
-
|
| 1171 |
-
|
| 1172 |
-
|
| 1173 |
-
# FastAPI endpoint to display a message
|
| 1174 |
-
@app.get("/", response_class=HTMLResponse)
|
| 1175 |
-
async def index():
|
| 1176 |
-
|
| 1177 |
|
| 1178 |
-
# Main entry point for the asynchronous execution
|
| 1179 |
-
async def main():
|
| 1180 |
-
|
| 1181 |
-
|
| 1182 |
|
| 1183 |
-
|
| 1184 |
-
|
| 1185 |
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
|
| 1189 |
-
if __name__ == "__main__":
|
| 1190 |
-
|
| 1191 |
-
|
|
|
|
|
|
|
|
|
|
| 1192 |
|
| 1193 |
-
|
| 1194 |
-
|
| 1195 |
-
#
|
| 1196 |
-
|
| 1197 |
-
# # Use Gradio's `server_app` to get an ASGI app for Blocks
|
| 1198 |
-
# gradio_asgi_app = demo.launch(share=False, inbrowser=False, server_name="0.0.0.0", server_port=7860, inline=False)
|
| 1199 |
|
| 1200 |
-
|
| 1201 |
-
|
| 1202 |
|
| 1203 |
-
#
|
| 1204 |
-
|
| 1205 |
|
| 1206 |
-
#
|
| 1207 |
-
|
| 1208 |
-
|
| 1209 |
|
| 1210 |
-
#
|
| 1211 |
-
|
| 1212 |
|
| 1213 |
-
#
|
| 1214 |
-
#
|
| 1215 |
|
| 1216 |
-
#
|
| 1217 |
-
#
|
| 1218 |
-
#
|
| 1219 |
-
#
|
| 1220 |
-
#
|
| 1221 |
|
| 1222 |
-
#
|
| 1223 |
-
|
| 1224 |
-
|
| 1225 |
-
|
| 1226 |
|
| 1227 |
-
#
|
| 1228 |
-
|
| 1229 |
-
#
|
| 1230 |
-
|
|
|
|
| 1160 |
# demo.load(load_fn, inputs=None, outputs=[state]) # Initialize the state when the page is loaded
|
| 1161 |
|
| 1162 |
# Function to launch Gradio
|
| 1163 |
+
# def launch_gradio():
|
| 1164 |
+
# demo = create_gradio_interface()
|
| 1165 |
+
# demo.launch(share=True, inline=False) # Gradio in the foreground
|
| 1166 |
+
|
| 1167 |
+
# # Function to run FastAPI server using uvicorn in the background
|
| 1168 |
+
# async def run_fastapi():
|
| 1169 |
+
# config = uvicorn.Config(app, host="0.0.0.0", port=5000, reload=True, log_level="debug")
|
| 1170 |
+
# server = uvicorn.Server(config)
|
| 1171 |
+
# await server.serve()
|
| 1172 |
+
|
| 1173 |
+
# # FastAPI endpoint to display a message
|
| 1174 |
+
# @app.get("/", response_class=HTMLResponse)
|
| 1175 |
+
# async def index():
|
| 1176 |
+
# return "FastAPI is running. Visit Gradio at the provided public URL."
|
| 1177 |
|
| 1178 |
+
# # Main entry point for the asynchronous execution
|
| 1179 |
+
# async def main():
|
| 1180 |
+
# # Run Gradio in the foreground and FastAPI in the background
|
| 1181 |
+
# loop = asyncio.get_event_loop()
|
| 1182 |
|
| 1183 |
+
# # Run Gradio in a separate thread (non-blocking)
|
| 1184 |
+
# loop.run_in_executor(None, launch_gradio)
|
| 1185 |
|
| 1186 |
+
# # Run FastAPI in the background (asynchronous)
|
| 1187 |
+
# await run_fastapi()
|
| 1188 |
|
| 1189 |
+
# if __name__ == "__main__":
|
| 1190 |
+
# import nest_asyncio
|
| 1191 |
+
# nest_asyncio.apply() # Allow nested use of asyncio event loops in Jupyter notebooks
|
| 1192 |
+
|
| 1193 |
+
# # Run the main function to launch both services concurrently
|
| 1194 |
+
# asyncio.run(main())
|
| 1195 |
|
| 1196 |
+
# In[21]:
|
| 1197 |
+
demo = create_gradio_interface()
|
| 1198 |
+
# Use Gradio's `server_app` to get an ASGI app for Blocks
|
| 1199 |
+
gradio_asgi_app = demo.launch(share=False, inbrowser=False, server_name="0.0.0.0", server_port=7860, inline=False)
|
|
|
|
|
|
|
| 1200 |
|
| 1201 |
+
logging.debug(f"Gradio version: {gr.__version__}")
|
| 1202 |
+
logging.debug(f"FastAPI version: {fastapi.__version__}")
|
| 1203 |
|
| 1204 |
+
# Mount the Gradio ASGI app at "/gradio"
|
| 1205 |
+
app.mount("/gradio", gradio_asgi_app)
|
| 1206 |
|
| 1207 |
+
# create a static directory to store the static files
|
| 1208 |
+
static_dir = Path('./static')
|
| 1209 |
+
static_dir.mkdir(parents=True, exist_ok=True)
|
| 1210 |
|
| 1211 |
+
# mount FastAPI StaticFiles server
|
| 1212 |
+
app.mount("/static", StaticFiles(directory=static_dir), name="static")
|
| 1213 |
|
| 1214 |
+
# Dynamically check for the Gradio asset directory
|
| 1215 |
+
# gradio_assets_path = os.path.join(os.path.dirname(gr.__file__), "static")
|
| 1216 |
|
| 1217 |
+
# if os.path.exists(gradio_assets_path):
|
| 1218 |
+
# # If assets exist, mount them
|
| 1219 |
+
# app.mount("/assets", StaticFiles(directory=gradio_assets_path), name="assets")
|
| 1220 |
+
# else:
|
| 1221 |
+
# logging.error(f"Gradio assets directory not found at: {gradio_assets_path}")
|
| 1222 |
|
| 1223 |
+
# Redirect from the root endpoint to the Gradio app
|
| 1224 |
+
@app.get("/", response_class=RedirectResponse)
|
| 1225 |
+
async def index():
|
| 1226 |
+
return RedirectResponse(url="/gradio", status_code=307)
|
| 1227 |
|
| 1228 |
+
# Run the FastAPI server using uvicorn
|
| 1229 |
+
if __name__ == "__main__":
|
| 1230 |
+
# port = int(os.getenv("PORT", 5000)) # Default to 7860 if PORT is not set
|
| 1231 |
+
uvicorn.run(app, host="0.0.0.0", port=5000)
|