Spaces:
Sleeping
Sleeping
manishw7 commited on
Commit ·
68beaa2
1
Parent(s): 6cd700e
Stability: Reverted to Gradio 3.50.2 with Premium CSS Styling
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: 3.50.2
|
| 20 |
python_version: "3.10"
|
| 21 |
app_file: app.py
|
| 22 |
pinned: true
|
app.py
CHANGED
|
@@ -10,19 +10,6 @@ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
|
| 10 |
from cnn_model import CharacterClassifier
|
| 11 |
from preprocessing import preprocess_for_ocr
|
| 12 |
|
| 13 |
-
# --- ROBUST GLOBAL PATCH FOR GRADIO 4.x ---
|
| 14 |
-
import gradio_client.utils
|
| 15 |
-
def robust_get_type(schema):
|
| 16 |
-
if isinstance(schema, bool): return "Any"
|
| 17 |
-
if not isinstance(schema, dict): return "Any"
|
| 18 |
-
if "const" in schema: return "Any"
|
| 19 |
-
return original_get_type(schema)
|
| 20 |
-
|
| 21 |
-
if hasattr(gradio_client.utils, "get_type"):
|
| 22 |
-
original_get_type = gradio_client.utils.get_type
|
| 23 |
-
gradio_client.utils.get_type = robust_get_type
|
| 24 |
-
# ------------------------------------------
|
| 25 |
-
|
| 26 |
# --- CONFIGURATION ---
|
| 27 |
BASE_MODEL_ID = "paudelanil/trocr-devanagari-2"
|
| 28 |
ADAPTER_ID = "manishw10/devgen-trocr-devanagari-lora"
|
|
@@ -30,7 +17,7 @@ CNN_MODEL_PATH = "devanagari-cnn-classifier.pt"
|
|
| 30 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 31 |
|
| 32 |
# --- ENGINE INITIALIZATION ---
|
| 33 |
-
print("System: Initializing
|
| 34 |
processor = TrOCRProcessor.from_pretrained(BASE_MODEL_ID)
|
| 35 |
base_model = VisionEncoderDecoderModel.from_pretrained(BASE_MODEL_ID)
|
| 36 |
base_model.config.decoder_start_token_id = processor.tokenizer.cls_token_id
|
|
@@ -91,8 +78,8 @@ def get_confidence_html(confidence):
|
|
| 91 |
return f"""<div style="display: flex; flex-direction: column; align-items: center; background: rgba(0,0,0,0.2); border-radius: 20px; padding: 15px;">
|
| 92 |
<svg width="100" height="100" viewBox="0 0 100 100">
|
| 93 |
<circle cx="50" cy="50" r="45" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="8" />
|
| 94 |
-
<circle cx="50" cy="50" r="45" fill="none" stroke="{color}" stroke-width="8" stroke-dasharray="282.7" stroke-dashoffset="{282.7 * (1 - confidence)}" stroke-linecap="round"
|
| 95 |
-
<text x="50" y="55" font-family="
|
| 96 |
</svg>
|
| 97 |
</div>"""
|
| 98 |
|
|
@@ -103,9 +90,9 @@ def predict(image, manual_mode):
|
|
| 103 |
pre_pil = preprocess_for_ocr(buf.getvalue())
|
| 104 |
if manual_mode == "Automatic":
|
| 105 |
mode, ar, bc = original_classify_input(pre_pil)
|
| 106 |
-
status = f"
|
| 107 |
else:
|
| 108 |
-
mode = manual_mode.lower(); status = f"
|
| 109 |
try:
|
| 110 |
if mode == "character" and cnn_engine.available:
|
| 111 |
res = cnn_engine.predict(pre_pil)
|
|
@@ -120,30 +107,30 @@ def predict(image, manual_mode):
|
|
| 120 |
except Exception as e:
|
| 121 |
return pre_pil, f"Error: {str(e)}", "Failed", "None", ""
|
| 122 |
|
| 123 |
-
# --- PREMIUM
|
| 124 |
CSS = """
|
| 125 |
-
|
| 126 |
-
.
|
| 127 |
-
.
|
| 128 |
-
|
| 129 |
"""
|
| 130 |
|
| 131 |
-
with gr.Blocks(css=CSS
|
| 132 |
with gr.Column(elem_classes="premium-card"):
|
| 133 |
gr.Markdown("# 🕉️ DevGen OCR")
|
| 134 |
with gr.Row():
|
| 135 |
with gr.Column(scale=1):
|
| 136 |
-
img_in = gr.Image(type="pil", label="Input
|
| 137 |
-
mode_ctrl = gr.Radio(["Automatic", "Word", "Character"], value="Automatic", label="
|
| 138 |
sub_btn = gr.Button("Recognize", variant="primary")
|
| 139 |
with gr.Column(scale=1):
|
| 140 |
conf_html = gr.HTML()
|
| 141 |
-
text_out = gr.Textbox(label="Result", elem_classes="result-box", interactive=False
|
| 142 |
-
status_md = gr.Markdown("
|
| 143 |
-
engine_txt = gr.Textbox(label="
|
| 144 |
with gr.Column():
|
| 145 |
gr.Markdown("### 🛠️ Visual Debug: What the Model Sees")
|
| 146 |
-
img_proc = gr.Image(type="pil", label="Preprocessed
|
| 147 |
|
| 148 |
sub_btn.click(predict, [img_in, mode_ctrl], [img_proc, text_out, status_md, engine_txt, conf_html])
|
| 149 |
|
|
|
|
| 10 |
from cnn_model import CharacterClassifier
|
| 11 |
from preprocessing import preprocess_for_ocr
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# --- CONFIGURATION ---
|
| 14 |
BASE_MODEL_ID = "paudelanil/trocr-devanagari-2"
|
| 15 |
ADAPTER_ID = "manishw10/devgen-trocr-devanagari-lora"
|
|
|
|
| 17 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
|
| 19 |
# --- ENGINE INITIALIZATION ---
|
| 20 |
+
print("System: Initializing Stable Premium Engine (3.50.2)...")
|
| 21 |
processor = TrOCRProcessor.from_pretrained(BASE_MODEL_ID)
|
| 22 |
base_model = VisionEncoderDecoderModel.from_pretrained(BASE_MODEL_ID)
|
| 23 |
base_model.config.decoder_start_token_id = processor.tokenizer.cls_token_id
|
|
|
|
| 78 |
return f"""<div style="display: flex; flex-direction: column; align-items: center; background: rgba(0,0,0,0.2); border-radius: 20px; padding: 15px;">
|
| 79 |
<svg width="100" height="100" viewBox="0 0 100 100">
|
| 80 |
<circle cx="50" cy="50" r="45" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="8" />
|
| 81 |
+
<circle cx="50" cy="50" r="45" fill="none" stroke="{color}" stroke-width="8" stroke-dasharray="282.7" stroke-dashoffset="{282.7 * (1 - confidence)}" stroke-linecap="round" />
|
| 82 |
+
<text x="50" y="55" font-family="Arial" font-size="20" font-weight="bold" fill="{color}" text-anchor="middle">{int(confidence * 100)}%</text>
|
| 83 |
</svg>
|
| 84 |
</div>"""
|
| 85 |
|
|
|
|
| 90 |
pre_pil = preprocess_for_ocr(buf.getvalue())
|
| 91 |
if manual_mode == "Automatic":
|
| 92 |
mode, ar, bc = original_classify_input(pre_pil)
|
| 93 |
+
status = f"System: {mode.upper()} (AR: {ar:.2f}, Blobs: {bc})"
|
| 94 |
else:
|
| 95 |
+
mode = manual_mode.lower(); status = f"Manual Mode: {mode.upper()}"
|
| 96 |
try:
|
| 97 |
if mode == "character" and cnn_engine.available:
|
| 98 |
res = cnn_engine.predict(pre_pil)
|
|
|
|
| 107 |
except Exception as e:
|
| 108 |
return pre_pil, f"Error: {str(e)}", "Failed", "None", ""
|
| 109 |
|
| 110 |
+
# --- PREMIUM CSS (Gradio 3.x Optimized) ---
|
| 111 |
CSS = """
|
| 112 |
+
.gradio-container { background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%) !important; color: white !important; }
|
| 113 |
+
.premium-card { background: rgba(30, 41, 59, 0.7) !important; border: 1px solid rgba(255,255,255,0.1); border-radius: 20px; padding: 20px; box-shadow: 0 10px 30px rgba(0,0,0,0.5); }
|
| 114 |
+
.result-box textarea { font-size: 2.5rem !important; font-weight: bold !important; color: #818cf8 !important; text-align: center !important; background: transparent !important; border: none !important; }
|
| 115 |
+
h1 { color: #818cf8 !important; font-size: 2.5rem !important; }
|
| 116 |
"""
|
| 117 |
|
| 118 |
+
with gr.Blocks(css=CSS) as demo:
|
| 119 |
with gr.Column(elem_classes="premium-card"):
|
| 120 |
gr.Markdown("# 🕉️ DevGen OCR")
|
| 121 |
with gr.Row():
|
| 122 |
with gr.Column(scale=1):
|
| 123 |
+
img_in = gr.Image(type="pil", label="Input")
|
| 124 |
+
mode_ctrl = gr.Radio(["Automatic", "Word", "Character"], value="Automatic", label="Mode")
|
| 125 |
sub_btn = gr.Button("Recognize", variant="primary")
|
| 126 |
with gr.Column(scale=1):
|
| 127 |
conf_html = gr.HTML()
|
| 128 |
+
text_out = gr.Textbox(label="Result", elem_classes="result-box", interactive=False)
|
| 129 |
+
status_md = gr.Markdown("Ready.")
|
| 130 |
+
engine_txt = gr.Textbox(label="Model", interactive=False)
|
| 131 |
with gr.Column():
|
| 132 |
gr.Markdown("### 🛠️ Visual Debug: What the Model Sees")
|
| 133 |
+
img_proc = gr.Image(type="pil", label="Preprocessed", interactive=False)
|
| 134 |
|
| 135 |
sub_btn.click(predict, [img_in, mode_ctrl], [img_proc, text_out, status_md, engine_txt, conf_html])
|
| 136 |
|