Electro0023 commited on
Commit
20a07f1
·
verified ·
1 Parent(s): 7de0eef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -18
app.py CHANGED
@@ -1,37 +1,73 @@
1
- # app.py — use Qwen2-VL via together.ai or openrouter (free tier)
2
  import gradio as gr
3
- import base64
4
- from io import BytesIO
5
  from PIL import Image
6
- from openai import OpenAI
7
 
8
- client = OpenAI(
9
- api_key="your_free_api_key", # openrouter.ai free tier
10
- base_url="https://openrouter.ai/api/v1"
 
 
 
 
 
 
 
 
 
11
  )
 
 
 
12
 
13
  def extract_text(image: Image.Image) -> str:
14
- buffered = BytesIO()
15
- image.save(buffered, format="PNG")
16
- img_b64 = base64.b64encode(buffered.getvalue()).decode()
17
 
18
- response = client.chat.completions.create(
19
- model="qwen/qwen2-vl-7b-instruct:free", # free model
20
- messages=[{
21
  "role": "user",
22
  "content": [
23
- {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{img_b64}"}},
24
- {"type": "text", "text": "Extract all text exactly as it appears. Preserve question numbers, options A B C D, tables, and equations."}
 
 
 
 
 
25
  ]
26
- }]
 
 
 
 
 
27
  )
28
- return response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()