Spaces:
Sleeping
Sleeping
File size: 16,077 Bytes
3c02b94 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
import streamlit as st
from PIL import Image
import os
from dotenv import load_dotenv
# Compatibility fix for Pillow 10.0.0+ where ANTIALIAS was removed
if not hasattr(Image, 'ANTIALIAS'):
Image.ANTIALIAS = Image.LANCZOS
from utils.ocr import OCRProcessor
from utils.audio import AudioProcessor
from utils.memory import MemorySystem
from utils.hitl import HITLSystem
from agents.parser import ParserAgent
from agents.router import RouterAgent
from agents.solver import SolverAgent
from agents.verifier import VerifierAgent
from agents.explainer import ExplainerAgent
from rag.retriever import Retriever
load_dotenv()
st.set_page_config(page_title="Math Mentor", page_icon="๐", layout="wide")
if 'memory' not in st.session_state:
st.session_state.memory = MemorySystem()
if 'hitl' not in st.session_state:
st.session_state.hitl = HITLSystem()
if 'ocr' not in st.session_state:
st.session_state.ocr = OCRProcessor()
if 'audio' not in st.session_state:
st.session_state.audio = AudioProcessor()
if 'parser' not in st.session_state:
st.session_state.parser = ParserAgent()
if 'router' not in st.session_state:
st.session_state.router = RouterAgent()
if 'solver' not in st.session_state:
st.session_state.solver = SolverAgent()
if 'verifier' not in st.session_state:
st.session_state.verifier = VerifierAgent()
if 'explainer' not in st.session_state:
st.session_state.explainer = ExplainerAgent()
if 'retriever' not in st.session_state:
st.session_state.retriever = Retriever()
st.title("๐ Math Mentor - AI Problem Solver")
st.markdown("Upload an image, record audio, or type your math problem")
col1, col2 = st.columns([2, 1])
with col1:
input_mode = st.radio("Input Mode", ["Text", "Image", "Audio"], horizontal=True)
extracted_text = ""
needs_review = False
confidence = 1.0
ocr_confidence = 1.0
audio_confidence = 1.0
if input_mode == "Text":
extracted_text = st.text_area("Extracted Text (edit if needed):", value=extracted_text, height=150, key="edited_text")
elif input_mode == "Image":
uploaded_file = st.file_uploader("Upload image", type=['png', 'jpg', 'jpeg'])
if uploaded_file:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
with st.spinner("Extracting text from image..."):
result = st.session_state.ocr.extract_text(image)
extracted_text = result['text']
ocr_confidence = result['confidence']
needs_review = result['needs_review']
col_conf1, col_conf2 = st.columns(2)
with col_conf1:
st.metric("OCR Confidence", f"{ocr_confidence:.2%}")
with col_conf2:
if needs_review:
st.error("โ ๏ธ Low Confidence")
else:
st.success("โ
High Confidence")
extracted_text = st.text_area("Extracted Text (edit if needed):", value=extracted_text, height=150)
elif input_mode == "Audio":
audio_file = st.file_uploader("Upload audio file", type=['wav', 'mp3', 'm4a'])
if audio_file:
st.audio(audio_file)
with st.spinner("Transcribing audio..."):
result = st.session_state.audio.transcribe(audio_file)
extracted_text = result['text']
audio_confidence = result['confidence']
needs_review = result['needs_review']
col_conf1, col_conf2 = st.columns(2)
with col_conf1:
st.metric("Transcription Confidence", f"{audio_confidence:.2%}")
with col_conf2:
if needs_review:
st.error("โ ๏ธ Low Confidence")
else:
st.success("โ
High Confidence")
extracted_text = st.text_area("Transcription (edit if needed):", value=extracted_text, height=150)
col_btn1, col_btn2 = st.columns(2)
with col_btn1:
solve_button = st.button("๐ Solve Problem", type="primary", disabled=not extracted_text, use_container_width=True)
with col_btn2:
recheck_button = st.button("๐ Request Re-check", disabled=not extracted_text, use_container_width=True)
with col2:
st.subheader("๐ Agent Trace")
trace_container = st.container()
if solve_button and extracted_text:
final_text = st.session_state.get("edited_text", extracted_text)
# Then use final_text for parsing
parsed = st.session_state.parser.parse(final_text, input_mode.lower())
extracted_text = st.session_state.memory.apply_learned_corrections(extracted_text, input_mode.lower())
with trace_container:
trace = []
st.write("๐ **Parser Agent**: Analyzing problem...")
parsed = st.session_state.parser.parse(extracted_text, input_mode.lower())
trace.append({"agent": "Parser", "output": parsed})
with st.expander("Parser Output", expanded=False):
st.json(parsed)
if input_mode == "Image" and extracted_text != result['text']:
ocr_confidence = 1.0
if input_mode == "Audio" and extracted_text != result['text']:
audio_confidence = 1.0
hitl_check = st.session_state.hitl.should_trigger_hitl(
ocr_confidence=ocr_confidence,
audio_confidence=audio_confidence,
parser_needs_clarification=parsed.get('needs_clarification', False),
explicit_request=False
)
if hitl_check['should_trigger']:
st.error(st.session_state.hitl.get_hitl_instructions(hitl_check))
st.session_state.hitl_triggered = True
st.stop()
st.write("๐งญ **Router Agent**: Determining strategy...")
routing = st.session_state.router.route(parsed)
trace.append({"agent": "Router", "output": routing})
with st.expander("Router Output", expanded=False):
st.json(routing)
if routing.get('requires_hitl'):
st.error(f"โ HITL Required: {routing.get('reason')}")
st.info("Please clarify your problem or edit the extracted text.")
st.stop()
st.write("๐ **Retriever**: Fetching relevant context...")
context = st.session_state.retriever.retrieve_context(parsed)
trace.append({"agent": "Retriever", "sources": len(context['knowledge_base'])})
st.write(f"๐ Retrieved {len(context['knowledge_base'])} knowledge chunks + {len(context['similar_problems'])} similar problems")
st.write("๐ก **Solver Agent**: Solving problem...")
solution = st.session_state.solver.solve(parsed, context, routing['strategy'])
trace.append({"agent": "Solver", "steps": len(solution['steps'])})
if solution.get('calculations_performed', 0) > 0:
st.write(f"๐งฎ Performed {solution['calculations_performed']} calculations")
st.write("โ
**Verifier Agent**: Checking solution...")
verification = st.session_state.verifier.verify(parsed, solution)
trace.append({"agent": "Verifier", "output": verification})
verifier_hitl = st.session_state.hitl.should_trigger_hitl(
verifier_confidence=verification.get('confidence', 1.0)
)
if verifier_hitl['should_trigger']:
st.warning("โ ๏ธ Verifier has concerns. Solution generated but needs review.")
with st.expander("Verifier Output", expanded=False):
st.json(verification)
st.write("๐ **Explainer Agent**: Creating explanation...")
explanation = st.session_state.explainer.explain(parsed, solution, verification)
st.session_state.current_solution = {
'input_mode': input_mode,
'original_text': extracted_text,
'parsed': parsed,
'routing': routing,
'solution': solution,
'verification': verification,
'explanation': explanation,
'context': context,
'trace': trace,
'hitl_data': verifier_hitl
}
if recheck_button and extracted_text:
hitl_explicit = st.session_state.hitl.should_trigger_hitl(explicit_request=True)
st.warning(st.session_state.hitl.get_hitl_instructions(hitl_explicit))
if 'current_solution' in st.session_state:
st.markdown("---")
tab1, tab2, tab3, tab4 = st.tabs(["๐ Explanation", "๐ Retrieved Context", "๐ Solution Details", "๐ Learning Insights"])
with tab1:
conf = st.session_state.current_solution['verification']['confidence']
col_metric1, col_metric2, col_metric3 = st.columns(3)
with col_metric1:
st.metric("Solution Confidence", f"{conf:.2%}")
with col_metric2:
is_correct = st.session_state.current_solution['verification']['is_correct']
st.metric("Verified", "โ
Yes" if is_correct else "โ ๏ธ Review Needed")
with col_metric3:
calc_count = st.session_state.current_solution['solution'].get('calculations_performed', 0)
st.metric("Calculations", calc_count)
if conf < 0.7:
st.warning("โ ๏ธ Low confidence solution. Please verify carefully.")
st.markdown("### Step-by-Step Explanation")
st.markdown(st.session_state.current_solution['explanation']['explanation'])
with tab2:
st.subheader("๐ Knowledge Base Sources")
for i, item in enumerate(st.session_state.current_solution['context']['knowledge_base'], 1):
with st.expander(f"Source {i}: {item['metadata']['topic']}"):
st.write(item['content'])
if st.session_state.current_solution['context']['similar_problems']:
st.subheader("๐ Similar Problems from Memory (Self-Learning)")
for i, prob in enumerate(st.session_state.current_solution['context']['similar_problems'], 1):
with st.expander(f"Similar Problem {i} (Similarity: {prob.get('similarity', 0):.2%})"):
st.write("**Problem:**", prob.get('parsed_question', {}).get('problem_text', ''))
if prob.get('user_feedback') == 'correct':
st.success("โ
This was a correct solution")
if 'solution' in prob:
st.write("**Previous Solution:**")
st.write(prob['solution'][:300] + "...")
with tab3:
st.subheader("๐ Full Solution")
st.write(st.session_state.current_solution['solution']['solution'])
st.subheader("๐ Verification Results")
ver = st.session_state.current_solution['verification']
st.json(ver)
if ver.get('issues'):
st.subheader("โ ๏ธ Issues Found")
for issue in ver['issues']:
st.warning(issue)
with tab4:
insights = st.session_state.memory.get_learning_insights()
col_i1, col_i2, col_i3 = st.columns(3)
with col_i1:
st.metric("Total Problems Solved", insights['total_problems'])
with col_i2:
st.metric("Overall Accuracy", f"{insights['accuracy']:.1f}%")
with col_i3:
st.metric("Best Strategy", insights['most_successful_strategy'] or "N/A")
if insights['topics_distribution']:
st.subheader("๐ Topics Distribution")
st.bar_chart(insights['topics_distribution'])
if insights['common_error_topics']:
st.subheader("โ ๏ธ Topics Needing Improvement")
for topic in insights['common_error_topics']:
st.write(f"- {topic}")
st.markdown("---")
st.subheader("๐ Provide Feedback (Helps System Learn)")
col_fb1, col_fb2, col_fb3 = st.columns(3)
with col_fb1:
if st.button("โ
Correct Solution", use_container_width=True):
st.session_state.memory.store({
'input_type': st.session_state.current_solution['input_mode'],
'original_text': st.session_state.current_solution['original_text'],
'parsed_question': st.session_state.current_solution['parsed'],
'routing': st.session_state.current_solution['routing'],
'solution': st.session_state.current_solution['solution']['solution'],
'verification': st.session_state.current_solution['verification'],
'user_feedback': 'correct',
'context_used': st.session_state.current_solution['context']
})
st.success("โ
Feedback saved! This solution will help improve future responses.")
st.balloons()
with col_fb2:
if st.button("โ Incorrect Solution", use_container_width=True):
st.session_state.show_feedback_form = True
with col_fb3:
if st.button("๐ Try Again", use_container_width=True):
del st.session_state.current_solution
st.rerun()
if st.session_state.get('show_feedback_form'):
st.markdown("---")
feedback_comment = st.text_area("What was wrong? Your feedback helps the system learn:", placeholder="e.g., Wrong formula used, calculation error, missed a constraint...")
col_submit, col_cancel = st.columns(2)
with col_submit:
if st.button("Submit Feedback", type="primary", use_container_width=True):
st.session_state.memory.store({
'input_type': st.session_state.current_solution['input_mode'],
'original_text': st.session_state.current_solution['original_text'],
'parsed_question': st.session_state.current_solution['parsed'],
'routing': st.session_state.current_solution['routing'],
'solution': st.session_state.current_solution['solution']['solution'],
'verification': st.session_state.current_solution['verification'],
'user_feedback': 'incorrect',
'user_comment': feedback_comment,
'context_used': st.session_state.current_solution['context']
})
st.success("โ
Thank you! This feedback will help the system learn and improve.")
st.session_state.show_feedback_form = False
st.rerun()
with col_cancel:
if st.button("Cancel", use_container_width=True):
st.session_state.show_feedback_form = False
st.rerun()
st.sidebar.title("๐ System Statistics")
insights = st.session_state.memory.get_learning_insights()
st.sidebar.metric("Problems Solved", insights['total_problems'])
if insights['total_problems'] > 0:
st.sidebar.metric("Success Rate", f"{insights['accuracy']:.1f}%")
if insights['most_successful_strategy']:
st.sidebar.metric("Best Strategy", insights['most_successful_strategy'])
st.sidebar.markdown("### ๐ Topics Learned")
for topic, count in insights['topics_distribution'].items():
st.sidebar.write(f"- {topic}: {count}")
st.sidebar.markdown("---")
st.sidebar.info("""
**How This System Learns:**
- Stores all solved problems
- Retrieves similar past solutions
- Learns from your feedback
- Improves OCR/audio corrections
- Identifies successful strategies
""") |