Spaces:
Running
Running
Upload 3 files
Browse files- app.py +60 -0
- assets/gemma3.png +0 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import ollama
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
import base64
|
6 |
+
# Page configuration
|
7 |
+
st.set_page_config(
|
8 |
+
page_title="Gemma-3 OCR",
|
9 |
+
page_icon="π",
|
10 |
+
layout="wide",
|
11 |
+
initial_sidebar_state="expanded"
|
12 |
+
)
|
13 |
+
# Title and description in main area
|
14 |
+
st.markdown("""
|
15 |
+
# <img src="data:image/png;base64,{}" width="50" style="vertical-align: -12px;"> Gemma-3 OCR
|
16 |
+
""".format(base64.b64encode(open("./assets/gemma3.png", "rb").read()).decode()), unsafe_allow_html=True)
|
17 |
+
# Add clear button to top right
|
18 |
+
col1, col2 = st.columns([6,1])
|
19 |
+
with col2:
|
20 |
+
if st.button("Clear ποΈ"):
|
21 |
+
if 'ocr_result' in st.session_state:
|
22 |
+
del st.session_state['ocr_result']
|
23 |
+
st.rerun()
|
24 |
+
st.markdown('<p style="margin-top: -20px;">Extract structured text from images using Gemma-3 Vision!</p>', unsafe_allow_html=True)
|
25 |
+
st.markdown("---")
|
26 |
+
# Move upload controls to sidebar
|
27 |
+
with st.sidebar:
|
28 |
+
st.header("Upload Image")
|
29 |
+
uploaded_file = st.file_uploader("Choose an image...", type=['png', 'jpg', 'jpeg'])
|
30 |
+
|
31 |
+
if uploaded_file is not None:
|
32 |
+
# Display the uploaded image
|
33 |
+
image = Image.open(uploaded_file)
|
34 |
+
st.image(image, caption="Uploaded Image")
|
35 |
+
|
36 |
+
if st.button("Extract Text π", type="primary"):
|
37 |
+
with st.spinner("Processing image..."):
|
38 |
+
try:
|
39 |
+
response = ollama.chat(
|
40 |
+
model='gemma3:12b',
|
41 |
+
messages=[{
|
42 |
+
'role': 'user',
|
43 |
+
'content': """Analyze the text in the provided image. Extract all readable content
|
44 |
+
and present it in a structured Markdown format that is clear, concise,
|
45 |
+
and well-organized. Ensure proper formatting (e.g., headings, lists, or
|
46 |
+
code blocks) as necessary to represent the content effectively.""",
|
47 |
+
'images': [uploaded_file.getvalue()]
|
48 |
+
}]
|
49 |
+
)
|
50 |
+
st.session_state['ocr_result'] = response.message.content
|
51 |
+
except Exception as e:
|
52 |
+
st.error(f"Error processing image: {str(e)}")
|
53 |
+
# Main content area for results
|
54 |
+
if 'ocr_result' in st.session_state:
|
55 |
+
st.markdown(st.session_state['ocr_result'])
|
56 |
+
else:
|
57 |
+
st.info("Upload an image and click 'Extract Text' to see the results here.")
|
58 |
+
# Footer
|
59 |
+
st.markdown("---")
|
60 |
+
st.markdown("Made with β€οΈ using Gemma-3 Vision Model ")
|
assets/gemma3.png
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
ollama
|
3 |
+
pillow
|