Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
# Load model and processor
|
| 7 |
-
model_id = "HuggingFaceM4/Idefics3-8B-Llama3"
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
model = Idefics3ForConditionalGeneration.from_pretrained(
|
| 10 |
model_id,
|
| 11 |
trust_remote_code=True,
|
|
@@ -16,22 +19,22 @@ def process_image(image):
|
|
| 16 |
# Safety check for empty input
|
| 17 |
if image is None:
|
| 18 |
return "Please upload an image first."
|
| 19 |
-
|
| 20 |
# Ensure image is PIL format
|
| 21 |
if not isinstance(image, Image.Image):
|
| 22 |
image = Image.fromarray(image)
|
| 23 |
-
|
| 24 |
image = image.convert("RGB")
|
| 25 |
-
|
| 26 |
# Prepare inputs
|
| 27 |
messages = [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "Describe this image."}]}]
|
| 28 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 29 |
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 30 |
-
|
| 31 |
# Generate
|
| 32 |
generated_ids = model.generate(**inputs, max_new_tokens=500)
|
| 33 |
result = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 34 |
-
|
| 35 |
return result[0]
|
| 36 |
|
| 37 |
# UI Setup
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import Idefics3Processor, Idefics3ForConditionalGeneration
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
# Load model and processor
|
| 7 |
+
model_id = "HuggingFaceM4/Idefics3-8B-Llama3"
|
| 8 |
+
|
| 9 |
+
# FIX: Import the specific processor class directly to bypass the AutoProcessor bug
|
| 10 |
+
processor = Idefics3Processor.from_pretrained(model_id, trust_remote_code=True)
|
| 11 |
+
|
| 12 |
model = Idefics3ForConditionalGeneration.from_pretrained(
|
| 13 |
model_id,
|
| 14 |
trust_remote_code=True,
|
|
|
|
| 19 |
# Safety check for empty input
|
| 20 |
if image is None:
|
| 21 |
return "Please upload an image first."
|
| 22 |
+
|
| 23 |
# Ensure image is PIL format
|
| 24 |
if not isinstance(image, Image.Image):
|
| 25 |
image = Image.fromarray(image)
|
| 26 |
+
|
| 27 |
image = image.convert("RGB")
|
| 28 |
+
|
| 29 |
# Prepare inputs
|
| 30 |
messages = [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "Describe this image."}]}]
|
| 31 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 32 |
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 33 |
+
|
| 34 |
# Generate
|
| 35 |
generated_ids = model.generate(**inputs, max_new_tokens=500)
|
| 36 |
result = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 37 |
+
|
| 38 |
return result[0]
|
| 39 |
|
| 40 |
# UI Setup
|