qqwjq1981 commited on
Commit
15f3f02
Β·
verified Β·
1 Parent(s): 311f05f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -4
app.py CHANGED
@@ -1,21 +1,24 @@
 
1
  import gradio as gr
2
  from transformers import AutoModelForVision2Seq, AutoProcessor
3
  import torch
4
  from PIL import Image
5
  import os
6
 
7
- # Load Qwen-VL model and processor
8
  model_id = "Qwen/Qwen-VL-Chat"
9
- processor = AutoProcessor.from_pretrained(model_id)
10
- model = AutoModelForVision2Seq.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
 
11
 
12
  # Inference function
13
  def ocr_with_qwen(image):
 
14
  if image is None:
15
  image = Image.open("test.png")
16
 
17
  prompt = "<|im_start|>system\nYou are a helpful assistant. Extract all text from the image and output only the text.<|im_end|>\n<|im_start|>user\n"
18
- inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device)
19
  outputs = model.generate(**inputs, max_new_tokens=512)
20
  result = processor.batch_decode(outputs, skip_special_tokens=True)[0]
21
  return result.strip()
 
1
+ # app.py
2
  import gradio as gr
3
  from transformers import AutoModelForVision2Seq, AutoProcessor
4
  import torch
5
  from PIL import Image
6
  import os
7
 
8
+ # Load Qwen-VL model and processor (trust custom code)
9
  model_id = "Qwen/Qwen-VL-Chat"
10
+ processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
11
+ model = AutoModelForVision2Seq.from_pretrained(model_id, trust_remote_code=True)
12
+ model = model.to("cpu")
13
 
14
  # Inference function
15
  def ocr_with_qwen(image):
16
+ # Fallback to test.png if no image uploaded
17
  if image is None:
18
  image = Image.open("test.png")
19
 
20
  prompt = "<|im_start|>system\nYou are a helpful assistant. Extract all text from the image and output only the text.<|im_end|>\n<|im_start|>user\n"
21
+ inputs = processor(images=image, text=prompt, return_tensors="pt").to("cpu")
22
  outputs = model.generate(**inputs, max_new_tokens=512)
23
  result = processor.batch_decode(outputs, skip_special_tokens=True)[0]
24
  return result.strip()