FlameF0X commited on
Commit
72dc636
·
verified ·
1 Parent(s): 7350006

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -24,7 +24,6 @@ from typing import Any, Dict, Iterator, List, Optional, Tuple, Union
24
 
25
  import gradio as gr
26
  import torch
27
- import uvicorn
28
  from fastapi import Depends, FastAPI, Header, HTTPException, Request
29
  from fastapi.responses import JSONResponse, StreamingResponse
30
  from pydantic import BaseModel, ConfigDict
@@ -466,8 +465,23 @@ with gr.Blocks(title="Myosotis API") as demo:
466
  api_name=False, # keep Gradio's own API surface out of the way
467
  )
468
 
469
- # API routes were registered first, so they win over the Gradio mount at "/".
470
- app = gr.mount_gradio_app(api, demo, path="/")
 
 
 
 
 
 
 
471
 
472
  if __name__ == "__main__":
473
- uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", "7860")))
 
 
 
 
 
 
 
 
 
24
 
25
  import gradio as gr
26
  import torch
 
27
  from fastapi import Depends, FastAPI, Header, HTTPException, Request
28
  from fastapi.responses import JSONResponse, StreamingResponse
29
  from pydantic import BaseModel, ConfigDict
 
465
  api_name=False, # keep Gradio's own API surface out of the way
466
  )
467
 
468
+ def attach_api_to_gradio():
469
+ """Copy the /v1 routes onto Gradio's own FastAPI app (same server, same port)."""
470
+ gradio_app = demo.app
471
+ api_routes = [r for r in api.routes if getattr(r, "path", "").startswith(("/v1", "/health"))]
472
+ # Insert at the front so no Gradio catch-all route can shadow them.
473
+ for route in reversed(api_routes):
474
+ gradio_app.router.routes.insert(0, route)
475
+ gradio_app.add_exception_handler(HTTPException, openai_style_errors)
476
+
477
 
478
  if __name__ == "__main__":
479
+ # Let Gradio own the port (Spaces expects it), then add the OpenAI routes.
480
+ demo.launch(
481
+ server_name="0.0.0.0",
482
+ server_port=int(os.getenv("PORT", "7860")),
483
+ prevent_thread_lock=True,
484
+ ssr_mode=False,
485
+ )
486
+ attach_api_to_gradio()
487
+ demo.block_thread()