Electro0023 commited on
Commit
835bd3f
·
verified ·
1 Parent(s): bb89ae6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -39
app.py CHANGED
@@ -1,56 +1,37 @@
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",
22
  "content": [
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
- )}
30
  ]
31
- }
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(
39
- **inputs,
40
- max_new_tokens=1024,
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()
 
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()