Spaces:
Sleeping
Sleeping
save
Browse files
app.py
CHANGED
@@ -73,23 +73,13 @@ about = """
|
|
73 |
Replace this placeholder with the actual text.
|
74 |
"""
|
75 |
|
76 |
-
with st.expander("How to use this App"):
|
77 |
-
st.markdown(about)
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
# st.image(image, width=400)
|
82 |
-
|
83 |
-
st.header("Quadratic Equations")
|
84 |
-
#load the problems form the pdf
|
85 |
-
with st.spinner("AI is thinking..."):
|
86 |
-
if st.session_state.uploaded_pdf_path is None:
|
87 |
-
st.session_state.uploaded_pdf_path = get_local_pdf_path()
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
Return your response
|
92 |
-
Do not include any extra text, explanations, or backslashes.
|
93 |
|
94 |
Example JSON output:
|
95 |
[
|
@@ -100,22 +90,63 @@ Example JSON output:
|
|
100 |
"x^2 + 8x + 15 = 0"
|
101 |
]
|
102 |
"""
|
103 |
-
response = multimodal_prompt(filepath, text_prompt) # Use the local filepath
|
104 |
-
st.markdown(response)
|
105 |
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
equations = []
|
109 |
for problem in problems:
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
for equation in equations:
|
114 |
st.write(equation)
|
115 |
-
|
116 |
-
st.
|
|
|
117 |
except Exception as e:
|
118 |
-
st.
|
|
|
119 |
|
120 |
st.markdown("Visit our Hugging Face Space!")
|
121 |
st.markdown("© 2025 WVSU AI Dev Team 🤖 ✨")
|
|
|
73 |
Replace this placeholder with the actual text.
|
74 |
"""
|
75 |
|
|
|
|
|
76 |
|
77 |
+
import re
|
78 |
+
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
# Define constants
|
81 |
+
TEXT_PROMPT = """Use the provided document. Read the list of 5 quadratic equations.
|
82 |
+
Return your response as a JSON list. Do not include any extra text, explanations, or backslashes.
|
|
|
83 |
|
84 |
Example JSON output:
|
85 |
[
|
|
|
90 |
"x^2 + 8x + 15 = 0"
|
91 |
]
|
92 |
"""
|
|
|
|
|
93 |
|
94 |
+
# Define a function to extract equations from the AI response
|
95 |
+
def extract_equations(response):
|
96 |
+
try:
|
97 |
+
if isinstance(response, str):
|
98 |
+
response = response.strip().replace("\n", "").replace("\r", "")
|
99 |
+
if response.lower().startswith("json"):
|
100 |
+
response = response[4:].strip()
|
101 |
+
if response.startswith("[") and response.endswith("]"):
|
102 |
+
return json.loads(response)
|
103 |
+
else:
|
104 |
+
st.error("Error: AI response is not in expected JSON list format.")
|
105 |
+
return []
|
106 |
+
elif isinstance(response, list):
|
107 |
+
return response
|
108 |
+
else:
|
109 |
+
st.error("Error: Unexpected response format from AI.")
|
110 |
+
return []
|
111 |
+
except json.JSONDecodeError:
|
112 |
+
st.error("Error: Failed to parse AI response as a list.")
|
113 |
+
return []
|
114 |
+
|
115 |
+
# Define a function to extract quadratic equations from the problems
|
116 |
+
def extract_quadratic_equations(problems):
|
117 |
equations = []
|
118 |
for problem in problems:
|
119 |
+
match = re.search(r'([0-9x\^\+\-\=\s]+)', problem)
|
120 |
+
if match:
|
121 |
+
equations.append(match.group(1).strip())
|
122 |
+
else:
|
123 |
+
st.warning(f"Could not extract equation from: '{problem}'")
|
124 |
+
return equations
|
125 |
+
|
126 |
+
# Main code
|
127 |
+
with st.spinner("AI is thinking..."):
|
128 |
+
if st.session_state.get("uploaded_pdf_path") is None:
|
129 |
+
st.session_state.uploaded_pdf_path = get_local_pdf_path()
|
130 |
+
|
131 |
+
filepath = st.session_state.uploaded_pdf_path
|
132 |
+
response = multimodal_prompt(filepath, TEXT_PROMPT)
|
133 |
+
|
134 |
+
# Debugging: Print response
|
135 |
+
st.write("Raw AI Response:", response)
|
136 |
+
|
137 |
+
# Extract equations
|
138 |
+
problems = extract_equations(response)
|
139 |
+
if problems:
|
140 |
+
equations = extract_quadratic_equations(problems)
|
141 |
+
st.write("Extracted Equations:")
|
142 |
for equation in equations:
|
143 |
st.write(equation)
|
144 |
+
else:
|
145 |
+
st.error("Error: No valid equations extracted.")
|
146 |
+
|
147 |
except Exception as e:
|
148 |
+
st.error(f"An unexpected error occurred: {e}")
|
149 |
+
|
150 |
|
151 |
st.markdown("Visit our Hugging Face Space!")
|
152 |
st.markdown("© 2025 WVSU AI Dev Team 🤖 ✨")
|