Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
-
from
|
| 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 |
-
#
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 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)
|