devfire commited on
Commit
7f792ef
·
verified ·
1 Parent(s): ca57036

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -9,18 +9,18 @@ os.environ["GROQ_API_KEY"] = GROQ_API_KEY
9
  # Initialize the Groq client
10
  client = Groq(api_key=GROQ_API_KEY)
11
 
12
- # Streamlit user interface
13
- st.title("AI Study Assistant Chatbot")
14
- st.write("Hello! I'm your AI Study Assistant. You can ask me anything related to your studies or exam preparation.")
15
 
16
- # User input for conversation
17
- user_input = st.text_input("Ask me anything about your study plan or exam:")
 
18
 
19
  # Function to generate chatbot response based on user input
20
  def generate_chatbot_response(user_message):
21
- # Generate prompt for Groq based on user message
22
  prompt = f"You are a helpful AI chatbot. The user is asking: {user_message}. Provide a detailed, helpful response."
23
-
24
  # Generate response using Groq API
25
  chat_completion = client.chat.completions.create(
26
  messages=[{"role": "user", "content": prompt}],
@@ -30,10 +30,17 @@ def generate_chatbot_response(user_message):
30
  response = chat_completion.choices[0].message.content
31
  return response
32
 
33
- # Display chatbot response when user submits a message
 
 
 
34
  if user_input:
35
  chatbot_response = generate_chatbot_response(user_input)
36
- st.write("### Chatbot Response:")
37
- st.write(chatbot_response)
38
- else:
39
- st.write("Please ask me a question about your study plan or exam.")
 
 
 
 
 
9
  # Initialize the Groq client
10
  client = Groq(api_key=GROQ_API_KEY)
11
 
12
+ # Streamlit user interface setup
13
+ st.title("AI Chatbot")
14
+ st.write("Hello! I'm your AI Study Assistant. Ask me anything, and I'll try to help.")
15
 
16
+ # Initialize session state for maintaining conversation
17
+ if 'conversation_history' not in st.session_state:
18
+ st.session_state.conversation_history = []
19
 
20
  # Function to generate chatbot response based on user input
21
  def generate_chatbot_response(user_message):
 
22
  prompt = f"You are a helpful AI chatbot. The user is asking: {user_message}. Provide a detailed, helpful response."
23
+
24
  # Generate response using Groq API
25
  chat_completion = client.chat.completions.create(
26
  messages=[{"role": "user", "content": prompt}],
 
30
  response = chat_completion.choices[0].message.content
31
  return response
32
 
33
+ # User input for conversation
34
+ user_input = st.text_input("Ask me anything:")
35
+
36
+ # Handle user input and display conversation
37
  if user_input:
38
  chatbot_response = generate_chatbot_response(user_input)
39
+
40
+ # Save the conversation history
41
+ st.session_state.conversation_history.append(("User: " + user_input, "Chatbot: " + chatbot_response))
42
+
43
+ # Display chat history
44
+ for question, answer in st.session_state.conversation_history:
45
+ st.markdown(f"**{question}**")
46
+ st.markdown(answer)