Spaces:
Sleeping
Sleeping
Commit
·
a02d815
1
Parent(s):
5426c44
Updated with OCR model and Gradio integration
Browse files
app.py
CHANGED
|
@@ -7,17 +7,22 @@ repo_id = "OpenGVLab/InternVL2-1B"
|
|
| 7 |
# Load the tokenizer, processor, and model directly from the Hub
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(repo_id, trust_remote_code=True)
|
| 9 |
processor = AutoProcessor.from_pretrained(repo_id, trust_remote_code=True)
|
| 10 |
-
model = AutoModel.from_pretrained(
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Move model to the appropriate device
|
| 13 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
model.to(device)
|
| 15 |
|
| 16 |
def analyze_image(image):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
demo = gr.Interface(
|
| 23 |
fn=analyze_image,
|
|
|
|
| 7 |
# Load the tokenizer, processor, and model directly from the Hub
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(repo_id, trust_remote_code=True)
|
| 9 |
processor = AutoProcessor.from_pretrained(repo_id, trust_remote_code=True)
|
| 10 |
+
model = AutoModel.from_pretrained(
|
| 11 |
+
repo_id, trust_remote_code=True, torch_dtype=torch.float16
|
| 12 |
+
)
|
| 13 |
|
| 14 |
# Move model to the appropriate device
|
| 15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
model.to(device)
|
| 17 |
|
| 18 |
def analyze_image(image):
|
| 19 |
+
try:
|
| 20 |
+
img = image.convert("RGB")
|
| 21 |
+
inputs = processor(images=img, text="describe this image", return_tensors="pt").to(device)
|
| 22 |
+
outputs = model.generate(**inputs)
|
| 23 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"An error occurred: {str(e)}"
|
| 26 |
|
| 27 |
demo = gr.Interface(
|
| 28 |
fn=analyze_image,
|