mwauranjorogekelvin commited on
Commit
5fd91a4
Β·
verified Β·
1 Parent(s): 99a512a

Update app.py

Browse files

Gradio initialization update

Files changed (1) hide show
  1. app.py +21 -22
app.py CHANGED
@@ -56,27 +56,6 @@ def recognize_captcha(image, model_id):
56
  generated_ids = model.generate(pixel_values, max_new_tokens=32)
57
  return processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
58
 
59
- # ── FastAPI ───────────────────────────────────────────────────────────────────
60
- app = FastAPI(title="CAPTCHA Solver API")
61
-
62
- app.add_middleware(
63
- CORSMiddleware,
64
- allow_origins=["*"],
65
- allow_methods=["*"],
66
- allow_headers=["*"],
67
- )
68
-
69
- # ── Gradio UI ─────────────────────────────────────────────────────────────────
70
- iface = gr.Interface(
71
- fn=recognize_captcha,
72
- inputs=[gr.Image(), gr.Dropdown(MODELS, label="Model", value=DEFAULT_MODEL)],
73
- outputs=["text"],
74
- title="CAPTCHA Solver",
75
- description="API available at /solve-captcha-base64"
76
- )
77
-
78
- app = gr.mount_gradio_app(app, iface, path="/")
79
-
80
  # ── Schemas ───────────────────────────────────────────────────────────────────
81
  class SolveRequest(BaseModel):
82
  image_base64: str
@@ -89,7 +68,16 @@ class SolveResponse(BaseModel):
89
  model_used: str = ""
90
  error: str = ""
91
 
92
- # ── Endpoints ─────────────────────────────────────────────────────────────────
 
 
 
 
 
 
 
 
 
93
  @app.get("/health")
94
  def health():
95
  return {"status": "ok", "device": DEVICE, "quantized": False, "default_model": DEFAULT_MODEL}
@@ -113,6 +101,17 @@ def solve(req: SolveRequest, _: str = Depends(verify_key)):
113
  logger.error(f"Error: {e}")
114
  return SolveResponse(success=False, error=str(e), processing_time=time.time() - start)
115
 
 
 
 
 
 
 
 
 
 
 
 
116
  # ── Launch ────────────────────────────────────────────────────────────────────
117
  if __name__ == "__main__":
118
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
56
  generated_ids = model.generate(pixel_values, max_new_tokens=32)
57
  return processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  # ── Schemas ───────────────────────────────────────────────────────────────────
60
  class SolveRequest(BaseModel):
61
  image_base64: str
 
68
  model_used: str = ""
69
  error: str = ""
70
 
71
+ # ── FastAPI app + routes FIRST ────────────────────────────────────────────────
72
+ app = FastAPI(title="CAPTCHA Solver API")
73
+
74
+ app.add_middleware(
75
+ CORSMiddleware,
76
+ allow_origins=["*"],
77
+ allow_methods=["*"],
78
+ allow_headers=["*"],
79
+ )
80
+
81
  @app.get("/health")
82
  def health():
83
  return {"status": "ok", "device": DEVICE, "quantized": False, "default_model": DEFAULT_MODEL}
 
101
  logger.error(f"Error: {e}")
102
  return SolveResponse(success=False, error=str(e), processing_time=time.time() - start)
103
 
104
+ # ── Mount Gradio LAST (at /ui so it doesn't swallow all routes) ───────────────
105
+ iface = gr.Interface(
106
+ fn=recognize_captcha,
107
+ inputs=[gr.Image(), gr.Dropdown(MODELS, label="Model", value=DEFAULT_MODEL)],
108
+ outputs=["text"],
109
+ title="CAPTCHA Solver",
110
+ description="API available at /solve-captcha-base64"
111
+ )
112
+
113
+ app = gr.mount_gradio_app(app, iface, path="/ui")
114
+
115
  # ── Launch ────────────────────────────────────────────────────────────────────
116
  if __name__ == "__main__":
117
  uvicorn.run(app, host="0.0.0.0", port=7860)