Spaces:
Build error
Build error
Update src/components/ai_tutor.py
Browse files- src/components/ai_tutor.py +26 -9
src/components/ai_tutor.py
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 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 |
def display_chat_interface(self):
|
| 10 |
-
"""Display the enhanced chat interface with voice output"""
|
| 11 |
st.header("AI Tutor")
|
| 12 |
|
| 13 |
# Voice controls
|
|
@@ -28,9 +30,6 @@ class AITutor:
|
|
| 28 |
</svg>
|
| 29 |
Voice Off
|
| 30 |
""", unsafe_allow_html=True)
|
| 31 |
-
with col2:
|
| 32 |
-
if self.service.tts_mode:
|
| 33 |
-
st.info(f"Using {self.service.tts_mode.upper()} TTS")
|
| 34 |
|
| 35 |
# Topic selection
|
| 36 |
topics = [None, 'Physics', 'Mathematics', 'Computer Science', 'Artificial Intelligence']
|
|
@@ -48,12 +47,15 @@ class AITutor:
|
|
| 48 |
# Display chat container
|
| 49 |
chat_container = st.container()
|
| 50 |
with chat_container:
|
| 51 |
-
# Display chat history with voice output
|
| 52 |
-
for message in context['chat_history']:
|
|
|
|
|
|
|
|
|
|
| 53 |
with st.chat_message(message["role"]):
|
| 54 |
st.write(message["content"])
|
| 55 |
if message["role"] == "assistant" and voice_active:
|
| 56 |
-
self.
|
| 57 |
|
| 58 |
# Chat input
|
| 59 |
if prompt := st.text_input("Ask your question...", key="chat_input"):
|
|
@@ -76,7 +78,22 @@ class AITutor:
|
|
| 76 |
context['chat_history'].append({
|
| 77 |
"role": "assistant",
|
| 78 |
"content": response,
|
| 79 |
-
"speak": True
|
| 80 |
})
|
| 81 |
|
| 82 |
-
# No need to rerun here, Streamlit will update automatically
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from src.services.ai_service import AITutorService
|
| 3 |
from src.utils.session import get_tutor_context
|
| 4 |
+
import openai
|
| 5 |
+
import os # Import the os module
|
| 6 |
|
| 7 |
class AITutor:
|
| 8 |
def __init__(self):
|
| 9 |
self.service = AITutorService()
|
| 10 |
|
| 11 |
def display_chat_interface(self):
|
| 12 |
+
"""Display the enhanced chat interface with voice output and tutor representation"""
|
| 13 |
st.header("AI Tutor")
|
| 14 |
|
| 15 |
# Voice controls
|
|
|
|
| 30 |
</svg>
|
| 31 |
Voice Off
|
| 32 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Topic selection
|
| 35 |
topics = [None, 'Physics', 'Mathematics', 'Computer Science', 'Artificial Intelligence']
|
|
|
|
| 47 |
# Display chat container
|
| 48 |
chat_container = st.container()
|
| 49 |
with chat_container:
|
| 50 |
+
# Display chat history with voice output and tutor representation
|
| 51 |
+
for i, message in enumerate(context['chat_history']):
|
| 52 |
+
if message["role"] == "assistant":
|
| 53 |
+
# Show tutor image for assistant messages
|
| 54 |
+
st.image("path/to/your/tutor_image.png", width=100) # Replace with your image path
|
| 55 |
with st.chat_message(message["role"]):
|
| 56 |
st.write(message["content"])
|
| 57 |
if message["role"] == "assistant" and voice_active:
|
| 58 |
+
self.speak(message["content"])
|
| 59 |
|
| 60 |
# Chat input
|
| 61 |
if prompt := st.text_input("Ask your question...", key="chat_input"):
|
|
|
|
| 78 |
context['chat_history'].append({
|
| 79 |
"role": "assistant",
|
| 80 |
"content": response,
|
|
|
|
| 81 |
})
|
| 82 |
|
| 83 |
+
# No need to rerun here, Streamlit will update automatically
|
| 84 |
+
|
| 85 |
+
def speak(self, text):
|
| 86 |
+
"""Uses OpenAI's TTS to speak the given text."""
|
| 87 |
+
try:
|
| 88 |
+
# Get the OpenAI API key from the environment variable
|
| 89 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 90 |
+
response = openai.Audio.create(
|
| 91 |
+
input=text,
|
| 92 |
+
model="tts-1",
|
| 93 |
+
voice="alloy"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Play the audio
|
| 97 |
+
st.audio(response['audio_content'], format="audio/mpeg")
|
| 98 |
+
except Exception as e:
|
| 99 |
+
st.error(f"Error in voice output: {e}")
|