Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,15 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import torch
|
| 3 |
-
import logging
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
import whisper
|
| 7 |
from TTS.api import TTS
|
| 8 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode, AudioProcessorBase
|
| 9 |
-
|
| 10 |
|
| 11 |
# Configure logging
|
| 12 |
logging.basicConfig(
|
|
@@ -23,186 +26,315 @@ logger = logging.getLogger(__name__)
|
|
| 23 |
AVATAR_PERSONAS = {
|
| 24 |
"Rat": {
|
| 25 |
"name": "Puzzle Master Rat",
|
| 26 |
-
"description": "
|
| 27 |
-
"teaching_style": "
|
| 28 |
-
"learning_approach": ""
|
| 29 |
-
Transform language learning into an intellectual adventure.
|
| 30 |
-
Break complex linguistic concepts into engaging, logical puzzles.
|
| 31 |
-
Encourage critical thinking and systematic problem-solving.
|
| 32 |
-
""",
|
| 33 |
"motivation_techniques": [
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
},
|
| 39 |
"Ox": {
|
| 40 |
"name": "Professor Steady Ox",
|
| 41 |
-
"description": "
|
| 42 |
-
"teaching_style": "
|
| 43 |
-
"learning_approach": ""
|
| 44 |
-
Construct language skills like building a robust architectural design.
|
| 45 |
-
Provide clear, systematic explanations of grammatical structures.
|
| 46 |
-
Emphasize consistent, patient progression in linguistic development.
|
| 47 |
-
""",
|
| 48 |
"motivation_techniques": [
|
| 49 |
-
"Break
|
| 50 |
-
"Celebrate
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
},
|
| 54 |
"Tiger": {
|
| 55 |
"name": "Adventure Coach Tiger",
|
| 56 |
-
"description": "
|
| 57 |
-
"teaching_style": "
|
| 58 |
-
"learning_approach": ""
|
| 59 |
-
Transform language learning into an exhilarating global adventure.
|
| 60 |
-
Create competitive and motivational linguistic environments.
|
| 61 |
-
Encourage bold communication attempts and celebrate linguistic courage.
|
| 62 |
-
""",
|
| 63 |
"motivation_techniques": [
|
| 64 |
-
"
|
| 65 |
-
"
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
},
|
| 69 |
"Rabbit": {
|
| 70 |
"name": "Storyteller Rabbit",
|
| 71 |
-
"description": "
|
| 72 |
-
"teaching_style": "
|
| 73 |
-
"learning_approach": ""
|
| 74 |
-
Teach language through compassionate, engaging storytelling.
|
| 75 |
-
Connect emotional experiences with linguistic development.
|
| 76 |
-
Create a safe, nurturing language learning sanctuary.
|
| 77 |
-
""",
|
| 78 |
"motivation_techniques": [
|
| 79 |
-
"Use
|
| 80 |
-
"
|
| 81 |
-
|
| 82 |
-
|
|
|
|
| 83 |
},
|
| 84 |
"Dragon": {
|
| 85 |
-
"name": "
|
| 86 |
-
"description": "
|
| 87 |
-
"teaching_style": "
|
| 88 |
-
"learning_approach": ""
|
| 89 |
-
Develop a scholarly, analytical approach to language acquisition.
|
| 90 |
-
Explore linguistic structures with intellectual curiosity.
|
| 91 |
-
Encourage deep, critical engagement with communication.
|
| 92 |
-
""",
|
| 93 |
"motivation_techniques": [
|
| 94 |
-
"
|
| 95 |
-
"
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
},
|
| 99 |
"Snake": {
|
| 100 |
-
"name": "
|
| 101 |
-
"description": "
|
| 102 |
-
"teaching_style": "
|
| 103 |
-
"learning_approach": ""
|
| 104 |
-
Explore language as a profound means of human connection.
|
| 105 |
-
Encourage deep thinking about communication's subtle nuances.
|
| 106 |
-
Develop critical, mindful language comprehension skills.
|
| 107 |
-
""",
|
| 108 |
"motivation_techniques": [
|
| 109 |
-
"
|
| 110 |
-
"
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
},
|
| 114 |
"Horse": {
|
| 115 |
"name": "Energetic Coach Horse",
|
| 116 |
-
"description": "
|
| 117 |
-
"teaching_style": "
|
| 118 |
-
"learning_approach": ""
|
| 119 |
-
Engage learners through vibrant, interactive language practice.
|
| 120 |
-
Encourage continuous speaking, listening, and communication.
|
| 121 |
-
Build confidence through enthusiastic participation.
|
| 122 |
-
""",
|
| 123 |
"motivation_techniques": [
|
| 124 |
-
"
|
| 125 |
-
"
|
| 126 |
-
|
| 127 |
-
|
|
|
|
| 128 |
},
|
| 129 |
"Goat": {
|
| 130 |
-
"name": "Creative
|
| 131 |
-
"description": "
|
| 132 |
-
"teaching_style": "
|
| 133 |
-
"learning_approach": ""
|
| 134 |
-
Transform language learning into an artistic, imaginative journey.
|
| 135 |
-
Connect vocabulary and grammar to creative expression.
|
| 136 |
-
Make linguistic development a playful, visual experience.
|
| 137 |
-
""",
|
| 138 |
"motivation_techniques": [
|
| 139 |
-
"
|
| 140 |
-
"
|
| 141 |
-
|
| 142 |
-
|
|
|
|
| 143 |
},
|
| 144 |
"Monkey": {
|
| 145 |
-
"name": "Playful
|
| 146 |
-
"description": "
|
| 147 |
-
"teaching_style": "
|
| 148 |
-
"learning_approach": ""
|
| 149 |
-
Transform language learning into an entertaining, witty adventure.
|
| 150 |
-
Use word games, linguistic puzzles, and clever challenges.
|
| 151 |
-
Make communication feel like an enjoyable, intellectual game.
|
| 152 |
-
""",
|
| 153 |
"motivation_techniques": [
|
| 154 |
-
"
|
| 155 |
-
"
|
| 156 |
-
|
| 157 |
-
|
|
|
|
| 158 |
},
|
| 159 |
"Rooster": {
|
| 160 |
-
"name": "Pronunciation
|
| 161 |
-
"description": "
|
| 162 |
-
"teaching_style": "
|
| 163 |
-
"learning_approach": ""
|
| 164 |
-
Develop crystal-clear pronunciation and melodious intonation.
|
| 165 |
-
Focus on the musical rhythm and precise articulation of language.
|
| 166 |
-
Encourage confident, articulate communication.
|
| 167 |
-
""",
|
| 168 |
"motivation_techniques": [
|
| 169 |
-
"Practice
|
| 170 |
-
"
|
| 171 |
-
|
| 172 |
-
|
|
|
|
| 173 |
},
|
| 174 |
"Dog": {
|
| 175 |
-
"name": "
|
| 176 |
-
"description": "
|
| 177 |
-
"teaching_style": "
|
| 178 |
-
"learning_approach": ""
|
| 179 |
-
Create a safe, unconditionally supportive language learning environment.
|
| 180 |
-
Build learner confidence through patient, kind guidance.
|
| 181 |
-
Emphasize emotional well-being in communication development.
|
| 182 |
-
""",
|
| 183 |
"motivation_techniques": [
|
| 184 |
-
"
|
| 185 |
-
"
|
| 186 |
-
|
| 187 |
-
|
|
|
|
| 188 |
},
|
| 189 |
"Pig": {
|
| 190 |
-
"name": "
|
| 191 |
-
"description": "
|
| 192 |
-
"teaching_style": "
|
| 193 |
-
"learning_approach": ""
|
| 194 |
-
Promote stress-free, organic language learning.
|
| 195 |
-
Allow learners to progress at their comfortable, natural pace.
|
| 196 |
-
Create a relaxed, enjoyable linguistic exploration environment.
|
| 197 |
-
""",
|
| 198 |
"motivation_techniques": [
|
| 199 |
-
"
|
| 200 |
-
"
|
| 201 |
-
|
| 202 |
-
|
|
|
|
| 203 |
}
|
| 204 |
}
|
| 205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
class AudioProcessor(AudioProcessorBase):
|
| 207 |
def __init__(self, companion):
|
| 208 |
self.companion = companion
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import streamlit.components.v1 as components
|
| 3 |
import torch
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
+
import av
|
| 6 |
+
import logging
|
| 7 |
+
import os
|
| 8 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 9 |
import whisper
|
| 10 |
from TTS.api import TTS
|
| 11 |
from streamlit_webrtc import webrtc_streamer, WebRtcMode, AudioProcessorBase
|
| 12 |
+
|
| 13 |
|
| 14 |
# Configure logging
|
| 15 |
logging.basicConfig(
|
|
|
|
| 26 |
AVATAR_PERSONAS = {
|
| 27 |
"Rat": {
|
| 28 |
"name": "Puzzle Master Rat",
|
| 29 |
+
"description": "I love solving word puzzles and making learning fun!",
|
| 30 |
+
"teaching_style": "Learning is like solving a fun game",
|
| 31 |
+
"learning_approach": "I break big words into small, easy pieces",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
"motivation_techniques": [
|
| 33 |
+
"Turn hard words into exciting challenges",
|
| 34 |
+
"Make learning feel like play"
|
| 35 |
+
],
|
| 36 |
+
"voice_sample": "Let's solve this language puzzle together!",
|
| 37 |
+
"image": "rat.png"
|
| 38 |
},
|
| 39 |
"Ox": {
|
| 40 |
"name": "Professor Steady Ox",
|
| 41 |
+
"description": "I help you learn English step by step, slowly and carefully",
|
| 42 |
+
"teaching_style": "Learning is like building a big tower, one block at a time",
|
| 43 |
+
"learning_approach": "We go slow and make sure you understand everything",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
"motivation_techniques": [
|
| 45 |
+
"Break big lessons into small, easy parts",
|
| 46 |
+
"Celebrate every little success"
|
| 47 |
+
],
|
| 48 |
+
"voice_sample": "We will build your English skills step by step, carefully and steadily.",
|
| 49 |
+
"image": "ox.png"
|
| 50 |
},
|
| 51 |
"Tiger": {
|
| 52 |
"name": "Adventure Coach Tiger",
|
| 53 |
+
"description": "Learning English is an exciting adventure!",
|
| 54 |
+
"teaching_style": "Every English lesson is a fun mission",
|
| 55 |
+
"learning_approach": "We learn by trying new things and having fun",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
"motivation_techniques": [
|
| 57 |
+
"Make learning feel like a game",
|
| 58 |
+
"Cheer you on with lots of energy"
|
| 59 |
+
],
|
| 60 |
+
"voice_sample": "Are you ready for an exciting English language adventure?",
|
| 61 |
+
"image": "tiger.png"
|
| 62 |
},
|
| 63 |
"Rabbit": {
|
| 64 |
"name": "Storyteller Rabbit",
|
| 65 |
+
"description": "I love telling stories that help you learn English",
|
| 66 |
+
"teaching_style": "Learning through fun and friendly stories",
|
| 67 |
+
"learning_approach": "Words become magic when they tell a story",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
"motivation_techniques": [
|
| 69 |
+
"Use funny and interesting stories",
|
| 70 |
+
"Help you imagine new words"
|
| 71 |
+
],
|
| 72 |
+
"voice_sample": "Let me tell you a story that will help you learn English.",
|
| 73 |
+
"image": "rabbit.png"
|
| 74 |
},
|
| 75 |
"Dragon": {
|
| 76 |
+
"name": "Smart Dragon",
|
| 77 |
+
"description": "I help you understand English like a language explorer",
|
| 78 |
+
"teaching_style": "Learning is like discovering a new world",
|
| 79 |
+
"learning_approach": "We look at words like they are treasure maps",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
"motivation_techniques": [
|
| 81 |
+
"Make learning feel like an exciting discovery",
|
| 82 |
+
"Explain things in a clear way"
|
| 83 |
+
],
|
| 84 |
+
"voice_sample": "Let us explore the world of English together!",
|
| 85 |
+
"image": "dragon.png"
|
| 86 |
},
|
| 87 |
"Snake": {
|
| 88 |
+
"name": "Wise Snake",
|
| 89 |
+
"description": "I help you understand English slowly and carefully",
|
| 90 |
+
"teaching_style": "Learning is like solving a gentle puzzle",
|
| 91 |
+
"learning_approach": "We think about words and their meanings",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
"motivation_techniques": [
|
| 93 |
+
"Take time to understand each word",
|
| 94 |
+
"Think about how words connect"
|
| 95 |
+
],
|
| 96 |
+
"voice_sample": "Let's understand language together, step by step.",
|
| 97 |
+
"image": "snake.png"
|
| 98 |
},
|
| 99 |
"Horse": {
|
| 100 |
"name": "Energetic Coach Horse",
|
| 101 |
+
"description": "Let's speak English and have fun!",
|
| 102 |
+
"teaching_style": "Learning is an active, exciting game",
|
| 103 |
+
"learning_approach": "We learn by speaking and playing",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
"motivation_techniques": [
|
| 105 |
+
"Speak English with lots of energy",
|
| 106 |
+
"Make learning feel like a fun activity"
|
| 107 |
+
],
|
| 108 |
+
"voice_sample": "Come on, let's speak English and have fun doing it!",
|
| 109 |
+
"image": "horse.png"
|
| 110 |
},
|
| 111 |
"Goat": {
|
| 112 |
+
"name": "Creative Goat",
|
| 113 |
+
"description": "I help you draw pictures with English words",
|
| 114 |
+
"teaching_style": "Learning is like creating colorful art",
|
| 115 |
+
"learning_approach": "We use imagination to learn words",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
"motivation_techniques": [
|
| 117 |
+
"Make words feel like colorful pictures",
|
| 118 |
+
"Use creativity to remember"
|
| 119 |
+
],
|
| 120 |
+
"voice_sample": "Let's paint beautiful pictures with our English words!",
|
| 121 |
+
"image": "sheep.png"
|
| 122 |
},
|
| 123 |
"Monkey": {
|
| 124 |
+
"name": "Playful Monkey",
|
| 125 |
+
"description": "Learning English is the most fun game!",
|
| 126 |
+
"teaching_style": "Every lesson is a funny, exciting game",
|
| 127 |
+
"learning_approach": "We laugh and learn at the same time",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
"motivation_techniques": [
|
| 129 |
+
"Turn learning into a funny game",
|
| 130 |
+
"Make English feel like play"
|
| 131 |
+
],
|
| 132 |
+
"voice_sample": "Learning English is the most fun game we'll play today!",
|
| 133 |
+
"image": "monkey.png"
|
| 134 |
},
|
| 135 |
"Rooster": {
|
| 136 |
+
"name": "Pronunciation Rooster",
|
| 137 |
+
"description": "I help you speak English clearly and correctly",
|
| 138 |
+
"teaching_style": "Learning is about saying words just right",
|
| 139 |
+
"learning_approach": "We practice saying words perfectly",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
"motivation_techniques": [
|
| 141 |
+
"Practice saying words clearly",
|
| 142 |
+
"Make pronunciation feel like a fun challenge"
|
| 143 |
+
],
|
| 144 |
+
"voice_sample": "Listen carefully and repeat after me, with perfect pronunciation!",
|
| 145 |
+
"image": "rooster.png"
|
| 146 |
},
|
| 147 |
"Dog": {
|
| 148 |
+
"name": "Friendly Dog",
|
| 149 |
+
"description": "I'm always here to help you learn English",
|
| 150 |
+
"teaching_style": "Learning is about being kind and patient",
|
| 151 |
+
"learning_approach": "We learn together, step by step",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
"motivation_techniques": [
|
| 153 |
+
"Cheer you on with lots of love",
|
| 154 |
+
"Make you feel confident"
|
| 155 |
+
],
|
| 156 |
+
"voice_sample": "You're doing great! Keep practicing your English.",
|
| 157 |
+
"image": "dog.png"
|
| 158 |
},
|
| 159 |
"Pig": {
|
| 160 |
+
"name": "Calm Pig",
|
| 161 |
+
"description": "Let's learn English together, nice and easy",
|
| 162 |
+
"teaching_style": "Learning is relaxed and comfortable",
|
| 163 |
+
"learning_approach": "We take our time and enjoy learning",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
"motivation_techniques": [
|
| 165 |
+
"Make learning feel relaxed",
|
| 166 |
+
"No stress, just fun"
|
| 167 |
+
],
|
| 168 |
+
"voice_sample": "Let's learn English together, nice and easy.",
|
| 169 |
+
"image": "pig.png"
|
| 170 |
}
|
| 171 |
}
|
| 172 |
|
| 173 |
+
class CharacterSelector:
|
| 174 |
+
def __init__(self):
|
| 175 |
+
self.tts_model = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
|
| 176 |
+
|
| 177 |
+
def create_avatar_carousel(self):
|
| 178 |
+
"""
|
| 179 |
+
Create a Swiper-like avatar carousel using HTML, CSS, and JavaScript
|
| 180 |
+
"""
|
| 181 |
+
carousel_html = """
|
| 182 |
+
<link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css">
|
| 183 |
+
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
|
| 184 |
+
|
| 185 |
+
<style>
|
| 186 |
+
.swiper-container {
|
| 187 |
+
width: 100%;
|
| 188 |
+
height: 500px;
|
| 189 |
+
}
|
| 190 |
+
.swiper-slide {
|
| 191 |
+
text-align: center;
|
| 192 |
+
background: #f4f4f4;
|
| 193 |
+
display: flex;
|
| 194 |
+
flex-direction: column;
|
| 195 |
+
align-items: center;
|
| 196 |
+
justify-content: center;
|
| 197 |
+
border-radius: 18px;
|
| 198 |
+
padding: 20px;
|
| 199 |
+
}
|
| 200 |
+
.avatar-image {
|
| 201 |
+
width: 250px;
|
| 202 |
+
height: 250px;
|
| 203 |
+
object-fit: cover;
|
| 204 |
+
border-radius: 50%;
|
| 205 |
+
margin-bottom: 15px;
|
| 206 |
+
}
|
| 207 |
+
.voice-preview-btn {
|
| 208 |
+
margin-top: 10px;
|
| 209 |
+
padding: 10px;
|
| 210 |
+
background-color: #4CAF50;
|
| 211 |
+
color: white;
|
| 212 |
+
border: none;
|
| 213 |
+
border-radius: 5px;
|
| 214 |
+
cursor: pointer;
|
| 215 |
+
}
|
| 216 |
+
</style>
|
| 217 |
+
|
| 218 |
+
<div class="swiper-container">
|
| 219 |
+
<div class="swiper-wrapper">
|
| 220 |
+
"""
|
| 221 |
+
|
| 222 |
+
# Generate slides for each avatar
|
| 223 |
+
for avatar_key, avatar_info in AVATAR_PERSONAS.items():
|
| 224 |
+
carousel_html += f"""
|
| 225 |
+
<div class="swiper-slide" data-avatar="{avatar_key}">
|
| 226 |
+
<img src="images/{avatar_info['image']}" class="avatar-image" alt="{avatar_info['name']}">
|
| 227 |
+
<h3>{avatar_info['name']}</h3>
|
| 228 |
+
<p>{avatar_info['description']}</p>
|
| 229 |
+
<button class="voice-preview-btn" onclick="previewVoice('{avatar_key}')">
|
| 230 |
+
Preview Voice
|
| 231 |
+
</button>
|
| 232 |
+
</div>
|
| 233 |
+
"""
|
| 234 |
+
|
| 235 |
+
carousel_html += """
|
| 236 |
+
</div>
|
| 237 |
+
<div class="swiper-pagination"></div>
|
| 238 |
+
<div class="swiper-button-prev"></div>
|
| 239 |
+
<div class="swiper-button-next"></div>
|
| 240 |
+
</div>
|
| 241 |
+
|
| 242 |
+
<script>
|
| 243 |
+
var swiper = new Swiper('.swiper-container', {
|
| 244 |
+
slidesPerView: 'auto',
|
| 245 |
+
centeredSlides: true,
|
| 246 |
+
spaceBetween: 30,
|
| 247 |
+
pagination: {
|
| 248 |
+
el: '.swiper-pagination',
|
| 249 |
+
clickable: true
|
| 250 |
+
},
|
| 251 |
+
navigation: {
|
| 252 |
+
nextEl: '.swiper-button-next',
|
| 253 |
+
prevEl: '.swiper-button-prev',
|
| 254 |
+
}
|
| 255 |
+
});
|
| 256 |
+
|
| 257 |
+
function previewVoice(avatarKey) {
|
| 258 |
+
window.parent.postMessage({
|
| 259 |
+
type: 'previewVoice',
|
| 260 |
+
avatarKey: avatarKey
|
| 261 |
+
}, '*');
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
function selectAvatar(avatarKey) {
|
| 265 |
+
window.parent.postMessage({
|
| 266 |
+
type: 'avatarSelected',
|
| 267 |
+
avatarKey: avatarKey
|
| 268 |
+
}, '*');
|
| 269 |
+
}
|
| 270 |
+
</script>
|
| 271 |
+
"""
|
| 272 |
+
|
| 273 |
+
return carousel_html
|
| 274 |
+
|
| 275 |
+
def generate_voice_preview(self, avatar_key):
|
| 276 |
+
"""Generate voice preview for a specific avatar"""
|
| 277 |
+
avatar = AVATAR_PERSONAS[avatar_key]
|
| 278 |
+
try:
|
| 279 |
+
# Generate voice preview
|
| 280 |
+
self.tts_model.tts_to_file(
|
| 281 |
+
text=avatar['voice_sample'],
|
| 282 |
+
file_path=f"{avatar_key}_preview.wav"
|
| 283 |
+
)
|
| 284 |
+
return f"{avatar_key}_preview.wav"
|
| 285 |
+
except Exception as e:
|
| 286 |
+
logging.error(f"Voice preview error for {avatar_key}: {e}")
|
| 287 |
+
return None
|
| 288 |
+
|
| 289 |
+
def main():
|
| 290 |
+
st.title("Language Learning Companion")
|
| 291 |
+
|
| 292 |
+
# Character Selection Carousel
|
| 293 |
+
character_selector = CharacterSelector()
|
| 294 |
+
|
| 295 |
+
# Render Swiper Carousel
|
| 296 |
+
carousel_html = character_selector.create_avatar_carousel()
|
| 297 |
+
components.html(carousel_html, height=600, scrolling=True)
|
| 298 |
+
|
| 299 |
+
# JavaScript communication for avatar selection and voice preview
|
| 300 |
+
components.html("""
|
| 301 |
+
<script>
|
| 302 |
+
window.addEventListener('message', function(event) {
|
| 303 |
+
if (event.data.type === 'avatarSelected') {
|
| 304 |
+
window.parent.postMessage({
|
| 305 |
+
type: 'streamlit:setComponentValue',
|
| 306 |
+
key: 'selected_avatar',
|
| 307 |
+
value: event.data.avatarKey
|
| 308 |
+
}, '*');
|
| 309 |
+
}
|
| 310 |
+
if (event.data.type === 'previewVoice') {
|
| 311 |
+
window.parent.postMessage({
|
| 312 |
+
type: 'streamlit:setComponentValue',
|
| 313 |
+
key: 'preview_voice',
|
| 314 |
+
value: event.data.avatarKey
|
| 315 |
+
}, '*');
|
| 316 |
+
}
|
| 317 |
+
});
|
| 318 |
+
</script>
|
| 319 |
+
""", height=0)
|
| 320 |
+
|
| 321 |
+
# Handle Voice Preview
|
| 322 |
+
preview_avatar = st.experimental_get_query_params().get('preview_voice', [None])[0]
|
| 323 |
+
if preview_avatar:
|
| 324 |
+
preview_audio = character_selector.generate_voice_preview(preview_avatar)
|
| 325 |
+
if preview_audio:
|
| 326 |
+
st.audio(preview_audio, format='audio/wav')
|
| 327 |
+
|
| 328 |
+
# Selected Avatar Handling
|
| 329 |
+
selected_avatar = st.experimental_get_query_params().get('selected_avatar', [None])[0]
|
| 330 |
+
if selected_avatar:
|
| 331 |
+
# Initialize Language Learning Companion with selected avatar
|
| 332 |
+
companion = LanguageLearningCompanion(selected_avatar)
|
| 333 |
+
# Rest of the conversation interface...
|
| 334 |
+
|
| 335 |
+
if __name__ == "__main__":
|
| 336 |
+
main()
|
| 337 |
+
|
| 338 |
class AudioProcessor(AudioProcessorBase):
|
| 339 |
def __init__(self, companion):
|
| 340 |
self.companion = companion
|