Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
import subprocess
|
| 4 |
import sys
|
| 5 |
-
import threading
|
| 6 |
|
| 7 |
import spaces
|
| 8 |
import torch
|
|
@@ -14,7 +13,6 @@ import pypdfium2 as pdfium
|
|
| 14 |
from transformers import (
|
| 15 |
LightOnOCRForConditionalGeneration,
|
| 16 |
LightOnOCRProcessor,
|
| 17 |
-
TextIteratorStreamer,
|
| 18 |
)
|
| 19 |
|
| 20 |
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
|
@@ -108,7 +106,7 @@ def clean_output_text(text):
|
|
| 108 |
|
| 109 |
|
| 110 |
@spaces.GPU
|
| 111 |
-
def extract_text_from_image(image, temperature=0.2
|
| 112 |
"""Extract text from image using LightOnOCR model, and run clinical NER."""
|
| 113 |
# Prepare the chat format
|
| 114 |
chat = [
|
|
@@ -149,55 +147,35 @@ def extract_text_from_image(image, temperature=0.2, stream=False):
|
|
| 149 |
do_sample=temperature > 0,
|
| 150 |
)
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
processor.tokenizer,
|
| 156 |
-
skip_prompt=True,
|
| 157 |
-
skip_special_tokens=True,
|
| 158 |
-
)
|
| 159 |
-
generation_kwargs["streamer"] = streamer
|
| 160 |
-
|
| 161 |
-
thread = threading.Thread(target=ocr_model.generate, kwargs=generation_kwargs)
|
| 162 |
-
thread.start()
|
| 163 |
-
|
| 164 |
-
full_text = ""
|
| 165 |
-
for new_text in streamer:
|
| 166 |
-
full_text += new_text
|
| 167 |
-
cleaned_text = clean_output_text(full_text)
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
yield cleaned_text, ""
|
| 172 |
|
| 173 |
-
|
| 174 |
-
else:
|
| 175 |
-
# Non-streaming generation
|
| 176 |
-
with torch.no_grad():
|
| 177 |
-
outputs = ocr_model.generate(**generation_kwargs)
|
| 178 |
-
|
| 179 |
-
output_text = processor.decode(outputs[0], skip_special_tokens=True)
|
| 180 |
-
cleaned_text = clean_output_text(output_text)
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
| 192 |
|
| 193 |
-
|
| 194 |
|
| 195 |
-
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
-
def process_input(file_input, temperature, page_num
|
| 201 |
"""Process uploaded file (image or PDF) and extract text with optional streaming."""
|
| 202 |
if file_input is None:
|
| 203 |
# 6 outputs: [output_text, medications_output, raw_output, page_info, rendered_image, num_pages]
|
|
@@ -233,7 +211,7 @@ def process_input(file_input, temperature, page_num, enable_streaming):
|
|
| 233 |
try:
|
| 234 |
# Extract text using LightOnOCR with optional streaming
|
| 235 |
for extracted_text, medications in extract_text_from_image(
|
| 236 |
-
image_to_process, temperature
|
| 237 |
):
|
| 238 |
raw_md = extracted_text # or you can keep a different raw version
|
| 239 |
# 6 outputs: markdown_text, medications, raw_output, page_info, image, slider
|
|
@@ -318,12 +296,6 @@ with gr.Blocks(title="📖 Image/PDF OCR with LightOnOCR", theme=gr.themes.Soft(
|
|
| 318 |
label="Temperature",
|
| 319 |
info="0.0 = deterministic, Higher = more varied"
|
| 320 |
)
|
| 321 |
-
enable_streaming = gr.Checkbox(
|
| 322 |
-
label="Enable Streaming",
|
| 323 |
-
value=True,
|
| 324 |
-
info="Show text progressively as it's generated"
|
| 325 |
-
)
|
| 326 |
-
submit_btn = gr.Button("Extract Text", variant="primary")
|
| 327 |
clear_btn = gr.Button("Clear", variant="secondary")
|
| 328 |
|
| 329 |
with gr.Column(scale=2):
|
|
@@ -353,7 +325,7 @@ with gr.Blocks(title="📖 Image/PDF OCR with LightOnOCR", theme=gr.themes.Soft(
|
|
| 353 |
# Event handlers
|
| 354 |
submit_btn.click(
|
| 355 |
fn=process_input,
|
| 356 |
-
inputs=[file_input, temperature, num_pages,
|
| 357 |
outputs=[output_text, medications_output, raw_output, page_info, rendered_image, num_pages]
|
| 358 |
)
|
| 359 |
|
|
|
|
| 2 |
|
| 3 |
import subprocess
|
| 4 |
import sys
|
|
|
|
| 5 |
|
| 6 |
import spaces
|
| 7 |
import torch
|
|
|
|
| 13 |
from transformers import (
|
| 14 |
LightOnOCRForConditionalGeneration,
|
| 15 |
LightOnOCRProcessor,
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
|
|
|
| 106 |
|
| 107 |
|
| 108 |
@spaces.GPU
|
| 109 |
+
def extract_text_from_image(image, temperature=0.2):
|
| 110 |
"""Extract text from image using LightOnOCR model, and run clinical NER."""
|
| 111 |
# Prepare the chat format
|
| 112 |
chat = [
|
|
|
|
| 147 |
do_sample=temperature > 0,
|
| 148 |
)
|
| 149 |
|
| 150 |
+
# Non-streaming generation
|
| 151 |
+
with torch.no_grad():
|
| 152 |
+
outputs = ocr_model.generate(**generation_kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
output_text = processor.decode(outputs[0], skip_special_tokens=True)
|
| 155 |
+
cleaned_text = clean_output_text(output_text)
|
|
|
|
| 156 |
|
| 157 |
+
print("\n this is cleaned_text",cleaned_text )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
# Clinical NER on the full cleaned text
|
| 160 |
+
entities = ner_pipeline(cleaned_text)
|
| 161 |
+
print("\n this is entity",entities)
|
| 162 |
+
medications = []
|
| 163 |
+
for ent in entities:
|
| 164 |
+
if ent["entity_group"] == "treatment":
|
| 165 |
+
word = ent["word"]
|
| 166 |
+
if word.startswith("##") and medications:
|
| 167 |
+
medications[-1] += word[2:]
|
| 168 |
+
else:
|
| 169 |
+
medications.append(word)
|
| 170 |
|
| 171 |
+
medications_str = ", ".join(set(medications)) if medications else "None detected"
|
| 172 |
|
| 173 |
+
yield cleaned_text, medications_str
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
+
def process_input(file_input, temperature, page_num):
|
| 179 |
"""Process uploaded file (image or PDF) and extract text with optional streaming."""
|
| 180 |
if file_input is None:
|
| 181 |
# 6 outputs: [output_text, medications_output, raw_output, page_info, rendered_image, num_pages]
|
|
|
|
| 211 |
try:
|
| 212 |
# Extract text using LightOnOCR with optional streaming
|
| 213 |
for extracted_text, medications in extract_text_from_image(
|
| 214 |
+
image_to_process, temperature
|
| 215 |
):
|
| 216 |
raw_md = extracted_text # or you can keep a different raw version
|
| 217 |
# 6 outputs: markdown_text, medications, raw_output, page_info, image, slider
|
|
|
|
| 296 |
label="Temperature",
|
| 297 |
info="0.0 = deterministic, Higher = more varied"
|
| 298 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
clear_btn = gr.Button("Clear", variant="secondary")
|
| 300 |
|
| 301 |
with gr.Column(scale=2):
|
|
|
|
| 325 |
# Event handlers
|
| 326 |
submit_btn.click(
|
| 327 |
fn=process_input,
|
| 328 |
+
inputs=[file_input, temperature, num_pages, ],
|
| 329 |
outputs=[output_text, medications_output, raw_output, page_info, rendered_image, num_pages]
|
| 330 |
)
|
| 331 |
|