manishw7 commited on
Commit
39ef92c
·
1 Parent(s): a6259a8

Fix: Surgical patch to enable Recognize button

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -1,18 +1,24 @@
1
  import os
2
  import sys
3
 
4
- # --- ROBUST MONKEY-PATCH FOR GRADIO SCHEMA BUG ---
5
- # This bypasses the 'TypeError: argument of type bool is not iterable'
6
- # that occurs in Gradio 4.x/5.x internal API generation.
7
  try:
8
- import gradio.blocks
9
- def robust_get_api_info(self):
10
- return {"root": "/", "endpoints": {}}
11
- gradio.blocks.Blocks.get_api_info = robust_get_api_info
12
- print("System: Applied robust monkey-patch for Gradio API schema bug.")
 
 
 
 
 
 
13
  except Exception as e:
14
- print(f"System: Failed to apply patch (non-critical): {e}")
15
- # ------------------------------------------------
16
 
17
  import gradio as gr
18
  import torch
@@ -53,16 +59,16 @@ def predict(image):
53
  except Exception as e:
54
  return f"Error during inference: {str(e)}"
55
 
56
- # Modern Interface with the patch protection
57
  demo = gr.Interface(
58
  fn=predict,
59
- inputs=gr.Image(type="pil", label="Upload Devanagari Word"),
60
  outputs=gr.Textbox(label="Recognized Text"),
61
- title="DevGen OCR",
62
- description="Handwritten Devanagari word recognition.",
63
  allow_flagging="never"
64
  )
65
 
66
  if __name__ == "__main__":
67
- # Launch on standard HF port
68
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import os
2
  import sys
3
 
4
+ # --- SURGICAL MONKEY-PATCH FOR GRADIO ---
5
+ # This fixes the exact internal function that crashes on 'bool' types
6
+ # without breaking the actual API endpoints.
7
  try:
8
+ import gradio_client.utils
9
+ original_fn = gradio_client.utils.json_schema_to_python_type
10
+
11
+ def patched_json_schema_to_python_type(schema, *args, **kwargs):
12
+ # The bug happens when 'schema' is a boolean. We force it to be a dict or handle it.
13
+ if isinstance(schema, bool):
14
+ return "Any"
15
+ return original_fn(schema, *args, **kwargs)
16
+
17
+ gradio_client.utils.json_schema_to_python_type = patched_json_schema_to_python_type
18
+ print("System: Applied surgical monkey-patch for Gradio schema bug.")
19
  except Exception as e:
20
+ print(f"System: Failed to apply surgical patch: {e}")
21
+ # ----------------------------------------
22
 
23
  import gradio as gr
24
  import torch
 
59
  except Exception as e:
60
  return f"Error during inference: {str(e)}"
61
 
62
+ # Simple Interface - Now protected by the surgical patch
63
  demo = gr.Interface(
64
  fn=predict,
65
+ inputs=gr.Image(type="pil", label="Upload Handwritten Devanagari Word"),
66
  outputs=gr.Textbox(label="Recognized Text"),
67
+ title="DevGen Devanagari OCR",
68
+ description="Recognize handwritten Devanagari words using TrOCR and LoRA adaptation.",
69
  allow_flagging="never"
70
  )
71
 
72
  if __name__ == "__main__":
73
+ # Zero-config launch is best for HF Spaces
74
+ demo.launch()