Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,9 +21,9 @@ def load_model():
|
|
| 21 |
"google/paligemma2-28b-pt-896", use_auth_token=token
|
| 22 |
)
|
| 23 |
model = AutoModelForImageTextToText.from_pretrained(
|
| 24 |
-
"google/paligemma2-28b-pt-896", use_auth_token=token, torch_dtype=torch.
|
| 25 |
)
|
| 26 |
-
|
| 27 |
# Move model to GPU if available
|
| 28 |
if torch.cuda.is_available():
|
| 29 |
model = model.to("cuda")
|
|
@@ -32,16 +32,18 @@ def load_model():
|
|
| 32 |
|
| 33 |
|
| 34 |
@spaces.GPU # Decorate the function that uses the GPU
|
| 35 |
-
def
|
| 36 |
"""Extract text from image using PaliGemma2."""
|
| 37 |
processor, model = load_model()
|
| 38 |
|
| 39 |
-
# Preprocess the image
|
| 40 |
-
inputs = processor(images=image, return_tensors="pt").to(
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Generate predictions
|
| 43 |
with torch.no_grad():
|
| 44 |
-
generated_ids = model.generate(**inputs)
|
| 45 |
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 46 |
|
| 47 |
return text
|
|
@@ -49,10 +51,13 @@ def process_image(image):
|
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
iface = gr.Interface(
|
| 52 |
-
fn=
|
| 53 |
-
inputs=
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
iface.launch()
|
|
|
|
| 21 |
"google/paligemma2-28b-pt-896", use_auth_token=token
|
| 22 |
)
|
| 23 |
model = AutoModelForImageTextToText.from_pretrained(
|
| 24 |
+
"google/paligemma2-28b-pt-896", use_auth_token=token, torch_dtype=torch.bfloat16
|
| 25 |
)
|
| 26 |
+
|
| 27 |
# Move model to GPU if available
|
| 28 |
if torch.cuda.is_available():
|
| 29 |
model = model.to("cuda")
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
@spaces.GPU # Decorate the function that uses the GPU
|
| 35 |
+
def process_image_and_text(image, text_input):
|
| 36 |
"""Extract text from image using PaliGemma2."""
|
| 37 |
processor, model = load_model()
|
| 38 |
|
| 39 |
+
# Preprocess the image and text
|
| 40 |
+
inputs = processor(text=text_input, images=image, return_tensors="pt").to(
|
| 41 |
+
"cuda" if torch.cuda.is_available() else "cpu", dtype=torch.bfloat16
|
| 42 |
+
)
|
| 43 |
|
| 44 |
# Generate predictions
|
| 45 |
with torch.no_grad():
|
| 46 |
+
generated_ids = model.generate(**inputs, max_new_tokens=100)
|
| 47 |
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 48 |
|
| 49 |
return text
|
|
|
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
iface = gr.Interface(
|
| 54 |
+
fn=process_image_and_text,
|
| 55 |
+
inputs=[
|
| 56 |
+
gr.Image(type="pil", label="Upload an image containing text"),
|
| 57 |
+
gr.Textbox(label="Enter Text Prompt"),
|
| 58 |
+
],
|
| 59 |
+
outputs=gr.Textbox(label="Extracted/Generated Text"),
|
| 60 |
+
title="Text Reading/Generation with PaliGemma2",
|
| 61 |
+
description="Upload an image and enter a text prompt. The model will generate text based on both.",
|
| 62 |
)
|
| 63 |
iface.launch()
|