Azmathussainthebo commited on
Commit
d8ae058
·
verified ·
1 Parent(s): 4ef07f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -81
app.py CHANGED
@@ -1,81 +1,47 @@
1
- from flask import Flask, request, jsonify, render_template
2
- from dotenv import load_dotenv
3
- import os
4
-
5
- # Import chatbot logic
6
- from chatbot import Chatbot
7
-
8
- # Load environment variables from .env file
9
- load_dotenv()
10
-
11
- app = Flask(__name__)
12
-
13
- # Initialize the chatbot
14
- chatbot = Chatbot()
15
-
16
- @app.route('/')
17
- def home():
18
- return render_template('index.html')
19
-
20
- @app.route('/ask', methods=['POST'])
21
- def ask():
22
- # Get the user's message
23
- user_message = request.form['message']
24
-
25
- # Get the response from the chatbot
26
- response = chatbot.get_response(user_message)
27
-
28
- return jsonify({'response': response})
29
-
30
- if __name__ == '__main__':
31
- app.run(debug=True)
32
- from .math import solve_math_problem
33
- from .english import correct_grammar
34
- from .science import explain_science_concept
35
- from .python import solve_python_code
36
-
37
- class Chatbot:
38
- def __init__(self):
39
- pass
40
-
41
- def get_response(self, user_input):
42
- # Determine the subject and return the appropriate response
43
- if 'math' in user_input.lower():
44
- return solve_math_problem(user_input)
45
- elif 'grammar' in user_input.lower() or 'english' in user_input.lower():
46
- return correct_grammar(user_input)
47
- elif 'science' in user_input.lower():
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")