Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,47 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import google.generativeai as genai
|
3 |
import os
|
4 |
-
from PIL import Image
|
5 |
|
6 |
-
#
|
7 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
8 |
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Streamlit UI
|
13 |
-
st.set_page_config(page_title="Handwritten
|
14 |
-
st.title("π Handwritten
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
if
|
19 |
-
|
20 |
-
st.image(
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
)
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
3 |
+
import pytesseract
|
4 |
import google.generativeai as genai
|
5 |
import os
|
|
|
6 |
|
7 |
+
# Set your Gemini API key
|
8 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
9 |
|
10 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
11 |
+
|
12 |
+
def preprocess_image(image):
|
13 |
+
image = image.convert("L") # grayscale
|
14 |
+
image = image.filter(ImageFilter.MedianFilter())
|
15 |
+
enhancer = ImageEnhance.Contrast(image)
|
16 |
+
image = enhancer.enhance(2)
|
17 |
+
return image
|
18 |
+
|
19 |
+
def extract_text(image):
|
20 |
+
return pytesseract.image_to_string(image)
|
21 |
+
|
22 |
+
def summarize_text(text):
|
23 |
+
prompt = f"Summarize the following handwritten note:\n\n{text}"
|
24 |
+
response = model.generate_content(prompt)
|
25 |
+
return response.text
|
26 |
|
27 |
# Streamlit UI
|
28 |
+
st.set_page_config(page_title="Handwritten Note Summarizer", layout="centered")
|
29 |
+
st.title("π Handwritten Note Summarizer")
|
30 |
+
|
31 |
+
uploaded_file = st.file_uploader("Upload an image of handwritten notes", type=["jpg", "jpeg", "png"])
|
32 |
+
|
33 |
+
if uploaded_file:
|
34 |
+
image = Image.open(uploaded_file)
|
35 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
36 |
+
|
37 |
+
if st.button("Extract & Summarize"):
|
38 |
+
with st.spinner("Processing..."):
|
39 |
+
preprocessed = preprocess_image(image)
|
40 |
+
extracted_text = extract_text(preprocessed)
|
41 |
+
summary = summarize_text(extracted_text)
|
42 |
+
|
43 |
+
st.subheader("π Extracted Text")
|
44 |
+
st.text(extracted_text.strip() or "Could not extract text.")
|
45 |
+
|
46 |
+
st.subheader("π§ Summary")
|
47 |
+
st.markdown(summary.strip() or "Summary not available.")
|