dejanseo commited on
Commit
847ab7c
·
verified ·
1 Parent(s): 359c05b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -318,10 +318,16 @@ with gr.Blocks(title="LinkBERT by DEJAN AI") as demo:
318
  )
319
 
320
 
321
- # === Custom FastAPI with CORS ===
 
 
 
 
 
 
322
  app = FastAPI()
323
 
324
- # Allow all origins for now (you can restrict to dejan.ai later)
325
  app.add_middleware(
326
  CORSMiddleware,
327
  allow_origins=["*"],
@@ -330,15 +336,10 @@ app.add_middleware(
330
  allow_headers=["*"],
331
  )
332
 
333
-
334
- class ClassifyRequest(BaseModel):
335
- text: str
336
- threshold: float = 70.0
337
-
338
-
339
  @app.post("/api/classify")
340
  async def api_classify(request: ClassifyRequest):
341
- """Direct REST endpoint - bypasses Gradio entirely"""
342
  result = classify_text_json(request.text, request.threshold)
343
  return result
344
 
@@ -348,10 +349,18 @@ async def health():
348
  return {"status": "ok", "model": config.model_name}
349
 
350
 
351
- # Mount Gradio app
352
- app = gr.mount_gradio_app(app, demo, path="/")
 
 
 
 
 
 
 
 
353
 
354
- # Launch
355
  if __name__ == "__main__":
356
  import uvicorn
357
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
318
  )
319
 
320
 
321
+ # === Create FastAPI app with custom routes, then mount Gradio ===
322
+ class ClassifyRequest(BaseModel):
323
+ text: str
324
+ threshold: float = 70.0
325
+
326
+
327
+ # Create FastAPI app FIRST
328
  app = FastAPI()
329
 
330
+ # Add CORS middleware
331
  app.add_middleware(
332
  CORSMiddleware,
333
  allow_origins=["*"],
 
336
  allow_headers=["*"],
337
  )
338
 
339
+ # Add custom API routes BEFORE mounting Gradio
 
 
 
 
 
340
  @app.post("/api/classify")
341
  async def api_classify(request: ClassifyRequest):
342
+ """Direct REST endpoint"""
343
  result = classify_text_json(request.text, request.threshold)
344
  return result
345
 
 
349
  return {"status": "ok", "model": config.model_name}
350
 
351
 
352
+ @app.get("/")
353
+ async def root():
354
+ """Redirect root to Gradio UI"""
355
+ from fastapi.responses import RedirectResponse
356
+ return RedirectResponse(url="/gradio")
357
+
358
+
359
+ # Mount Gradio AFTER defining routes
360
+ app = gr.mount_gradio_app(app, demo, path="/gradio")
361
+
362
 
363
+ # Launch with uvicorn
364
  if __name__ == "__main__":
365
  import uvicorn
366
  uvicorn.run(app, host="0.0.0.0", port=7860)