Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,125 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from dotenv import load_dotenv
|
| 3 |
-
import os
|
| 4 |
-
import tempfile
|
| 5 |
-
from gtts import gTTS # ✅ Use gTTS instead of pyttsx3
|
| 6 |
-
from langchain_groq import ChatGroq
|
| 7 |
-
from langchain.chains import LLMChain
|
| 8 |
-
from langchain.prompts import PromptTemplate
|
| 9 |
-
|
| 10 |
-
# Load environment variables
|
| 11 |
-
load_dotenv()
|
| 12 |
-
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 13 |
-
|
| 14 |
-
# ✅ Exact optimized prompt as provided
|
| 15 |
-
optimized_prompt = PromptTemplate(
|
| 16 |
-
input_variables=["problem"],
|
| 17 |
-
template=(
|
| 18 |
-
"You are an advanced AI tutor specializing in solving **math and physics problems** step by step. "
|
| 19 |
-
"Your goal is to **guide students logically**, ensuring they **understand every step** and its relevance. "
|
| 20 |
-
"Think like a **patient teacher** who explains concepts with clarity.\n\n"
|
| 21 |
-
|
| 22 |
-
"📚 **Guidelines for solving problems:**\n"
|
| 23 |
-
"1️⃣ **Understanding the Problem:**\n"
|
| 24 |
-
" - Restate the problem in simple terms.\n"
|
| 25 |
-
" - Identify what is given and what needs to be found.\n\n"
|
| 26 |
-
|
| 27 |
-
"2️⃣ **Relevant Concepts & Formulas:**\n"
|
| 28 |
-
" - List the key principles, equations, or theorems needed to solve the problem.\n"
|
| 29 |
-
" - Explain why they are relevant.\n\n"
|
| 30 |
-
|
| 31 |
-
"3️⃣ **Step-by-Step Solution:**\n"
|
| 32 |
-
" - Break down the solution into small, logical steps.\n"
|
| 33 |
-
" - Show calculations with proper notation.\n"
|
| 34 |
-
" - Explain **each transformation, substitution, or simplification** clearly.\n\n"
|
| 35 |
-
|
| 36 |
-
"4️⃣ **Final Answer:**\n"
|
| 37 |
-
" - ✅ Box or highlight the final result.\n"
|
| 38 |
-
" - Include units where applicable.\n\n"
|
| 39 |
-
|
| 40 |
-
"5️⃣ **Verification & Insights:**\n"
|
| 41 |
-
" - 🔄 Verify the answer using an alternative method (if possible).\n"
|
| 42 |
-
" - 🏗️ Provide a real-world analogy or intuition behind the result.\n\n"
|
| 43 |
-
|
| 44 |
-
"💡 **Example Solutions:**\n"
|
| 45 |
-
"---\n"
|
| 46 |
-
"🔢 **Example 1:**\n"
|
| 47 |
-
"**Problem:** Find the integral of x.\n\n"
|
| 48 |
-
"**Solution:**\n"
|
| 49 |
-
"1️⃣ **Understanding the Problem:** Compute the indefinite integral of f(x) = x.\n"
|
| 50 |
-
"2️⃣ **Relevant Concepts:** Use the power rule: ∫x^n dx = (x^(n+1))/(n+1) + C.\n"
|
| 51 |
-
"3️⃣ **Step-by-Step Solution:**\n"
|
| 52 |
-
" - Recognize x as x^1.\n"
|
| 53 |
-
" - Apply the power rule: increase the exponent by 1, giving x², then divide by the new exponent.\n"
|
| 54 |
-
" - Add the constant of integration, C.\n"
|
| 55 |
-
"4️⃣ **Final Answer:** (x²)/2 + C.\n"
|
| 56 |
-
"5️⃣ **Verification & Insights:** Differentiating (x²)/2 gives x, confirming correctness.\n\n"
|
| 57 |
-
"---\n"
|
| 58 |
-
"🚗 **Example 2:**\n"
|
| 59 |
-
"**Problem:** A car accelerates from rest at 5 m/s². Find its velocity after 4 seconds.\n\n"
|
| 60 |
-
"**Solution:**\n"
|
| 61 |
-
"1️⃣ **Understanding the Problem:** The car starts from rest and accelerates uniformly.\n"
|
| 62 |
-
"2️⃣ **Relevant Concepts:** Use kinematic equation: v = u + at.\n"
|
| 63 |
-
"3️⃣ **Step-by-Step Solution:**\n"
|
| 64 |
-
" - Given: u = 0 m/s, a = 5 m/s², t = 4 s.\n"
|
| 65 |
-
" - Apply formula: v = 0 + (5 × 4) = 20 m/s.\n"
|
| 66 |
-
"4️⃣ **Final Answer:** v = 20 m/s.\n"
|
| 67 |
-
"5️⃣ **Verification & Insights:** The result aligns with expected acceleration; using v² = u² + 2as also gives v = 20 m/s.\n\n"
|
| 68 |
-
"---\n\n"
|
| 69 |
-
|
| 70 |
-
"🎯 **Now, solve the following problem using this structured approach:**\n"
|
| 71 |
-
"**Problem:** {problem}\n\n"
|
| 72 |
-
"**Solution:**"
|
| 73 |
-
)
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
# Initialize Groq API model
|
| 77 |
-
llm = ChatGroq(api_key=GROQ_API_KEY, model_name="gemma2-9b-it")
|
| 78 |
-
llm_chain = LLMChain(llm=llm, prompt=optimized_prompt)
|
| 79 |
-
|
| 80 |
-
# Function to generate speech using gTTS
|
| 81 |
-
def text_to_speech(text):
|
| 82 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
| 83 |
-
tts = gTTS(text, lang="en")
|
| 84 |
-
tts.save(temp_audio.name)
|
| 85 |
-
return temp_audio.name # Return file path
|
| 86 |
-
|
| 87 |
-
# Streamlit UI Design
|
| 88 |
-
st.set_page_config(page_title="STEM Solver 🤖", layout="centered", page_icon="🧠")
|
| 89 |
-
|
| 90 |
-
st.markdown("<h1 style='text-align: center;'>📚 STEM Problem Solver 🤖</h1>", unsafe_allow_html=True)
|
| 91 |
-
st.markdown("<p style='text-align: center; font-size:18px;'>Enter a math or physics problem, and I'll solve it step by step! 🚀</p>", unsafe_allow_html=True)
|
| 92 |
-
|
| 93 |
-
# User Input
|
| 94 |
-
problem = st.text_area("📝 Enter your problem:", placeholder="e.g., What is the integral of x?", height=100)
|
| 95 |
-
|
| 96 |
-
# Solve (Text) Button
|
| 97 |
-
if st.button("🔍 Solve (Text)"):
|
| 98 |
-
if problem.strip():
|
| 99 |
-
with st.spinner("Thinking... 🤔"):
|
| 100 |
-
response = llm_chain.invoke({"problem": problem})
|
| 101 |
-
solution_text = response['text']
|
| 102 |
-
st.success("✅ Solution Found!")
|
| 103 |
-
|
| 104 |
-
st.markdown("### ✨ Solve (Text):")
|
| 105 |
-
st.markdown(f"<div style='background-color:#222831; padding:15px; border-radius:10px; color:white;'>"
|
| 106 |
-
f"<p style='font-size:16px;'>{solution_text}</p></div>", unsafe_allow_html=True)
|
| 107 |
-
else:
|
| 108 |
-
st.warning("⚠️ Please enter a valid problem.")
|
| 109 |
-
|
| 110 |
-
# Solve (Speech) Button
|
| 111 |
-
if st.button("🔊 Solve (Speech)"):
|
| 112 |
-
if problem.strip():
|
| 113 |
-
with st.spinner("Speaking... 🎤"):
|
| 114 |
-
response = llm_chain.invoke({"problem": problem})
|
| 115 |
-
solution_text = response['text']
|
| 116 |
-
|
| 117 |
-
audio_file_path = text_to_speech(solution_text) # Generate speech
|
| 118 |
-
|
| 119 |
-
st.success("✅ Solve (Speech) Started!")
|
| 120 |
-
st.audio(audio_file_path, format="audio/mp3") # ✅ Play speech
|
| 121 |
-
else:
|
| 122 |
-
st.warning("⚠️ Please enter a valid problem.")
|
| 123 |
-
|
| 124 |
-
# Footer
|
| 125 |
-
st.markdown("<br><p style='text-align:center; font-size:14px;'>🚀 Created with ❤️ by an AI-powered tutor! 📖</p>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|