Musharraf11 commited on
Commit
0a0f0fc
·
verified ·
1 Parent(s): e7dbf4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -49
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
- # Upload image
14
- uploaded_file = st.file_uploader("Upload an image of handwritten text", type=["jpg", "jpeg", "png", "webp"])
 
 
 
 
 
 
 
15
 
16
- if uploaded_file:
17
- image = Image.open(uploaded_file)
18
- st.image(image, caption="Uploaded Image", use_column_width=True)
19
 
20
- if st.button("Extract & Refine Text"):
21
- with st.spinner("Extracting text..."):
22
- try:
23
- # Initial Extraction
24
- prompt_initial = [
25
- image,
26
- "You are an expert in extracting handwritten text from student answer scripts. Your task is to:\n\n"
27
- "1. Accurately extract the handwritten text exactly as it is.\n"
28
- "2. Carefully cross-check and correct only recognition errors (e.g., OCR mistakes).\n"
29
- "3. Do not modify or clean up grammatical or factual errors.\n"
30
- "4. If the student's script contains tables, diagrams, figures, or flowcharts, you must:\n"
31
- "- Reproduce tables in plain text or markdown table format, ensuring blank cells remain blank.\n"
32
- "- Recreate flowcharts using markdown-friendly formats (e.g., Mermaid).\n"
33
- "- For diagrams, recreate using ASCII art if possible.\n\n"
34
- "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"
35
- "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"
36
- "Additionally, if there are any strikethrough texts, either preserve them with formatting (e.g., ~~strikethrough~~) or ignore them from the extraction."
37
- ]
38
 
39
- response_initial = model.generate_content(prompt_initial)
40
- extracted_text = response_initial.text
41
 
42
- # Refinement Pass
43
- prompt_refine = [
44
- image,
45
- f"Here is the initially extracted text:\n```\n{extracted_text}\n```\n"
46
- "Now, carefully correct any OCR errors **without modifying spelling mistakes, grammar, or factual content**:\n"
47
- "- Maintain markdown tables correctly aligned, ensuring blank cells remain blank.\n"
48
- "- Fix incorrect characters or missing words but do not autofill empty spaces.\n"
49
- "- Ensure strikethrough text is either preserved as `~~text~~` or removed based on the original formatting."
50
- ]
51
 
52
- response_refine = model.generate_content(prompt_refine)
53
- refined_text = response_refine.text
54
 
55
- # Display results
56
- st.subheader("✅ Final Extracted Text:")
57
- st.markdown(refined_text, unsafe_allow_html=True)
58
- st.code(refined_text, language="text")
59
 
60
- # Allow Download
61
- st.download_button("⬇️ Download Markdown", refined_text, file_name="extracted_text.md", mime="text/markdown")
62
 
63
- except Exception as e:
64
- st.error(f"Error: {e}")
 
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}")