Update app.py
Browse files
app.py
CHANGED
@@ -23,4 +23,41 @@ def process_image(image, processor, model):
|
|
23 |
generated_ids = model.generate(**inputs)
|
24 |
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
25 |
|
26 |
-
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
generated_ids = model.generate(**inputs)
|
24 |
text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
25 |
|
26 |
+
return text
|
27 |
+
|
28 |
+
def main():
|
29 |
+
# Set page configuration
|
30 |
+
st.set_page_config(page_title="Text Reading with PaliGemma2", layout="centered")
|
31 |
+
st.title("Text Reading from Images using PaliGemma2")
|
32 |
+
|
33 |
+
# Load model and processor
|
34 |
+
with st.spinner("Loading PaliGemma2 model... This may take a few moments."):
|
35 |
+
try:
|
36 |
+
processor, model = load_model()
|
37 |
+
st.success("Model loaded successfully!")
|
38 |
+
except ValueError as e:
|
39 |
+
st.error(str(e))
|
40 |
+
st.stop()
|
41 |
+
|
42 |
+
# User input: upload image
|
43 |
+
uploaded_image = st.file_uploader("Upload an image containing text", type=["png", "jpg", "jpeg"])
|
44 |
+
|
45 |
+
if uploaded_image is not None:
|
46 |
+
# Display uploaded image
|
47 |
+
image = Image.open(uploaded_image)
|
48 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
49 |
+
|
50 |
+
# Extract text button
|
51 |
+
if st.button("Extract Text"):
|
52 |
+
with st.spinner("Processing image..."):
|
53 |
+
extracted_text = process_image(image, processor, model)
|
54 |
+
st.success("Text extraction complete!")
|
55 |
+
st.subheader("Extracted Text")
|
56 |
+
st.write(extracted_text)
|
57 |
+
|
58 |
+
# Footer
|
59 |
+
st.markdown("---")
|
60 |
+
st.markdown("**Built with [PaliGemma2](https://huggingface.co/google/paligemma2) and Streamlit**")
|
61 |
+
|
62 |
+
if __name__ == "__main__":
|
63 |
+
main()
|