Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import PaliGemmaProcessor, PaliGemmaForConditionalGeneration
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def load_model():
|
| 8 |
+
"""Load PaliGemma2 model and processor."""
|
| 9 |
+
processor = PaliGemmaProcessor.from_pretrained("google/paligemma2")
|
| 10 |
+
model = PaliGemmaForConditionalGeneration.from_pretrained("google/paligemma2")
|
| 11 |
+
return processor, model
|
| 12 |
+
|
| 13 |
+
def process_image(image, processor, model):
|
| 14 |
+
"""Extract text from image using PaliGemma2."""
|
| 15 |
+
# Preprocess the image
|
| 16 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 17 |
+
|
| 18 |
+
# Generate predictions
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
generated_ids = model.generate(**inputs)
|
| 21 |
+
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 22 |
+
|
| 23 |
+
return text
|
| 24 |
+
|
| 25 |
+
def main():
|
| 26 |
+
# Set page configuration
|
| 27 |
+
st.set_page_config(page_title="Text Reading with PaliGemma2", layout="centered")
|
| 28 |
+
st.title("Text Reading from Images using PaliGemma2")
|
| 29 |
+
|
| 30 |
+
# Load model and processor
|
| 31 |
+
with st.spinner("Loading PaliGemma2 model... This may take a few moments."):
|
| 32 |
+
processor, model = load_model()
|
| 33 |
+
st.success("Model loaded successfully!")
|
| 34 |
+
|
| 35 |
+
# User input: upload image
|
| 36 |
+
uploaded_image = st.file_uploader("Upload an image containing text", type=["png", "jpg", "jpeg"])
|
| 37 |
+
|
| 38 |
+
if uploaded_image is not None:
|
| 39 |
+
# Display uploaded image
|
| 40 |
+
image = Image.open(uploaded_image)
|
| 41 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 42 |
+
|
| 43 |
+
# Extract text button
|
| 44 |
+
if st.button("Extract Text"):
|
| 45 |
+
with st.spinner("Processing image..."):
|
| 46 |
+
extracted_text = process_image(image, processor, model)
|
| 47 |
+
st.success("Text extraction complete!")
|
| 48 |
+
st.subheader("Extracted Text")
|
| 49 |
+
st.write(extracted_text)
|
| 50 |
+
|
| 51 |
+
# Footer
|
| 52 |
+
st.markdown("---")
|
| 53 |
+
st.markdown("**Built with [PaliGemma2](https://huggingface.co/google/paligemma2) and Streamlit**")
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
main()
|