Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,47 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 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 |
-
return explain_science_concept(user_input)
|
| 49 |
-
elif 'python' in user_input.lower() or 'code' in user_input.lower():
|
| 50 |
-
return solve_python_code(user_input)
|
| 51 |
-
else:
|
| 52 |
-
return "Sorry, I don't understand. Can you please clarify your question?"
|
| 53 |
-
from sympy import symbols, Eq, solve
|
| 54 |
-
|
| 55 |
-
def solve_math_problem(user_input):
|
| 56 |
-
# For simplicity, assume the user asks a solvable equation like 'solve x + 5 = 10'
|
| 57 |
-
try:
|
| 58 |
-
# Create symbolic variable x
|
| 59 |
-
x = symbols('x')
|
| 60 |
-
|
| 61 |
-
# Parse and solve the equation
|
| 62 |
-
equation = Eq(eval(user_input), 0) # Simplified eval, you can make it more complex
|
| 63 |
-
solution = solve(equation, x)
|
| 64 |
-
|
| 65 |
-
return f"The solution to {user_input} is {solution}"
|
| 66 |
-
except Exception as e:
|
| 67 |
-
return "Sorry, I couldn't solve the math problem. Please check your input."
|
| 68 |
-
from textblob import TextBlob
|
| 69 |
-
|
| 70 |
-
def correct_grammar(user_input):
|
| 71 |
-
blob = TextBlob(user_input)
|
| 72 |
-
corrected = blob.correct()
|
| 73 |
-
|
| 74 |
-
return f"Corrected sentence: {corrected}"
|
| 75 |
-
def explain_science_concept(user_input):
|
| 76 |
-
# Placeholder logic, can be expanded to integrate an external knowledge base or API
|
| 77 |
-
return f"Here's an explanation about {user_input}. [This can be extended further with a real knowledge base]."
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 1 |
+
pip install streamlit requests
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# Set up the Groq API endpoint and your API key
|
| 6 |
+
GROQ_API_URL = "https://api.groq.com/v1/chat/completions"
|
| 7 |
+
GROQ_API_KEY = "your_groq_api_key_here" # Replace with your actual Groq API key
|
| 8 |
+
|
| 9 |
+
# Streamlit app title
|
| 10 |
+
st.title("Educational Chatbot")
|
| 11 |
+
|
| 12 |
+
# Initialize session state for chat history
|
| 13 |
+
if "messages" not in st.session_state:
|
| 14 |
+
st.session_state.messages = []
|
| 15 |
+
|
| 16 |
+
# Display chat messages from history on app rerun
|
| 17 |
+
for message in st.session_state.messages:
|
| 18 |
+
with st.chat_message(message["role"]):
|
| 19 |
+
st.markdown(message["content"])
|
| 20 |
+
|
| 21 |
+
# Accept user input
|
| 22 |
+
if prompt := st.chat_input("What is your question?"):
|
| 23 |
+
# Add user message to chat history
|
| 24 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 25 |
+
# Display user message in chat message container
|
| 26 |
+
with st.chat_message("user"):
|
| 27 |
+
st.markdown(prompt)
|
| 28 |
+
|
| 29 |
+
# Send user message to Groq API
|
| 30 |
+
headers = {
|
| 31 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 32 |
+
"Content-Type": "application/json"
|
| 33 |
+
}
|
| 34 |
+
data = {
|
| 35 |
+
"model": "groq-3.5-turbo", # Replace with the model you want to use
|
| 36 |
+
"messages": st.session_state.messages
|
| 37 |
+
}
|
| 38 |
+
response = requests.post(GROQ_API_URL, headers=headers, json=data)
|
| 39 |
+
if response.status_code == 200:
|
| 40 |
+
chatbot_response = response.json()["choices"][0]["message"]["content"]
|
| 41 |
+
# Add chatbot response to chat history
|
| 42 |
+
st.session_state.messages.append({"role": "assistant", "content": chatbot_response})
|
| 43 |
+
# Display chatbot response in chat message container
|
| 44 |
+
with st.chat_message("assistant"):
|
| 45 |
+
st.markdown(chatbot_response)
|
| 46 |
+
else:
|
| 47 |
+
st.error("Failed to get response from Groq API")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|