Update app.py
Browse files
app.py
CHANGED
|
@@ -482,134 +482,8 @@ with st.sidebar:
|
|
| 482 |
st.caption("Made with ❤️ for H2 Physics students | Powered by Groq AI")
|
| 483 |
|
| 484 |
|
| 485 |
-
#
|
| 486 |
-
if "messages" not in st.session_state:
|
| 487 |
-
st.session_state.messages = [
|
| 488 |
-
{"role": "system", "content": SYSTEM_INSTRUCTIONS},
|
| 489 |
-
{"role": "assistant", "content": "**Hello JPJC Physics students! I'm Richard Feynman, ready to help you master H2 Physics!** ⚛️\n\nI can:\n- 📊 **Plot graphs** using Python (ask me to plot any physics equation!)\n- 🖼️ **Find diagrams** through web search \n- 💬 **Explain concepts** with analogies\n- ❓ **Ask questions** to test your understanding\n\n**What would you like to learn today?**"}
|
| 490 |
-
]
|
| 491 |
-
|
| 492 |
-
# Title and mode indicator
|
| 493 |
-
st.title("⚛️ H2 Physics Feynman Tutor")
|
| 494 |
-
st.caption(f"**Topic:** {topic} | **Mode:** {'Text + Image' if visual_content else 'Text'} | **Powered by Groq AI**")
|
| 495 |
-
|
| 496 |
-
# Display chat history
|
| 497 |
-
for message in st.session_state.messages:
|
| 498 |
-
if message["role"] != "system": # Don't show system messages
|
| 499 |
-
display_message(message["role"], message["content"], enable_voice)
|
| 500 |
-
|
| 501 |
-
# Chat input
|
| 502 |
-
user_input = st.chat_input("Type your physics question here...")
|
| 503 |
-
|
| 504 |
-
# Process user input
|
| 505 |
-
if user_input or visual_content:
|
| 506 |
-
# Prepare user message
|
| 507 |
-
user_message = ""
|
| 508 |
-
if user_input:
|
| 509 |
-
user_message += user_input + " "
|
| 510 |
-
if visual_content:
|
| 511 |
-
user_message += "[I've uploaded an image related to physics. Please analyze it.] "
|
| 512 |
-
if topic != "General / Any":
|
| 513 |
-
user_message += f"(Focus on: {topic})"
|
| 514 |
-
|
| 515 |
-
# Add user message to history
|
| 516 |
-
st.session_state.messages.append({"role": "user", "content": user_message})
|
| 517 |
-
|
| 518 |
-
# Display user message
|
| 519 |
-
with st.chat_message("user"):
|
| 520 |
-
if user_input:
|
| 521 |
-
st.markdown(user_input)
|
| 522 |
-
if visual_content:
|
| 523 |
-
st.image(visual_content, caption="Uploaded Image", use_column_width=True)
|
| 524 |
-
st.caption("📸 Image attached for analysis")
|
| 525 |
-
|
| 526 |
-
# Check for API key
|
| 527 |
-
if not api_key:
|
| 528 |
-
st.error(
|
| 529 |
-
"**❌ Groq API Key Required**\n\n"
|
| 530 |
-
"Please enter your Groq API Key in the sidebar to continue.\n\n"
|
| 531 |
-
"**How to get one (FREE):**\n"
|
| 532 |
-
"1. Go to [console.groq.com](https://console.groq.com)\n"
|
| 533 |
-
"2. Sign up (free, no credit card needed)\n"
|
| 534 |
-
"3. Navigate to API Keys\n"
|
| 535 |
-
"4. Create a new key\n"
|
| 536 |
-
"5. Copy and paste it in the sidebar\n\n"
|
| 537 |
-
"**Free Tier:** 10,000 requests/month\n"
|
| 538 |
-
"**Perfect for testing!** ⚡"
|
| 539 |
-
)
|
| 540 |
-
st.stop()
|
| 541 |
-
|
| 542 |
-
# Prepare API call
|
| 543 |
-
try:
|
| 544 |
-
# Build conversation context
|
| 545 |
-
conversation_context = []
|
| 546 |
-
|
| 547 |
-
# Add system message
|
| 548 |
-
conversation_context.append({"role": "system", "content": SYSTEM_INSTRUCTIONS})
|
| 549 |
-
|
| 550 |
-
# Add recent conversation (last 8 messages for context)
|
| 551 |
-
recent_messages = st.session_state.messages[-8:]
|
| 552 |
-
for msg in recent_messages:
|
| 553 |
-
if msg["role"] != "system": # Don't duplicate system message
|
| 554 |
-
conversation_context.append(msg)
|
| 555 |
-
|
| 556 |
-
# Call Groq API
|
| 557 |
-
with st.spinner("Feynman is thinking... ⚛️"):
|
| 558 |
-
response_text = call_groq_api(api_key, conversation_context)
|
| 559 |
-
|
| 560 |
-
# Handle response
|
| 561 |
-
if response_text:
|
| 562 |
-
# Check for error messages
|
| 563 |
-
if response_text.startswith("❌") or response_text.startswith("⚠️"):
|
| 564 |
-
st.error(response_text)
|
| 565 |
-
if "Invalid API Key" in response_text:
|
| 566 |
-
st.info(
|
| 567 |
-
"**Fix Invalid API Key:**\n"
|
| 568 |
-
"1. Go to [console.groq.com](https://console.groq.com)\n"
|
| 569 |
-
"2. Check your API keys\n"
|
| 570 |
-
"3. Make sure you're using the correct key\n"
|
| 571 |
-
"4. Keys start with: `gsk_`"
|
| 572 |
-
)
|
| 573 |
-
else:
|
| 574 |
-
# Add assistant response to history
|
| 575 |
-
st.session_state.messages.append({"role": "assistant", "content": response_text})
|
| 576 |
-
|
| 577 |
-
# Display response
|
| 578 |
-
display_message("assistant", response_text, enable_voice)
|
| 579 |
-
|
| 580 |
-
# Show token estimate
|
| 581 |
-
estimated_tokens = len(response_text) // 4
|
| 582 |
-
st.caption(f"📝 Response length: ~{estimated_tokens} tokens | ⚡ Powered by Groq")
|
| 583 |
-
else:
|
| 584 |
-
st.error(
|
| 585 |
-
"**Failed to get response from Groq API.**\n\n"
|
| 586 |
-
"Possible reasons:\n"
|
| 587 |
-
"1. **Rate Limit** - Free tier has limits, wait a moment\n"
|
| 588 |
-
"2. **Service Down** - Groq might be experiencing issues\n"
|
| 589 |
-
"3. **Network Issue** - Check your connection\n\n"
|
| 590 |
-
"Try again in a moment."
|
| 591 |
-
)
|
| 592 |
-
|
| 593 |
-
except Exception as e:
|
| 594 |
-
st.error(
|
| 595 |
-
f"**❌ Unexpected Error**\n\n"
|
| 596 |
-
f"```python\n{str(e)[:200]}\n```\n\n"
|
| 597 |
-
f"**Troubleshooting:**\n"
|
| 598 |
-
f"1. Refresh the page\n"
|
| 599 |
-
f"2. Check your API key\n"
|
| 600 |
-
f"3. Clear chat and try again\n"
|
| 601 |
-
f"4. Visit [status.groq.com](https://status.groq.com) for service status"
|
| 602 |
-
)
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
# Footer - FIXED VERSION
|
| 606 |
-
|
| 607 |
st.divider()
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
<p>Powered by <a href="https://groq.com" target="_blank">Groq AI</a></p>
|
| 612 |
-
<p style="font-size: 0.8em;">Disclaimer: This is an AI tutoring assistant. Always verify with official syllabus documents.</p>
|
| 613 |
-
</div>
|
| 614 |
-
"""
|
| 615 |
-
st.markdown(footer_html, unsafe_allow_html=True)
|
|
|
|
| 482 |
st.caption("Made with ❤️ for H2 Physics students | Powered by Groq AI")
|
| 483 |
|
| 484 |
|
| 485 |
+
# Footer - SUPER SIMPLE VERSION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
st.divider()
|
| 487 |
+
st.markdown("**H2 Physics Feynman Tutor** | Singapore H2 Physics (9478) Syllabus")
|
| 488 |
+
st.markdown("Powered by [Groq AI](https://groq.com)")
|
| 489 |
+
st.markdown("*Disclaimer: AI tutoring assistant. Verify with official syllabus.*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|