Spaces:
Sleeping
Sleeping
sync with remote
Browse files- app.py +28 -30
- problems/problem1.png +0 -0
- problems/problem2.png +0 -0
- problems/problem3.png +0 -0
- problems/problem4.png +0 -0
- problems/problem5.png +0 -0
app.py
CHANGED
|
@@ -3,7 +3,22 @@ import os
|
|
| 3 |
import google.generativeai as genai
|
| 4 |
import json
|
| 5 |
from PIL import Image
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
MODEL_ID = "gemini-2.0-flash-exp" # Keep the model ID as is
|
| 9 |
try:
|
|
@@ -17,17 +32,17 @@ except Exception as e:
|
|
| 17 |
model = genai.GenerativeModel(MODEL_ID)
|
| 18 |
chat = model.start_chat()
|
| 19 |
|
| 20 |
-
def
|
| 21 |
"""
|
| 22 |
Returns the path to the local PDF file.
|
| 23 |
"""
|
| 24 |
try:
|
| 25 |
-
|
| 26 |
-
if not os.path.exists(
|
| 27 |
-
raise FileNotFoundError(f"{
|
| 28 |
-
return
|
| 29 |
except Exception as e:
|
| 30 |
-
st.error(f"Failed to find the local
|
| 31 |
st.stop() # Stop if the file is not found
|
| 32 |
|
| 33 |
# Initialize conversation history in Streamlit session state
|
|
@@ -36,7 +51,7 @@ if "conversation_history" not in st.session_state:
|
|
| 36 |
if "uploaded_file_part" not in st.session_state: # Store the file *part*
|
| 37 |
st.session_state.uploaded_file_part = None
|
| 38 |
if "uploaded_pdf_path" not in st.session_state:
|
| 39 |
-
st.session_state.uploaded_pdf_path =
|
| 40 |
|
| 41 |
def multimodal_prompt(pdf_path, text_prompt, file_type="PDF"):
|
| 42 |
"""
|
|
@@ -76,7 +91,6 @@ def multimodal_prompt(pdf_path, text_prompt, file_type="PDF"):
|
|
| 76 |
except Exception as e:
|
| 77 |
return f"An error occurred: {e}"
|
| 78 |
|
| 79 |
-
|
| 80 |
# --- Main Page ---
|
| 81 |
st.title("📚❓Problem Solving Tutor")
|
| 82 |
about = """
|
|
@@ -84,31 +98,15 @@ about = """
|
|
| 84 |
Replace this placeholder with the actual text.
|
| 85 |
"""
|
| 86 |
|
| 87 |
-
|
| 88 |
-
import re
|
| 89 |
-
import json
|
| 90 |
-
|
| 91 |
-
# Define constants
|
| 92 |
-
TEXT_PROMPT = """Use the provided document. Read the list of 5 quadratic equations.
|
| 93 |
-
Return your response as a JSON list. Do not include any extra text, explanations, or backslashes.
|
| 94 |
-
|
| 95 |
-
Example JSON output:
|
| 96 |
-
[
|
| 97 |
-
"x^2 - 5x + 6 = 0",
|
| 98 |
-
"2x^2 + 3x - 1 = 0",
|
| 99 |
-
"x^2 - 9 = 0",
|
| 100 |
-
"3x^2 - 2x + 4 = 0",
|
| 101 |
-
"x^2 + 8x + 15 = 0"
|
| 102 |
-
]
|
| 103 |
-
"""
|
| 104 |
-
|
| 105 |
-
# Main code
|
| 106 |
-
with st.spinner("AI is thinking..."):
|
| 107 |
if st.session_state.get("uploaded_pdf_path") is None:
|
| 108 |
-
st.session_state.uploaded_pdf_path =
|
| 109 |
|
| 110 |
filepath = st.session_state.uploaded_pdf_path
|
| 111 |
-
response = multimodal_prompt(filepath, TEXT_PROMPT, file_type="
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# Remove the ```json and ``` and extra spaces.
|
| 114 |
try:
|
|
|
|
| 3 |
import google.generativeai as genai
|
| 4 |
import json
|
| 5 |
from PIL import Image
|
| 6 |
+
import re
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
+
# Define constants
|
| 10 |
+
TEXT_PROMPT = """Use the provided document. Read the list of quadratic equations.
|
| 11 |
+
Return your response as a JSON list. Do not include any extra text, explanations, or backslashes.
|
| 12 |
+
|
| 13 |
+
Example JSON output:
|
| 14 |
+
[
|
| 15 |
+
"x^2 - 5x + 6 = 0",
|
| 16 |
+
"2x^2 + 3x - 1 = 0",
|
| 17 |
+
"x^2 - 9 = 0",
|
| 18 |
+
"3x^2 - 2x + 4 = 0",
|
| 19 |
+
"x^2 + 8x + 15 = 0"
|
| 20 |
+
]
|
| 21 |
+
"""
|
| 22 |
|
| 23 |
MODEL_ID = "gemini-2.0-flash-exp" # Keep the model ID as is
|
| 24 |
try:
|
|
|
|
| 32 |
model = genai.GenerativeModel(MODEL_ID)
|
| 33 |
chat = model.start_chat()
|
| 34 |
|
| 35 |
+
def get_local_file_path(img_file="problem1.png"):
|
| 36 |
"""
|
| 37 |
Returns the path to the local PDF file.
|
| 38 |
"""
|
| 39 |
try:
|
| 40 |
+
file_path = os.path.join("problems", img_file)
|
| 41 |
+
if not os.path.exists(file_path):
|
| 42 |
+
raise FileNotFoundError(f"{file_path} does not exist.")
|
| 43 |
+
return file_path
|
| 44 |
except Exception as e:
|
| 45 |
+
st.error(f"Failed to find the local file: {e}")
|
| 46 |
st.stop() # Stop if the file is not found
|
| 47 |
|
| 48 |
# Initialize conversation history in Streamlit session state
|
|
|
|
| 51 |
if "uploaded_file_part" not in st.session_state: # Store the file *part*
|
| 52 |
st.session_state.uploaded_file_part = None
|
| 53 |
if "uploaded_pdf_path" not in st.session_state:
|
| 54 |
+
st.session_state.uploaded_pdf_path = get_local_file_path()
|
| 55 |
|
| 56 |
def multimodal_prompt(pdf_path, text_prompt, file_type="PDF"):
|
| 57 |
"""
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
return f"An error occurred: {e}"
|
| 93 |
|
|
|
|
| 94 |
# --- Main Page ---
|
| 95 |
st.title("📚❓Problem Solving Tutor")
|
| 96 |
about = """
|
|
|
|
| 98 |
Replace this placeholder with the actual text.
|
| 99 |
"""
|
| 100 |
|
| 101 |
+
with st.spinner("Loading the problem..."):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if st.session_state.get("uploaded_pdf_path") is None:
|
| 103 |
+
st.session_state.uploaded_pdf_path = get_local_file_path("problem1.png")
|
| 104 |
|
| 105 |
filepath = st.session_state.uploaded_pdf_path
|
| 106 |
+
response = multimodal_prompt(filepath, TEXT_PROMPT, file_type="image")
|
| 107 |
+
|
| 108 |
+
# --- Display the image ---
|
| 109 |
+
st.image(filepath, caption="Problem Image", use_container_width=True)
|
| 110 |
|
| 111 |
# Remove the ```json and ``` and extra spaces.
|
| 112 |
try:
|
problems/problem1.png
ADDED
|
problems/problem2.png
ADDED
|
problems/problem3.png
ADDED
|
problems/problem4.png
ADDED
|
problems/problem5.png
ADDED
|