Files
patchy631--ai-engineering-hub/gemma3-ocr/app.py
T
2026-07-13 12:37:47 +08:00

67 lines
2.6 KiB
Python

import streamlit as st
import ollama
from PIL import Image
import io
import base64
# Page configuration
st.set_page_config(
page_title="Gemma-3 OCR",
page_icon="🔎",
layout="wide",
initial_sidebar_state="expanded"
)
# Title and description in main area
st.markdown("""
# <img src="data:image/png;base64,{}" width="50" style="vertical-align: -12px;"> Gemma-3 OCR
""".format(base64.b64encode(open("./assets/gemma3.png", "rb").read()).decode()), unsafe_allow_html=True)
# Add clear button to top right
col1, col2 = st.columns([6,1])
with col2:
if st.button("Clear 🗑️"):
if 'ocr_result' in st.session_state:
del st.session_state['ocr_result']
st.rerun()
st.markdown('<p style="margin-top: -20px;">Extract structured text from images using Gemma-3 Vision!</p>', unsafe_allow_html=True)
st.markdown("---")
# Move upload controls to sidebar
with st.sidebar:
st.header("Upload Image")
uploaded_file = st.file_uploader("Choose an image...", type=['png', 'jpg', 'jpeg'])
if uploaded_file is not None:
# Display the uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image")
if st.button("Extract Text 🔍", type="primary"):
with st.spinner("Processing image..."):
try:
response = ollama.chat(
model='gemma3:12b',
messages=[{
'role': 'user',
'content': """Analyze the text in the provided image. Extract all readable content
and present it in a structured Markdown format that is clear, concise,
and well-organized. Ensure proper formatting (e.g., headings, lists, or
code blocks) as necessary to represent the content effectively.""",
'images': [uploaded_file.getvalue()]
}]
)
st.session_state['ocr_result'] = response.message.content
except Exception as e:
st.error(f"Error processing image: {str(e)}")
# Main content area for results
if 'ocr_result' in st.session_state:
st.markdown(st.session_state['ocr_result'])
else:
st.info("Upload an image and click 'Extract Text' to see the results here.")
# Footer
st.markdown("---")
st.markdown("Made with ❤️ using Gemma-3 Vision Model | [Report an Issue](https://github.com/patchy631/ai-engineering-hub/issues)")