Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,73 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
-
from io import BytesIO
|
| 5 |
from PIL import Image
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def extract_text(image: Image.Image) -> str:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
img_b64 = base64.b64encode(buffered.getvalue()).decode()
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
messages=[{
|
| 21 |
"role": "user",
|
| 22 |
"content": [
|
| 23 |
-
{"type": "
|
| 24 |
-
{"type": "text", "text":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
]
|
| 26 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
demo = gr.Interface(
|
| 31 |
fn=extract_text,
|
| 32 |
inputs=gr.Image(type="pil", label="Upload NEET Question Image"),
|
| 33 |
outputs=gr.Textbox(label="Extracted Text", lines=20),
|
| 34 |
-
title="NEET Question Extractor"
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
demo.launch()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
|
| 8 |
+
|
| 9 |
+
model_id = "HuggingFaceTB/SmolVLM-256M-Instruct" # smallest possible - 256M params
|
| 10 |
+
|
| 11 |
+
print("Loading processor...")
|
| 12 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 13 |
+
|
| 14 |
+
print("Loading model...")
|
| 15 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 16 |
+
model_id,
|
| 17 |
+
torch_dtype=torch.float32,
|
| 18 |
+
device_map="auto"
|
| 19 |
)
|
| 20 |
+
model.eval()
|
| 21 |
+
print("Model ready!")
|
| 22 |
+
|
| 23 |
|
| 24 |
def extract_text(image: Image.Image) -> str:
|
| 25 |
+
if image is None:
|
| 26 |
+
return "Please upload an image."
|
|
|
|
| 27 |
|
| 28 |
+
messages = [
|
| 29 |
+
{
|
|
|
|
| 30 |
"role": "user",
|
| 31 |
"content": [
|
| 32 |
+
{"type": "image"},
|
| 33 |
+
{"type": "text", "text": (
|
| 34 |
+
"Extract all text from this image exactly as it appears. "
|
| 35 |
+
"Preserve question numbers, options A B C D, "
|
| 36 |
+
"tables, and any mathematical or chemical expressions. "
|
| 37 |
+
"Format clearly."
|
| 38 |
+
)}
|
| 39 |
]
|
| 40 |
+
}
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
prompt = processor.apply_chat_template(
|
| 44 |
+
messages,
|
| 45 |
+
add_generation_prompt=True
|
| 46 |
)
|
| 47 |
+
|
| 48 |
+
inputs = processor(
|
| 49 |
+
text=prompt,
|
| 50 |
+
images=[image],
|
| 51 |
+
return_tensors="pt"
|
| 52 |
+
).to(model.device)
|
| 53 |
+
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
outputs = model.generate(
|
| 56 |
+
**inputs,
|
| 57 |
+
max_new_tokens=1024,
|
| 58 |
+
do_sample=False
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
generated = outputs[0][inputs["input_ids"].shape[1]:]
|
| 62 |
+
return processor.decode(generated, skip_special_tokens=True)
|
| 63 |
+
|
| 64 |
|
| 65 |
demo = gr.Interface(
|
| 66 |
fn=extract_text,
|
| 67 |
inputs=gr.Image(type="pil", label="Upload NEET Question Image"),
|
| 68 |
outputs=gr.Textbox(label="Extracted Text", lines=20),
|
| 69 |
+
title="NEET Question Extractor",
|
| 70 |
+
description="Upload a scanned NEET question paper image to extract text"
|
| 71 |
)
|
| 72 |
|
| 73 |
demo.launch()
|