Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,63 +2,65 @@ import streamlit as st
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
# Configure Gemini API
|
| 6 |
-
GEMINI_API_KEY = "AIzaSyBw9qlfSD4LBiZpAQEf0jaUCd70Z_fnkB8"
|
| 7 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 8 |
-
model = genai.GenerativeModel('gemini-2.5-pro-exp-03-25')
|
| 9 |
-
|
| 10 |
# Streamlit UI
|
| 11 |
st.title("📝 OCR with Gemini (Accurate Extraction & Refinement)")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
if uploaded_file:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
from PIL import Image
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Streamlit UI
|
| 6 |
st.title("📝 OCR with Gemini (Accurate Extraction & Refinement)")
|
| 7 |
|
| 8 |
+
# API Key Input
|
| 9 |
+
GEMINI_API_KEY = st.text_input("Enter your Gemini API Key:", type="password")
|
| 10 |
+
|
| 11 |
+
if GEMINI_API_KEY:
|
| 12 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 13 |
+
model = genai.GenerativeModel('gemini-2.5-pro-exp-03-25')
|
| 14 |
+
|
| 15 |
+
# Upload image
|
| 16 |
+
uploaded_file = st.file_uploader("Upload an image of handwritten text", type=["jpg", "jpeg", "png", "webp"])
|
| 17 |
|
| 18 |
+
if uploaded_file:
|
| 19 |
+
image = Image.open(uploaded_file)
|
| 20 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 21 |
|
| 22 |
+
if st.button("Extract & Refine Text"):
|
| 23 |
+
with st.spinner("Extracting text..."):
|
| 24 |
+
try:
|
| 25 |
+
# Initial Extraction
|
| 26 |
+
prompt_initial = [
|
| 27 |
+
image,
|
| 28 |
+
"You are an expert in extracting handwritten text from student answer scripts. Your task is to:\n\n"
|
| 29 |
+
"1. Accurately extract the handwritten text exactly as it is.\n"
|
| 30 |
+
"2. Carefully cross-check and correct only recognition errors (e.g., OCR mistakes).\n"
|
| 31 |
+
"3. Do not modify or clean up grammatical or factual errors.\n"
|
| 32 |
+
"4. If the student's script contains tables, diagrams, figures, or flowcharts, you must:\n"
|
| 33 |
+
"- Reproduce tables in plain text or markdown table format, ensuring blank cells remain blank.\n"
|
| 34 |
+
"- Recreate flowcharts using markdown-friendly formats (e.g., Mermaid).\n"
|
| 35 |
+
"- For diagrams, recreate using ASCII art if possible.\n\n"
|
| 36 |
+
"If the student's script contains any graphs (e.g., line graphs, bar charts, scatter plots with X and Y axes), recreate them using markdown-supported formats such as Mermaid.js or ASCII plots. Ensure the axes, labels, and plotted data points are captured accurately. If needed, describe the graph layout and its key data in words.\n\n"
|
| 37 |
+
"Your role is to capture the student's original input precisely, as this will be evaluated by a human. Maintain full fidelity to the original script.\n\n"
|
| 38 |
+
"Additionally, if there are any strikethrough texts, either preserve them with formatting (e.g., ~~strikethrough~~) or ignore them from the extraction."
|
| 39 |
+
]
|
| 40 |
|
| 41 |
+
response_initial = model.generate_content(prompt_initial)
|
| 42 |
+
extracted_text = response_initial.text
|
| 43 |
|
| 44 |
+
# Refinement Pass
|
| 45 |
+
prompt_refine = [
|
| 46 |
+
image,
|
| 47 |
+
f"Here is the initially extracted text:\n```\n{extracted_text}\n```\n"
|
| 48 |
+
"Now, carefully correct any OCR errors **without modifying spelling mistakes, grammar, or factual content**:\n"
|
| 49 |
+
"- Maintain markdown tables correctly aligned, ensuring blank cells remain blank.\n"
|
| 50 |
+
"- Fix incorrect characters or missing words but do not autofill empty spaces.\n"
|
| 51 |
+
"- Ensure strikethrough text is either preserved as `~~text~~` or removed based on the original formatting."
|
| 52 |
+
]
|
| 53 |
|
| 54 |
+
response_refine = model.generate_content(prompt_refine)
|
| 55 |
+
refined_text = response_refine.text
|
| 56 |
|
| 57 |
+
# Display results
|
| 58 |
+
st.subheader("✅ Final Extracted Text:")
|
| 59 |
+
st.markdown(refined_text, unsafe_allow_html=True)
|
| 60 |
+
st.code(refined_text, language="text")
|
| 61 |
|
| 62 |
+
# Allow Download
|
| 63 |
+
st.download_button("⬇️ Download Markdown", refined_text, file_name="extracted_text.md", mime="text/markdown")
|
| 64 |
|
| 65 |
+
except Exception as e:
|
| 66 |
+
st.error(f"Error: {e}")
|