Electro0023 commited on
Commit
924e33b
·
verified ·
1 Parent(s): c723d07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -1,21 +1,33 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
3
 
4
- # This line downloads the model from Hugging Face
5
- pipe = pipeline("image-text-to-text", model="ibm-granite/granite-docling-258M")
 
 
 
6
 
7
  def process_image(image):
8
- # This runs the model on the uploaded image
9
- result = pipe(image)
10
- # This returns the text extracted from the document
11
- return str(result)
 
 
 
 
 
 
 
 
12
 
13
- # This creates the user interface
14
  demo = gr.Interface(
15
  fn=process_image,
16
  inputs=gr.Image(type="pil"),
17
  outputs="text"
18
  )
19
 
20
- # This launches the app
21
  demo.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoProcessor, AutoModelForVision2Seq
4
+ from PIL import Image
5
 
6
+ # Load model and processor
7
+ model_id = "ibm-granite/granite-docling-258M"
8
+ # We must use trust_remote_code=True for this specific model architecture
9
+ processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
10
+ model = AutoModelForVision2Seq.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"),
30
  outputs="text"
31
  )
32
 
 
33
  demo.launch()