Spaces:
Sleeping
Sleeping
manishw7 commited on
Commit ·
ced8950
1
Parent(s): e8bc8af
Fix: Event context and re-enable premium Gradio 4.x theme
Browse files
README.md
CHANGED
|
@@ -16,7 +16,7 @@ tags:
|
|
| 16 |
datasets:
|
| 17 |
- c3rl/IIIT-INDIC-HW-WORDS-Hindi
|
| 18 |
sdk: gradio
|
| 19 |
-
sdk_version:
|
| 20 |
python_version: "3.10"
|
| 21 |
app_file: app.py
|
| 22 |
pinned: true
|
|
|
|
| 16 |
datasets:
|
| 17 |
- c3rl/IIIT-INDIC-HW-WORDS-Hindi
|
| 18 |
sdk: gradio
|
| 19 |
+
sdk_version: "4.44.1"
|
| 20 |
python_version: "3.10"
|
| 21 |
app_file: app.py
|
| 22 |
pinned: true
|
app.py
CHANGED
|
@@ -11,6 +11,17 @@ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
|
| 11 |
from cnn_model import CharacterClassifier
|
| 12 |
from preprocessing import preprocess_for_ocr
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# --- CONFIGURATION ---
|
| 15 |
BASE_MODEL_ID = "paudelanil/trocr-devanagari-2"
|
| 16 |
ADAPTER_ID = "manishw10/devgen-trocr-devanagari-lora"
|
|
@@ -19,7 +30,7 @@ CNN_MODEL_PATH = "devanagari-cnn-classifier.pt"
|
|
| 19 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 20 |
|
| 21 |
# --- ENGINE CORE ---
|
| 22 |
-
print("System: Initializing Full Suite
|
| 23 |
processor = TrOCRProcessor.from_pretrained(BASE_MODEL_ID)
|
| 24 |
base_model = VisionEncoderDecoderModel.from_pretrained(BASE_MODEL_ID)
|
| 25 |
|
|
@@ -74,7 +85,6 @@ def original_classify_input(image):
|
|
| 74 |
y0, x0 = coords.min(axis=0); y1, x1 = coords.max(axis=0)
|
| 75 |
w, h = x1-x0+1, y1-y0+1
|
| 76 |
ar, bc = w/h, count_blobs(binary, min_size=max(binary.size * 0.001, 10))
|
| 77 |
-
|
| 78 |
is_char = True
|
| 79 |
if ar > 2.5: is_char = False
|
| 80 |
elif ar > 1.8 and bc >= 3: is_char = False
|
|
@@ -106,15 +116,13 @@ def get_confidence_html(confidence):
|
|
| 106 |
# --- PREDICT ---
|
| 107 |
def predict(image, manual_mode):
|
| 108 |
if image is None: return None, None, "Upload image.", "", ""
|
| 109 |
-
buf = io.BytesIO()
|
| 110 |
-
image.save(buf, format="PNG")
|
| 111 |
preprocessed_pil = preprocess_for_ocr(buf.getvalue())
|
| 112 |
if manual_mode == "Automatic":
|
| 113 |
mode, ar, bc = original_classify_input(preprocessed_pil)
|
| 114 |
status = f"**System Insight**: {mode.upper()} detected (AR: {ar:.2f}, Blobs: {bc})"
|
| 115 |
else:
|
| 116 |
mode = manual_mode.lower(); status = f"**Manual Mode**: {mode.upper()}"
|
| 117 |
-
|
| 118 |
try:
|
| 119 |
if mode == "character" and cnn_engine.available:
|
| 120 |
result = cnn_engine.predict(preprocessed_pil)
|
|
@@ -134,7 +142,7 @@ def predict(image, manual_mode):
|
|
| 134 |
CSS = """
|
| 135 |
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@400;600&family=Inter:wght@400;500&display=swap');
|
| 136 |
.gradio-container { background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%) !important; color: white !important; font-family: 'Inter', sans-serif !important; }
|
| 137 |
-
.premium-card { background: rgba(30, 41, 59, 0.7) !important; backdrop-filter: blur(12px); border: 1px solid rgba(255,255,255,0.1); border-radius: 24px; padding: 2rem; box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5); }
|
| 138 |
.result-box { font-size: 3rem !important; font-weight: 600; text-align: center; color: #818cf8; background: transparent !important; border: none !important; }
|
| 139 |
.btn-primary { background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%) !important; border: none !important; border-radius: 12px !important; font-family: 'Outfit', sans-serif !important; font-weight: 600 !important; }
|
| 140 |
.diagnostic-panel { margin-top: 30px; border-top: 1px solid rgba(255,255,255,0.1); padding-top: 20px; }
|
|
@@ -150,7 +158,7 @@ with gr.Blocks(css=CSS, theme=gr.themes.Default()) as demo:
|
|
| 150 |
sub_btn = gr.Button("Recognize", variant="primary", elem_classes="btn-primary")
|
| 151 |
with gr.Column(scale=1):
|
| 152 |
conf_html = gr.HTML()
|
| 153 |
-
text_out = gr.Textbox(label="
|
| 154 |
status_md = gr.Markdown("Engine ready.")
|
| 155 |
engine_txt = gr.Textbox(label="Active Model", interactive=False)
|
| 156 |
|
|
@@ -158,9 +166,8 @@ with gr.Blocks(css=CSS, theme=gr.themes.Default()) as demo:
|
|
| 158 |
gr.Markdown("### 🛠️ Visual Debug: What the Model Sees")
|
| 159 |
img_proc = gr.Image(type="pil", label="Preprocessed Input", interactive=False, show_label=False)
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
sub_btn.click(predict, [img_in, mode_ctrl], [img_proc, text_out, status_md, engine_txt, conf_html])
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|
| 166 |
demo.launch()
|
|
|
|
| 11 |
from cnn_model import CharacterClassifier
|
| 12 |
from preprocessing import preprocess_for_ocr
|
| 13 |
|
| 14 |
+
# --- SURGICAL MONKEY-PATCH FOR GRADIO 4.x ---
|
| 15 |
+
try:
|
| 16 |
+
import gradio_client.utils
|
| 17 |
+
original_fn = gradio_client.utils.json_schema_to_python_type
|
| 18 |
+
def patched_fn(schema, *args, **kwargs):
|
| 19 |
+
if isinstance(schema, bool): return "Any"
|
| 20 |
+
return original_fn(schema, *args, **kwargs)
|
| 21 |
+
gradio_client.utils.json_schema_to_python_type = patched_fn
|
| 22 |
+
except Exception: pass
|
| 23 |
+
# --------------------------------------------
|
| 24 |
+
|
| 25 |
# --- CONFIGURATION ---
|
| 26 |
BASE_MODEL_ID = "paudelanil/trocr-devanagari-2"
|
| 27 |
ADAPTER_ID = "manishw10/devgen-trocr-devanagari-lora"
|
|
|
|
| 30 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 31 |
|
| 32 |
# --- ENGINE CORE ---
|
| 33 |
+
print("System: Initializing Full Suite (Gradio 4.x Patched)...")
|
| 34 |
processor = TrOCRProcessor.from_pretrained(BASE_MODEL_ID)
|
| 35 |
base_model = VisionEncoderDecoderModel.from_pretrained(BASE_MODEL_ID)
|
| 36 |
|
|
|
|
| 85 |
y0, x0 = coords.min(axis=0); y1, x1 = coords.max(axis=0)
|
| 86 |
w, h = x1-x0+1, y1-y0+1
|
| 87 |
ar, bc = w/h, count_blobs(binary, min_size=max(binary.size * 0.001, 10))
|
|
|
|
| 88 |
is_char = True
|
| 89 |
if ar > 2.5: is_char = False
|
| 90 |
elif ar > 1.8 and bc >= 3: is_char = False
|
|
|
|
| 116 |
# --- PREDICT ---
|
| 117 |
def predict(image, manual_mode):
|
| 118 |
if image is None: return None, None, "Upload image.", "", ""
|
| 119 |
+
buf = io.BytesIO(); image.save(buf, format="PNG")
|
|
|
|
| 120 |
preprocessed_pil = preprocess_for_ocr(buf.getvalue())
|
| 121 |
if manual_mode == "Automatic":
|
| 122 |
mode, ar, bc = original_classify_input(preprocessed_pil)
|
| 123 |
status = f"**System Insight**: {mode.upper()} detected (AR: {ar:.2f}, Blobs: {bc})"
|
| 124 |
else:
|
| 125 |
mode = manual_mode.lower(); status = f"**Manual Mode**: {mode.upper()}"
|
|
|
|
| 126 |
try:
|
| 127 |
if mode == "character" and cnn_engine.available:
|
| 128 |
result = cnn_engine.predict(preprocessed_pil)
|
|
|
|
| 142 |
CSS = """
|
| 143 |
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@400;600&family=Inter:wght@400;500&display=swap');
|
| 144 |
.gradio-container { background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%) !important; color: white !important; font-family: 'Inter', sans-serif !important; }
|
| 145 |
+
.premium-card { background: rgba(30, 41, 59, 0.7) !important; backdrop-filter: blur(12px); border: 1px solid rgba(255,255,255,0.1); border-radius: 24px; padding: 2rem; box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5); }
|
| 146 |
.result-box { font-size: 3rem !important; font-weight: 600; text-align: center; color: #818cf8; background: transparent !important; border: none !important; }
|
| 147 |
.btn-primary { background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%) !important; border: none !important; border-radius: 12px !important; font-family: 'Outfit', sans-serif !important; font-weight: 600 !important; }
|
| 148 |
.diagnostic-panel { margin-top: 30px; border-top: 1px solid rgba(255,255,255,0.1); padding-top: 20px; }
|
|
|
|
| 158 |
sub_btn = gr.Button("Recognize", variant="primary", elem_classes="btn-primary")
|
| 159 |
with gr.Column(scale=1):
|
| 160 |
conf_html = gr.HTML()
|
| 161 |
+
text_out = gr.Textbox(label="Result", elem_classes="result-box", interactive=False, show_label=False)
|
| 162 |
status_md = gr.Markdown("Engine ready.")
|
| 163 |
engine_txt = gr.Textbox(label="Active Model", interactive=False)
|
| 164 |
|
|
|
|
| 166 |
gr.Markdown("### 🛠️ Visual Debug: What the Model Sees")
|
| 167 |
img_proc = gr.Image(type="pil", label="Preprocessed Input", interactive=False, show_label=False)
|
| 168 |
|
| 169 |
+
# EVENT HANDLER (Now correctly inside the Blocks context)
|
| 170 |
+
sub_btn.click(predict, [img_in, mode_ctrl], [img_proc, text_out, status_md, engine_txt, conf_html])
|
|
|
|
| 171 |
|
| 172 |
if __name__ == "__main__":
|
| 173 |
demo.launch()
|