Electro0023 commited on
Commit
6a57222
·
verified ·
1 Parent(s): 5615f77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -1,29 +1,40 @@
1
  import gradio as gr
2
  import torch
3
- from transformers import AutoProcessor, AutoModel
4
  from PIL import Image
5
 
6
- # Load model and processor using generic AutoModel
7
- model_id = "ibm-granite/granite-docling-258M"
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 = AutoModel.from_pretrained(model_id, trust_remote_code=True, torch_dtype=torch.float32)
 
 
 
 
11
 
12
  def process_image(image):
13
- # Convert image to RGB
 
 
 
 
 
 
 
14
  image = image.convert("RGB")
15
 
16
- # Prepare the inputs
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 output
22
- output = model.generate(**inputs, max_new_tokens=500)
23
- result = processor.decode(output[0], skip_special_tokens=True)
24
- return result
 
25
 
26
- # Create interface
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"),