satyam007 commited on
Commit
d109c3e
·
verified ·
1 Parent(s): 999fdaf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
- from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
3
- from qwen_vl_utils import process_vision_info
4
  import torch
5
  import pandas as pd
6
  import pytesseract
@@ -9,19 +9,16 @@ import cv2
9
  # Set Tesseract command (only works if Tesseract is already installed on the hosting server)
10
  pytesseract.pytesseract_cmd = r'/usr/bin/tesseract'
11
 
12
- # Check if GPU is available, and use it if possible
13
- device = "cuda" if torch.cuda.is_available() else "cpu"
14
 
15
- # Initialize the model and processor
16
- try:
17
- model = Qwen2VLForConditionalGeneration.from_pretrained(
18
- "Qwen/Qwen2-VL-2B-Instruct-AWQ",
19
- torch_dtype="auto"
20
- )
21
- model.to(device) # Move the model to the selected device (CPU or GPU)
22
- processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct-AWQ")
23
- except Exception as e:
24
- print(f"Error loading model: {e}")
25
 
26
  # Preprocessing image for OCR
27
  def preprocess_image(image_path):
@@ -53,11 +50,10 @@ def process_image(image_path):
53
  ]
54
  }]
55
 
56
- # Process the vision info and prepare inputs
57
  text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
58
  image_inputs, video_inputs = process_vision_info(messages)
59
  inputs = processor(text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt")
60
- inputs = inputs.to(device)
61
 
62
  generated_ids = model.generate(**inputs, max_new_tokens=128)
63
  output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
 
1
  import gradio as gr
2
+ from transformers import AutoModelForConditionalGeneration, AutoProcessor
3
+ from huggingface_hub import hf_api
4
  import torch
5
  import pandas as pd
6
  import pytesseract
 
9
  # Set Tesseract command (only works if Tesseract is already installed on the hosting server)
10
  pytesseract.pytesseract_cmd = r'/usr/bin/tesseract'
11
 
12
+ # Initialize the model and processor from Hugging Face Hub
13
+ model_name = "Qwen/Qwen2-VL-2B-Instruct-AWQ"
14
 
15
+ model = AutoModelForConditionalGeneration.from_pretrained(
16
+ model_name,
17
+ torch_dtype="auto"
18
+ )
19
+ model.to("cpu")
20
+
21
+ processor = AutoProcessor.from_pretrained(model_name)
 
 
 
22
 
23
  # Preprocessing image for OCR
24
  def preprocess_image(image_path):
 
50
  ]
51
  }]
52
 
 
53
  text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
54
  image_inputs, video_inputs = process_vision_info(messages)
55
  inputs = processor(text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt")
56
+ inputs = inputs.to(model.device)
57
 
58
  generated_ids = model.generate(**inputs, max_new_tokens=128)
59
  output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)