muhammadpriv001 commited on
Commit
f17040f
·
1 Parent(s): 9d3bc6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -17
app.py CHANGED
@@ -10,6 +10,7 @@ import gradio as gr
10
  from fastapi import FastAPI, File, UploadFile, Form, HTTPException
11
  from fastapi.responses import JSONResponse, FileResponse
12
  from fastapi.middleware.cors import CORSMiddleware
 
13
 
14
  # ---------------------------------------------------------------------------
15
  # Monkey-patch gradio_client bug (Gradio 4.44.0):
@@ -29,19 +30,39 @@ def _safe_json_schema_to_python_type(schema, defs=None):
29
  _gc_utils._json_schema_to_python_type = _safe_json_schema_to_python_type
30
 
31
  # ---------------------------------------------------------------------------
32
- # Monkey-patch Jinja2 Environment.get_template to prevent
33
- # TypeError: unhashable type: 'dict' during template caching
34
  # ---------------------------------------------------------------------------
35
- import jinja2
36
-
37
- _orig_get_template = jinja2.Environment.get_template
38
-
39
- def _safe_get_template(self, name, globals=None):
40
- if isinstance(name, dict):
41
- name = str(name)
42
- return _orig_get_template(self, name, globals)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- jinja2.Environment.get_template = _safe_get_template
 
 
 
 
45
 
46
  from backend.config import (
47
  KNOWN_CONFIDENCE_THRESHOLD,
@@ -294,7 +315,8 @@ with gr.Blocks(css=custom_css, title="Open-World Object Intelligence Platform")
294
  confidence_slider,
295
  similarity_slider
296
  ],
297
- outputs=[output_img, json_output]
 
298
  )
299
 
300
  # -------------------------------------------------------------------
@@ -326,7 +348,8 @@ with gr.Blocks(css=custom_css, title="Open-World Object Intelligence Platform")
326
  btn_teach.click(
327
  fn=teach_new_object,
328
  inputs=[teach_name, teach_category, teach_desc, teach_files],
329
- outputs=[teach_status, objects_df]
 
330
  )
331
 
332
  btn_del.click(
@@ -513,11 +536,12 @@ def api_download_requirements_route():
513
 
514
 
515
  # ---------------------------------------------------------------------------
516
- # Mount Gradio app onto FastAPI app & launch with ZeroGPU support
517
  # ---------------------------------------------------------------------------
 
 
518
  app = gr.mount_gradio_app(app, demo, path="/")
519
 
520
  if __name__ == "__main__":
521
- demo.launch(server_name="0.0.0.0", server_port=7860)
522
- else:
523
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
10
  from fastapi import FastAPI, File, UploadFile, Form, HTTPException
11
  from fastapi.responses import JSONResponse, FileResponse
12
  from fastapi.middleware.cors import CORSMiddleware
13
+ import uvicorn
14
 
15
  # ---------------------------------------------------------------------------
16
  # Monkey-patch gradio_client bug (Gradio 4.44.0):
 
30
  _gc_utils._json_schema_to_python_type = _safe_json_schema_to_python_type
31
 
32
  # ---------------------------------------------------------------------------
33
+ # Starlette / Gradio TemplateResponse compatibility
 
34
  # ---------------------------------------------------------------------------
35
+ # Gradio 4.x uses the older TemplateResponse calling convention while newer
36
+ # Starlette releases expect the Request object first. When these versions
37
+ # are mixed, Starlette can pass the context dict to Jinja2 as the template
38
+ # name, producing: TypeError: unhashable type: 'dict'.
39
+ # Normalize the old Gradio call without changing Jinja2 itself.
40
+ try:
41
+ from starlette.templating import Jinja2Templates
42
+
43
+ _original_template_response = Jinja2Templates.TemplateResponse
44
+
45
+ def _compatible_template_response(self, *args, **kwargs):
46
+ if (
47
+ len(args) >= 2
48
+ and isinstance(args[0], dict)
49
+ and isinstance(args[1], str)
50
+ ):
51
+ # New Starlette interprets args[0] as Request. Gradio's older
52
+ # call style can provide context first and template name second.
53
+ context = args[0]
54
+ template_name = args[1]
55
+ remaining = args[2:]
56
+ return _original_template_response(
57
+ self, template_name, context, *remaining, **kwargs
58
+ )
59
+ return _original_template_response(self, *args, **kwargs)
60
 
61
+ Jinja2Templates.TemplateResponse = _compatible_template_response
62
+ except Exception:
63
+ # Do not prevent the application from starting if the installed
64
+ # Starlette version does not expose this class in the expected form.
65
+ pass
66
 
67
  from backend.config import (
68
  KNOWN_CONFIDENCE_THRESHOLD,
 
315
  confidence_slider,
316
  similarity_slider
317
  ],
318
+ outputs=[output_img, json_output],
319
+ api_name="detect"
320
  )
321
 
322
  # -------------------------------------------------------------------
 
348
  btn_teach.click(
349
  fn=teach_new_object,
350
  inputs=[teach_name, teach_category, teach_desc, teach_files],
351
+ outputs=[teach_status, objects_df],
352
+ api_name="teach_object"
353
  )
354
 
355
  btn_del.click(
 
536
 
537
 
538
  # ---------------------------------------------------------------------------
539
+ # Mount Gradio onto FastAPI and run ONE ASGI server
540
  # ---------------------------------------------------------------------------
541
+ # Do not call demo.launch() after mount_gradio_app(). That starts a second
542
+ # server and can cause routing/template failures on Hugging Face Spaces.
543
  app = gr.mount_gradio_app(app, demo, path="/")
544
 
545
  if __name__ == "__main__":
546
+ port = int(os.environ.get("PORT", os.environ.get("GRADIO_SERVER_PORT", "7860")))
547
+ uvicorn.run(app, host="0.0.0.0", port=port)