Emeritus-21 commited on
Commit
5da8400
·
verified ·
1 Parent(s): 702e9b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -53
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py — HTR Space (Full Clean Version with Progress Fix)
2
  import os, time
3
  from threading import Thread
4
  import gradio as gr
@@ -130,59 +130,29 @@ def save_as_audio(text):
130
  # ---------------- Gradio Interface ----------------
131
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
132
  gr.Markdown("## ✍🏾 wilson Handwritten OCR")
133
-
134
  model_choice = gr.Radio(choices=list(MODEL_PATHS.keys()), value=list(MODEL_PATHS.keys())[0], label="Select OCR Model")
135
-
136
- with gr.Row(): # Two-column layout
137
- with gr.Column(scale=1): # Inputs
138
- query_input = gr.Textbox(label="Custom Prompt (optional)", placeholder="Leave empty for RAW structured output")
139
- image_input = gr.Image(type="pil", label="Upload / Capture Handwritten Image", sources=["upload", "webcam"])
140
-
141
- with gr.Accordion("⚙️ Advanced Options", open=False):
142
- max_new_tokens = gr.Slider(1, 2048, value=MAX_NEW_TOKENS_DEFAULT, step=1, label="Max new tokens")
143
- temperature = gr.Slider(0.1, 2.0, value=0.1, step=0.05, label="Temperature")
144
- top_p = gr.Slider(0.05, 1.0, value=1.0, step=0.05, label="Top-p (nucleus)")
145
- top_k = gr.Slider(0, 1000, value=0, step=1, label="Top-k")
146
- repetition_penalty = gr.Slider(0.8, 2.0, value=1.0, step=0.05, label="Repetition penalty")
147
-
148
- extract_btn = gr.Button("📤 Extract RAW Text", variant="primary")
149
- clear_btn = gr.Button("🧹 Clear")
150
-
151
- with gr.Column(scale=1): # Outputs
152
- gr.Markdown("## Output")
153
- output_clean = gr.Textbox(label="Clean OCR Output", interactive=False, lines=18, show_copy_button=True)
154
- with gr.Accordion("(Result.md)", open=False):
155
- markdown_output = gr.Markdown()
156
- pdf_file = gr.File(label="PDF File")
157
- word_file = gr.File(label="Word File")
158
- audio_file = gr.File(label="Audio File")
159
- pdf_btn = gr.Button("⬇️ Download as PDF")
160
- word_btn = gr.Button("⬇️ Download as Word")
161
- audio_btn = gr.Button("🔊 Download as Audio")
162
-
163
- # ---------------- Functions ----------------
164
- def ocr_and_clean(image, model_choice, query, max_new_tokens, temperature, top_p, top_k, repetition_penalty, progress=None):
165
- raw = ocr_image(
166
- image, model_choice, query, max_new_tokens, temperature, top_p, top_k, repetition_penalty, progress=progress
167
- )
168
- clean_text = raw.replace("<|im_end|>", "").strip()
169
- return clean_text, clean_text
170
-
171
- # Connect OCR extraction with Progress
172
- extract_btn.click(
173
- fn=ocr_and_clean,
174
- inputs=[image_input, model_choice, query_input, max_new_tokens, temperature, top_p, top_k, repetition_penalty, gr.Progress(track_tqdm=True)],
175
- outputs=[output_clean, markdown_output]
176
- )
177
-
178
- # Export buttons
179
- pdf_btn.click(fn=save_as_pdf, inputs=[output_clean], outputs=[pdf_file])
180
- word_btn.click(fn=save_as_word, inputs=[output_clean], outputs=[word_file])
181
- audio_btn.click(fn=save_as_audio, inputs=[output_clean], outputs=[audio_file])
182
-
183
- # Clear button
184
- clear_btn.click(fn=lambda: ("", None, "", MAX_NEW_TOKENS_DEFAULT, 0.1, 1.0, 0, 1.0, 1.0),
185
- outputs=[output_clean, image_input, query_input, max_new_tokens, temperature, top_p, top_k, repetition_penalty])
186
 
187
  if __name__ == "__main__":
188
  demo.queue(max_size=50).launch(show_error=True)
 
1
+ # app.py — HTR Space (Compact Version)
2
  import os, time
3
  from threading import Thread
4
  import gradio as gr
 
130
  # ---------------- Gradio Interface ----------------
131
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
132
  gr.Markdown("## ✍🏾 wilson Handwritten OCR")
 
133
  model_choice = gr.Radio(choices=list(MODEL_PATHS.keys()), value=list(MODEL_PATHS.keys())[0], label="Select OCR Model")
134
+ with gr.Tab("🖼 Image Inference"):
135
+ query_input = gr.Textbox(label="Custom Prompt (optional)", placeholder="Leave empty for RAW structured output")
136
+ image_input = gr.Image(type="pil", label="Upload / Capture Handwritten Image", sources=["upload", "webcam"])
137
+ with gr.Accordion("⚙️ Advanced Options", open=False):
138
+ max_new_tokens = gr.Slider(1, 2048, value=MAX_NEW_TOKENS_DEFAULT, step=1, label="Max new tokens")
139
+ temperature = gr.Slider(0.1, 2.0, value=0.1, step=0.05, label="Temperature")
140
+ top_p = gr.Slider(0.05, 1.0, value=1.0, step=0.05, label="Top-p (nucleus)")
141
+ top_k = gr.Slider(0, 1000, value=0, step=1, label="Top-k")
142
+ repetition_penalty = gr.Slider(0.8, 2.0, value=1.0, step=0.05, label="Repetition penalty")
143
+ extract_btn = gr.Button("📤 Extract RAW Text", variant="primary")
144
+ clear_btn = gr.Button("🧹 Clear")
145
+ raw_output = gr.Textbox(label="📜 RAW Structured Output (exact as written)", lines=18, show_copy_button=True)
146
+ pdf_btn = gr.Button("⬇️ Download as PDF")
147
+ word_btn = gr.Button("⬇️ Download as Word")
148
+ audio_btn = gr.Button("🔊 Download as Audio")
149
+ pdf_file, word_file, audio_file = gr.File(label="PDF File"), gr.File(label="Word File"), gr.File(label="Audio File")
150
+
151
+ extract_btn.click(fn=ocr_image, inputs=[image_input, model_choice, query_input, max_new_tokens, temperature, top_p, top_k, repetition_penalty], outputs=[raw_output], api_name="ocr_image")
152
+ pdf_btn.click(fn=save_as_pdf, inputs=[raw_output], outputs=[pdf_file])
153
+ word_btn.click(fn=save_as_word, inputs=[raw_output], outputs=[word_file])
154
+ audio_btn.click(fn=save_as_audio, inputs=[raw_output], outputs=[audio_file])
155
+ clear_btn.click(fn=lambda: ("", None, "", MAX_NEW_TOKENS_DEFAULT, 0.1, 1.0, 0, 1.0), outputs=[raw_output, image_input, query_input, max_new_tokens, temperature, top_p, top_k, repetition_penalty])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
  if __name__ == "__main__":
158
  demo.queue(max_size=50).launch(show_error=True)