Spaces:
Build error
Build error
Update src/components/ai_tutor.py
Browse files- src/components/ai_tutor.py +15 -19
src/components/ai_tutor.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def display_chat_interface(self):
|
| 11 |
"""Display the enhanced chat interface with voice output"""
|
|
@@ -14,8 +17,8 @@ class AITutor:
|
|
| 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"/>
|
|
@@ -49,37 +52,30 @@ class AITutor:
|
|
| 49 |
# Display chat container
|
| 50 |
chat_container = st.container()
|
| 51 |
with chat_container:
|
| 52 |
-
|
| 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 !=
|
| 62 |
self.handle_user_input(prompt)
|
| 63 |
-
|
| 64 |
|
| 65 |
def handle_user_input(self, user_input: str):
|
| 66 |
"""Process user input and generate response"""
|
| 67 |
context = get_tutor_context()
|
| 68 |
|
| 69 |
-
|
| 70 |
-
context['chat_history'].append({
|
| 71 |
"role": "user",
|
| 72 |
"content": user_input
|
| 73 |
})
|
| 74 |
|
| 75 |
-
# Generate and display AI response
|
| 76 |
response = self.service.generate_response(user_input, context['current_topic'])
|
| 77 |
|
| 78 |
-
|
| 79 |
-
context['chat_history'].append({
|
| 80 |
"role": "assistant",
|
| 81 |
"content": response,
|
| 82 |
-
"speak": True # You might want to remove this if not using voice output
|
| 83 |
})
|
| 84 |
-
|
| 85 |
-
# No need to rerun here, Streamlit will update automatically
|
|
|
|
| 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 |
# 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"/>
|
|
|
|
| 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 |
})
|
|
|
|
|
|