cryogenic22 commited on
Commit
50e8ea8
·
verified ·
1 Parent(s): f11ae7d

Update src/components/ai_tutor.py

Browse files
Files changed (1) hide show
  1. src/components/ai_tutor.py +33 -19
src/components/ai_tutor.py CHANGED
@@ -1,14 +1,11 @@
1
  import streamlit as st
2
- from src.services.ai_service import AITutorService # Assuming this is your AI service
3
- from src.utils.session import get_tutor_context # Assuming this provides context
4
 
5
  class AITutor:
6
  def __init__(self):
7
  self.service = AITutorService()
8
- if "chat_history" not in st.session_state:
9
- st.session_state.chat_history = []
10
- if "last_question" not in st.session_state:
11
- st.session_state.last_question = None
12
 
13
  def display_chat_interface(self):
14
  """Display the enhanced chat interface with voice output"""
@@ -17,8 +14,8 @@ class AITutor:
17
  # Voice controls
18
  col1, col2 = st.columns([3, 2])
19
  with col1:
20
- st.session_state.voice_active = st.checkbox("Enable Voice", value=False, key="voice_active")
21
- if st.session_state.voice_active:
22
  st.markdown("""
23
  <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
24
  <path d="M15 8V16M11 8V16M7 12H5C3.89543 12 3 12.8954 3 14V16C3 17.1046 3.89543 18 5 18H7V12ZM19 12V16C19 17.1046 18.1046 18 17 18H15" stroke="#4A90E2" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
@@ -49,33 +46,50 @@ class AITutor:
49
  if selected_topic != context['current_topic']:
50
  context['current_topic'] = selected_topic
51
 
52
- # Display chat container
53
- chat_container = st.container()
54
- with chat_container:
55
- for message in st.session_state.chat_history:
 
56
  with st.chat_message(message["role"]):
57
  st.write(message["content"])
58
- if message["role"] == "assistant" and st.session_state.voice_active:
59
  self.service.speak(message["content"])
60
 
61
  # Chat input
62
  prompt = st.text_input("Ask your question...", key="chat_input")
63
- if prompt and prompt != st.session_state.last_question:
64
- self.handle_user_input(prompt)
65
- st.session_state.last_question = prompt
66
 
67
- def handle_user_input(self, user_input: str):
68
  """Process user input and generate response"""
69
  context = get_tutor_context()
70
 
71
- st.session_state.chat_history.append({
 
72
  "role": "user",
73
  "content": user_input
74
  })
75
 
 
 
 
 
 
 
 
76
  response = self.service.generate_response(user_input, context['current_topic'])
77
 
78
- st.session_state.chat_history.append({
 
79
  "role": "assistant",
80
  "content": response,
 
81
  })
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from src.services.ai_service import AITutorService
3
+ from src.utils.session import get_tutor_context
4
 
5
  class AITutor:
6
  def __init__(self):
7
  self.service = AITutorService()
8
+ self.last_question = None # Store the last question asked
 
 
 
9
 
10
  def display_chat_interface(self):
11
  """Display the enhanced chat interface with voice output"""
 
14
  # Voice controls
15
  col1, col2 = st.columns([3, 2])
16
  with col1:
17
+ voice_active = st.checkbox("Enable Voice", value=False, key="voice_active")
18
+ if voice_active:
19
  st.markdown("""
20
  <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
21
  <path d="M15 8V16M11 8V16M7 12H5C3.89543 12 3 12.8954 3 14V16C3 17.1046 3.89543 18 5 18H7V12ZM19 12V16C19 17.1046 18.1046 18 17 18H15" stroke="#4A90E2" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
 
46
  if selected_topic != context['current_topic']:
47
  context['current_topic'] = selected_topic
48
 
49
+ # Display chat container using st.empty
50
+ chat_container = st.empty()
51
+ with chat_container.container(): # Use nested container
52
+ # Display chat history with voice output
53
+ for message in context['chat_history']:
54
  with st.chat_message(message["role"]):
55
  st.write(message["content"])
56
+ if message["role"] == "assistant" and voice_active:
57
  self.service.speak(message["content"])
58
 
59
  # Chat input
60
  prompt = st.text_input("Ask your question...", key="chat_input")
61
+ if prompt and prompt != self.last_question: # Check if the question is new
62
+ self.handle_user_input(prompt, chat_container) # Pass the container
63
+ self.last_question = prompt # Update the last question asked
64
 
65
+ def handle_user_input(self, user_input: str, chat_container): # Accept the container
66
  """Process user input and generate response"""
67
  context = get_tutor_context()
68
 
69
+ # Add user message
70
+ context['chat_history'].append({
71
  "role": "user",
72
  "content": user_input
73
  })
74
 
75
+ # Update the chat container immediately with the user message
76
+ with chat_container.container(): # Use nested container
77
+ for message in context['chat_history']:
78
+ with st.chat_message(message["role"]):
79
+ st.write(message["content"])
80
+
81
+ # Generate and display AI response
82
  response = self.service.generate_response(user_input, context['current_topic'])
83
 
84
+ # Add AI response
85
+ context['chat_history'].append({
86
  "role": "assistant",
87
  "content": response,
88
+ "speak": True
89
  })
90
+
91
+ # Update the chat container again with the AI's response
92
+ with chat_container.container(): # Use nested container
93
+ for message in context['chat_history']:
94
+ with st.chat_message(message["role"]):
95
+ st.write(message["content"])