Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def process_image(image):
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 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()
|