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