Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoProcessor,
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
# Load model and processor
|
| 7 |
-
model_id = "
|
| 8 |
-
# We must use trust_remote_code=True for this specific model
|
| 9 |
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 10 |
-
model =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def process_image(image):
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
image = image.convert("RGB")
|
| 15 |
|
| 16 |
-
# Prepare
|
| 17 |
-
messages = [{"role": "user", "content": [{"type": "image"}]}]
|
| 18 |
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
| 19 |
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 20 |
|
| 21 |
-
# Generate
|
| 22 |
-
|
| 23 |
-
result = processor.
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
demo = gr.Interface(
|
| 28 |
fn=process_image,
|
| 29 |
inputs=gr.Image(type="pil"),
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoProcessor, Idefics3ForConditionalGeneration
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Load model and processor
|
| 7 |
+
model_id = "HuggingFaceM4/Idefics3-8B-Llama3" # Ensure this matches your model
|
|
|
|
| 8 |
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 9 |
+
model = Idefics3ForConditionalGeneration.from_pretrained(
|
| 10 |
+
model_id,
|
| 11 |
+
trust_remote_code=True,
|
| 12 |
+
torch_dtype=torch.float32
|
| 13 |
+
)
|
| 14 |
|
| 15 |
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
|
| 38 |
demo = gr.Interface(
|
| 39 |
fn=process_image,
|
| 40 |
inputs=gr.Image(type="pil"),
|