Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,21 @@
|
|
| 1 |
import torch
|
|
|
|
| 2 |
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 3 |
from PIL import Image
|
| 4 |
-
from fastapi import FastAPI, UploadFile, File
|
| 5 |
-
from fastapi.responses import JSONResponse
|
| 6 |
-
import io
|
| 7 |
-
import transformers
|
| 8 |
-
print(f"Transformers version: {transformers.__version__}")
|
| 9 |
|
| 10 |
-
app = FastAPI()
|
| 11 |
-
|
| 12 |
-
# SmolVLM - lightweight, works on CPU/low VRAM, same team as Idefics3
|
| 13 |
model_id = "HuggingFaceTB/SmolVLM-Instruct"
|
| 14 |
|
| 15 |
-
print("Loading processor...")
|
| 16 |
-
processor = AutoProcessor.from_pretrained(model_id)
|
| 17 |
-
|
| 18 |
print("Loading model...")
|
|
|
|
| 19 |
model = AutoModelForVision2Seq.from_pretrained(
|
| 20 |
model_id,
|
| 21 |
-
torch_dtype=torch.float32,
|
| 22 |
device_map="auto"
|
| 23 |
)
|
| 24 |
model.eval()
|
| 25 |
print("Model ready!")
|
| 26 |
|
| 27 |
-
|
| 28 |
-
@app.get("/")
|
| 29 |
-
def root():
|
| 30 |
-
return {"status": "running", "model": model_id}
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
@app.post("/extract")
|
| 34 |
-
async def extract_text(file: UploadFile = File(...)):
|
| 35 |
-
# Read uploaded image
|
| 36 |
-
contents = await file.read()
|
| 37 |
-
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 38 |
-
|
| 39 |
-
# Prompt for NEET question extraction
|
| 40 |
messages = [
|
| 41 |
{
|
| 42 |
"role": "user",
|
|
@@ -44,7 +23,7 @@ async def extract_text(file: UploadFile = File(...)):
|
|
| 44 |
{"type": "image"},
|
| 45 |
{"type": "text", "text": (
|
| 46 |
"Extract all text from this image exactly as it appears. "
|
| 47 |
-
"Preserve question numbers, options
|
| 48 |
"and any mathematical or chemical expressions. "
|
| 49 |
"Format clearly."
|
| 50 |
)}
|
|
@@ -53,12 +32,7 @@ async def extract_text(file: UploadFile = File(...)):
|
|
| 53 |
]
|
| 54 |
|
| 55 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 56 |
-
|
| 57 |
-
inputs = processor(
|
| 58 |
-
text=prompt,
|
| 59 |
-
images=[image],
|
| 60 |
-
return_tensors="pt"
|
| 61 |
-
)
|
| 62 |
|
| 63 |
with torch.no_grad():
|
| 64 |
outputs = model.generate(
|
|
@@ -67,8 +41,16 @@ async def extract_text(file: UploadFile = File(...)):
|
|
| 67 |
do_sample=False
|
| 68 |
)
|
| 69 |
|
| 70 |
-
# Decode only the generated part
|
| 71 |
generated = outputs[0][inputs["input_ids"].shape[1]:]
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
model_id = "HuggingFaceTB/SmolVLM-Instruct"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
print("Loading model...")
|
| 9 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 10 |
model = AutoModelForVision2Seq.from_pretrained(
|
| 11 |
model_id,
|
| 12 |
+
torch_dtype=torch.float32,
|
| 13 |
device_map="auto"
|
| 14 |
)
|
| 15 |
model.eval()
|
| 16 |
print("Model ready!")
|
| 17 |
|
| 18 |
+
def extract_text(image: Image.Image) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
messages = [
|
| 20 |
{
|
| 21 |
"role": "user",
|
|
|
|
| 23 |
{"type": "image"},
|
| 24 |
{"type": "text", "text": (
|
| 25 |
"Extract all text from this image exactly as it appears. "
|
| 26 |
+
"Preserve question numbers, options A B C D, tables, "
|
| 27 |
"and any mathematical or chemical expressions. "
|
| 28 |
"Format clearly."
|
| 29 |
)}
|
|
|
|
| 32 |
]
|
| 33 |
|
| 34 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 35 |
+
inputs = processor(text=prompt, images=[image], return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
with torch.no_grad():
|
| 38 |
outputs = model.generate(
|
|
|
|
| 41 |
do_sample=False
|
| 42 |
)
|
| 43 |
|
|
|
|
| 44 |
generated = outputs[0][inputs["input_ids"].shape[1]:]
|
| 45 |
+
return processor.decode(generated, skip_special_tokens=True)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
demo = gr.Interface(
|
| 49 |
+
fn=extract_text,
|
| 50 |
+
inputs=gr.Image(type="pil", label="Upload NEET Question Image"),
|
| 51 |
+
outputs=gr.Textbox(label="Extracted Text", lines=20),
|
| 52 |
+
title="NEET Question Extractor",
|
| 53 |
+
description="Upload a scanned NEET question paper image to extract text"
|
| 54 |
+
)
|
| 55 |
|
| 56 |
+
demo.launch()
|