Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,316 +1,404 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import torch
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
)
|
| 11 |
-
|
| 12 |
|
| 13 |
-
# Avatar Personas
|
| 14 |
AVATAR_PERSONAS = {
|
| 15 |
"Rat": {
|
| 16 |
-
"image": "rat.png",
|
| 17 |
"name": "Puzzle Master Rat",
|
| 18 |
-
"description": "Clever
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
]
|
| 26 |
},
|
| 27 |
"Ox": {
|
| 28 |
-
"
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
"
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
]
|
| 38 |
},
|
| 39 |
"Tiger": {
|
| 40 |
-
"image": "tiger.png",
|
| 41 |
"name": "Adventure Coach Tiger",
|
| 42 |
-
"description": "
|
| 43 |
-
"
|
| 44 |
-
"
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
]
|
| 50 |
},
|
| 51 |
"Rabbit": {
|
| 52 |
-
"image": "rabbit.png",
|
| 53 |
"name": "Storyteller Rabbit",
|
| 54 |
-
"description": "Gentle
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
]
|
| 62 |
},
|
| 63 |
"Dragon": {
|
| 64 |
-
"image": "dragon.png",
|
| 65 |
"name": "Scholar Dragon",
|
| 66 |
-
"description": "
|
| 67 |
-
"
|
| 68 |
-
"
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
]
|
| 74 |
},
|
| 75 |
"Snake": {
|
| 76 |
-
"
|
| 77 |
-
"
|
| 78 |
-
"
|
| 79 |
-
"
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
]
|
| 86 |
},
|
| 87 |
"Horse": {
|
| 88 |
-
"image": "horse.png",
|
| 89 |
"name": "Energetic Coach Horse",
|
| 90 |
-
"description": "
|
| 91 |
-
"
|
| 92 |
-
"
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
]
|
| 98 |
},
|
| 99 |
"Goat": {
|
| 100 |
-
"image": "goat.png",
|
| 101 |
"name": "Creative Mentor Goat",
|
| 102 |
-
"description": "
|
| 103 |
-
"
|
| 104 |
-
"
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
]
|
| 110 |
},
|
| 111 |
"Monkey": {
|
| 112 |
-
"image": "monkey.png",
|
| 113 |
"name": "Playful Genius Monkey",
|
| 114 |
-
"description": "
|
| 115 |
-
"
|
| 116 |
-
"
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
]
|
| 122 |
},
|
| 123 |
"Rooster": {
|
| 124 |
-
"image": "rooster.png",
|
| 125 |
"name": "Pronunciation Master Rooster",
|
| 126 |
-
"description": "
|
| 127 |
-
"
|
| 128 |
-
"
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
]
|
| 134 |
},
|
| 135 |
"Dog": {
|
| 136 |
-
"image": "dog.png",
|
| 137 |
"name": "Supportive Buddy Dog",
|
| 138 |
-
"description": "Loyal
|
| 139 |
-
"
|
| 140 |
-
"
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
]
|
| 146 |
},
|
| 147 |
"Pig": {
|
| 148 |
-
"image": "pig.png",
|
| 149 |
"name": "Relaxed Mentor Pig",
|
| 150 |
-
"description": "Easygoing
|
| 151 |
-
"
|
| 152 |
-
"
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
]
|
| 158 |
}
|
| 159 |
}
|
| 160 |
|
| 161 |
-
class
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
def __init__(self, avatar_name):
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
try:
|
| 168 |
-
|
| 169 |
-
|
|
|
|
| 170 |
except Exception as e:
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
|
| 175 |
def generate_response(self, user_input):
|
| 176 |
-
|
| 177 |
-
return "Sorry, I'm having trouble generating a response."
|
| 178 |
-
|
| 179 |
-
# Incorporate avatar's unique personality
|
| 180 |
-
prompt = f"""
|
| 181 |
-
Avatar: {self.avatar['name']}
|
| 182 |
-
Teaching Style: {self.avatar['teaching_style']}
|
| 183 |
-
User Input: {user_input}
|
| 184 |
-
|
| 185 |
-
Generate a response that:
|
| 186 |
-
1. Reflects the avatar's unique personality
|
| 187 |
-
2. Provides encouraging language learning support
|
| 188 |
-
3. Maintains an engaging tone
|
| 189 |
-
"""
|
| 190 |
-
|
| 191 |
try:
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
-
response
|
| 200 |
return response
|
|
|
|
| 201 |
except Exception as e:
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
"""
|
| 206 |
-
First stage: Avatar Selection
|
| 207 |
-
"""
|
| 208 |
-
st.title("Choose Your Learning Companion")
|
| 209 |
-
|
| 210 |
-
# Custom CSS for avatar selection
|
| 211 |
-
st.markdown("""
|
| 212 |
-
<style>
|
| 213 |
-
.avatar-grid {
|
| 214 |
-
display: grid;
|
| 215 |
-
grid-template-columns: repeat(3, 1fr);
|
| 216 |
-
gap: 20px;
|
| 217 |
-
padding: 20px;
|
| 218 |
-
}
|
| 219 |
-
.avatar-card {
|
| 220 |
-
border: 2px solid #f0f0f0;
|
| 221 |
-
border-radius: 10px;
|
| 222 |
-
padding: 15px;
|
| 223 |
-
text-align: center;
|
| 224 |
-
transition: all 0.3s ease;
|
| 225 |
-
}
|
| 226 |
-
.avatar-card:hover {
|
| 227 |
-
transform: scale(1.05);
|
| 228 |
-
border-color: #4CAF50;
|
| 229 |
-
}
|
| 230 |
-
.avatar-image {
|
| 231 |
-
width: 200px;
|
| 232 |
-
height: 200px;
|
| 233 |
-
object-fit: cover;
|
| 234 |
-
border-radius: 50%;
|
| 235 |
-
}
|
| 236 |
-
</style>
|
| 237 |
-
""", unsafe_allow_html=True)
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
</div>
|
| 250 |
-
''', unsafe_allow_html=True)
|
| 251 |
-
|
| 252 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 253 |
-
|
| 254 |
-
# JavaScript to handle avatar selection
|
| 255 |
-
st.markdown("""
|
| 256 |
-
<script>
|
| 257 |
-
function selectAvatar(avatarKey) {
|
| 258 |
-
window.parent.postMessage({type: 'avatarSelected', avatarKey: avatarKey}, '*');
|
| 259 |
-
}
|
| 260 |
-
</script>
|
| 261 |
-
""", unsafe_allow_html=True)
|
| 262 |
|
| 263 |
-
def
|
| 264 |
-
""
|
| 265 |
-
Second stage: Conversation Interface
|
| 266 |
-
"""
|
| 267 |
-
st.title(f"Learning English with {AVATAR_PERSONAS[selected_avatar]['name']}")
|
| 268 |
|
| 269 |
-
#
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
-
#
|
| 273 |
-
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
# Initialize session state for tracking app stage
|
| 282 |
-
if 'stage' not in st.session_state:
|
| 283 |
-
st.session_state.stage = 'avatar_selection'
|
| 284 |
-
if 'selected_avatar' not in st.session_state:
|
| 285 |
-
st.session_state.selected_avatar = None
|
| 286 |
-
|
| 287 |
-
# Handle avatar selection
|
| 288 |
-
components.html("""
|
| 289 |
-
<script>
|
| 290 |
-
window.addEventListener('message', function(event) {
|
| 291 |
-
if (event.data.type === 'avatarSelected') {
|
| 292 |
-
window.parent.postMessage({
|
| 293 |
-
type: 'streamlit:setComponentValue',
|
| 294 |
-
key: 'selected_avatar',
|
| 295 |
-
value: event.data.avatarKey
|
| 296 |
-
}, '*');
|
| 297 |
-
}
|
| 298 |
-
});
|
| 299 |
-
</script>
|
| 300 |
-
""", height=0)
|
| 301 |
-
|
| 302 |
-
# Retrieve selected avatar
|
| 303 |
-
selected_avatar = st.experimental_get_query_params().get('selected_avatar', [None])[0]
|
| 304 |
|
| 305 |
-
if
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
|
| 315 |
if __name__ == "__main__":
|
| 316 |
main()
|
|
|
|
| 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 |
+
import av
|
| 10 |
+
|
| 11 |
+
# Configure logging
|
| 12 |
+
logging.basicConfig(
|
| 13 |
+
level=logging.INFO,
|
| 14 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 15 |
+
handlers=[
|
| 16 |
+
logging.FileHandler("language_companion.log"),
|
| 17 |
+
logging.StreamHandler()
|
| 18 |
+
]
|
| 19 |
)
|
| 20 |
+
logger = logging.getLogger(__name__)
|
| 21 |
|
| 22 |
+
# Comprehensive Avatar Personas
|
| 23 |
AVATAR_PERSONAS = {
|
| 24 |
"Rat": {
|
|
|
|
| 25 |
"name": "Puzzle Master Rat",
|
| 26 |
+
"description": "Clever linguistic detective specializing in language mysteries",
|
| 27 |
+
"teaching_style": "Analytical, puzzle-driven language learning",
|
| 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 |
+
"Convert grammar rules into exciting challenges",
|
| 35 |
+
"Reward innovative language problem-solving",
|
| 36 |
+
"Create intricate linguistic treasure hunts"
|
| 37 |
]
|
| 38 |
},
|
| 39 |
"Ox": {
|
| 40 |
+
"name": "Professor Steady Ox",
|
| 41 |
+
"description": "Methodical language foundation builder",
|
| 42 |
+
"teaching_style": "Structured, step-by-step comprehensive learning",
|
| 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 down complex language concepts into manageable segments",
|
| 50 |
+
"Celebrate incremental learning achievements",
|
| 51 |
+
"Provide consistent, encouraging feedback"
|
| 52 |
]
|
| 53 |
},
|
| 54 |
"Tiger": {
|
|
|
|
| 55 |
"name": "Adventure Coach Tiger",
|
| 56 |
+
"description": "Dynamic linguistic expedition leader",
|
| 57 |
+
"teaching_style": "High-energy, challenge-driven language exploration",
|
| 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 |
+
"Design exciting language learning challenges",
|
| 65 |
+
"Turn mistakes into heroic growth opportunities",
|
| 66 |
+
"Use passionate, motivational communication"
|
| 67 |
]
|
| 68 |
},
|
| 69 |
"Rabbit": {
|
|
|
|
| 70 |
"name": "Storyteller Rabbit",
|
| 71 |
+
"description": "Gentle narrative-based language nurturer",
|
| 72 |
+
"teaching_style": "Empathetic, story-driven emotional learning",
|
| 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 storytelling to illustrate complex language concepts",
|
| 80 |
+
"Connect vocabulary to emotional narratives",
|
| 81 |
+
"Provide gentle, supportive linguistic guidance"
|
| 82 |
]
|
| 83 |
},
|
| 84 |
"Dragon": {
|
|
|
|
| 85 |
"name": "Scholar Dragon",
|
| 86 |
+
"description": "Intellectual linguistic mentor",
|
| 87 |
+
"teaching_style": "Sophisticated, precision-focused academic learning",
|
| 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 |
+
"Explain intricate linguistic logic and patterns",
|
| 95 |
+
"Encourage sophisticated language analysis",
|
| 96 |
+
"Provide nuanced, intellectual explanations"
|
| 97 |
]
|
| 98 |
},
|
| 99 |
"Snake": {
|
| 100 |
+
"name": "Philosophical Snake",
|
| 101 |
+
"description": "Contemplative language wisdom guide",
|
| 102 |
+
"teaching_style": "Reflective, meditative language exploration",
|
| 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 |
+
"Discuss language as a cultural and philosophical expression",
|
| 110 |
+
"Encourage thoughtful, intentional communication",
|
| 111 |
+
"Explore linguistic subtleties and contextual meanings"
|
| 112 |
]
|
| 113 |
},
|
| 114 |
"Horse": {
|
|
|
|
| 115 |
"name": "Energetic Coach Horse",
|
| 116 |
+
"description": "Dynamic, interactive language motivator",
|
| 117 |
+
"teaching_style": "Active, participatory communication training",
|
| 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 |
+
"Create engaging, interactive speaking exercises",
|
| 125 |
+
"Provide immediate, energetic feedback",
|
| 126 |
+
"Celebrate every communication attempt"
|
| 127 |
]
|
| 128 |
},
|
| 129 |
"Goat": {
|
|
|
|
| 130 |
"name": "Creative Mentor Goat",
|
| 131 |
+
"description": "Imaginative linguistic artist",
|
| 132 |
+
"teaching_style": "Creative, visual language exploration",
|
| 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 |
+
"Use artistic and visual language learning techniques",
|
| 140 |
+
"Encourage creative linguistic representation",
|
| 141 |
+
"Turn grammar into artistic challenges"
|
| 142 |
]
|
| 143 |
},
|
| 144 |
"Monkey": {
|
|
|
|
| 145 |
"name": "Playful Genius Monkey",
|
| 146 |
+
"description": "Witty, intelligent language game master",
|
| 147 |
+
"teaching_style": "Humorous, game-oriented linguistic play",
|
| 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 |
+
"Create entertaining language learning games",
|
| 155 |
+
"Use humor to make learning memorable",
|
| 156 |
+
"Reward creative and playful language use"
|
| 157 |
]
|
| 158 |
},
|
| 159 |
"Rooster": {
|
|
|
|
| 160 |
"name": "Pronunciation Master Rooster",
|
| 161 |
+
"description": "Precise linguistic sound expert",
|
| 162 |
+
"teaching_style": "Phonetic, accent-perfection focused learning",
|
| 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 pronunciation through engaging exercises",
|
| 170 |
+
"Provide detailed, constructive phonetic guidance",
|
| 171 |
+
"Celebrate clear, confident speaking"
|
| 172 |
]
|
| 173 |
},
|
| 174 |
"Dog": {
|
|
|
|
| 175 |
"name": "Supportive Buddy Dog",
|
| 176 |
+
"description": "Loyal, encouraging language learning companion",
|
| 177 |
+
"teaching_style": "Supportive, confidence-building linguistic nurturing",
|
| 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 |
+
"Provide constant, positive linguistic reinforcement",
|
| 185 |
+
"Create a judgment-free learning space",
|
| 186 |
+
"Celebrate every communication milestone"
|
| 187 |
]
|
| 188 |
},
|
| 189 |
"Pig": {
|
|
|
|
| 190 |
"name": "Relaxed Mentor Pig",
|
| 191 |
+
"description": "Easygoing, kind language learning facilitator",
|
| 192 |
+
"teaching_style": "Calm, natural language absorption",
|
| 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 |
+
"Reduce language learning anxiety",
|
| 200 |
+
"Encourage natural, pressure-free communication",
|
| 201 |
+
"Provide gentle, supportive linguistic guidance"
|
| 202 |
]
|
| 203 |
}
|
| 204 |
}
|
| 205 |
|
| 206 |
+
class AudioProcessor(AudioProcessorBase):
|
| 207 |
+
def __init__(self, companion):
|
| 208 |
+
self.companion = companion
|
| 209 |
+
self.audio_buffer = []
|
| 210 |
+
self.sample_rate = 16000
|
| 211 |
+
self.recording_duration = 5 # 5 seconds of audio before processing
|
| 212 |
+
|
| 213 |
+
def recv(self, frame: av.AudioFrame) -> av.AudioFrame:
|
| 214 |
+
# Convert audio frame to numpy array
|
| 215 |
+
audio_input = frame.to_ndarray(format="f32")
|
| 216 |
+
|
| 217 |
+
# Accumulate audio
|
| 218 |
+
if len(audio_input.shape) > 1:
|
| 219 |
+
audio_input = audio_input[:, 0] # Take first channel if stereo
|
| 220 |
+
|
| 221 |
+
self.audio_buffer.extend(audio_input)
|
| 222 |
+
|
| 223 |
+
# Process when buffer reaches sufficient length
|
| 224 |
+
if len(self.audio_buffer) >= self.sample_rate * self.recording_duration:
|
| 225 |
+
try:
|
| 226 |
+
# Convert buffer to wav file
|
| 227 |
+
audio_array = np.array(self.audio_buffer[:self.sample_rate * self.recording_duration])
|
| 228 |
+
sf.write('temp_recording.wav', audio_array, self.sample_rate)
|
| 229 |
+
|
| 230 |
+
# Transcribe
|
| 231 |
+
transcription = self.companion.transcribe_audio('temp_recording.wav')
|
| 232 |
+
|
| 233 |
+
# Generate response
|
| 234 |
+
if transcription:
|
| 235 |
+
response = self.companion.generate_response(transcription)
|
| 236 |
+
|
| 237 |
+
# Text to Speech
|
| 238 |
+
if response:
|
| 239 |
+
audio_response = self.companion.text_to_speech(response)
|
| 240 |
+
|
| 241 |
+
# Display results
|
| 242 |
+
st.session_state.transcription = transcription
|
| 243 |
+
st.session_state.response = response
|
| 244 |
+
|
| 245 |
+
# Clear buffer
|
| 246 |
+
self.audio_buffer = []
|
| 247 |
+
|
| 248 |
+
except Exception as e:
|
| 249 |
+
logging.error(f"Audio processing error: {e}")
|
| 250 |
+
st.error("Error processing audio")
|
| 251 |
+
self.audio_buffer = []
|
| 252 |
+
|
| 253 |
+
return frame
|
| 254 |
+
|
| 255 |
+
def main():
|
| 256 |
+
st.title("Language Learning Companion")
|
| 257 |
+
|
| 258 |
+
# Initialize session state for storing conversation
|
| 259 |
+
if 'transcription' not in st.session_state:
|
| 260 |
+
st.session_state.transcription = ""
|
| 261 |
+
if 'response' not in st.session_state:
|
| 262 |
+
st.session_state.response = ""
|
| 263 |
+
|
| 264 |
+
# Avatar Selection
|
| 265 |
+
avatar_selection = st.selectbox(
|
| 266 |
+
"Choose Your Learning Buddy",
|
| 267 |
+
list(AVATAR_PERSONAS.keys())
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
# Initialize Companion
|
| 271 |
+
companion = LanguageLearningCompanion(avatar_selection)
|
| 272 |
+
|
| 273 |
+
# WebRTC Audio Capture with Custom Processor
|
| 274 |
+
ctx = webrtc_streamer(
|
| 275 |
+
key="language-learning",
|
| 276 |
+
mode=WebRtcMode.SENDRECV,
|
| 277 |
+
audio_processor_factory=lambda: AudioProcessor(companion),
|
| 278 |
+
media_stream_constraints={
|
| 279 |
+
"audio": True,
|
| 280 |
+
"video": False
|
| 281 |
+
}
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
# Display Conversation
|
| 285 |
+
if st.session_state.transcription:
|
| 286 |
+
st.subheader("Your Speech")
|
| 287 |
+
st.write(st.session_state.transcription)
|
| 288 |
+
|
| 289 |
+
if st.session_state.response:
|
| 290 |
+
st.subheader("Companion's Response")
|
| 291 |
+
st.write(st.session_state.response)
|
| 292 |
+
|
| 293 |
+
# Optional: Play response audio
|
| 294 |
+
if os.path.exists('response.wav'):
|
| 295 |
+
st.audio('response.wav', format='audio/wav')
|
| 296 |
+
|
| 297 |
+
if __name__ == "__main__":
|
| 298 |
+
main()
|
| 299 |
+
|
| 300 |
+
class LanguageLearningCompanion:
|
| 301 |
def __init__(self, avatar_name):
|
| 302 |
+
try:
|
| 303 |
+
# Model Initialization with Quantization
|
| 304 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 305 |
+
"mistralai/Mistral-7B-Instruct-v0.1",
|
| 306 |
+
load_in_8bit=True,
|
| 307 |
+
device_map="auto"
|
| 308 |
+
)
|
| 309 |
+
self.tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
|
| 310 |
+
|
| 311 |
+
# Speech Models
|
| 312 |
+
self.whisper_model = whisper.load_model("base")
|
| 313 |
+
self.tts_model = TTS(model_name="tts_models/multilingual/multi-dataset/your_tts")
|
| 314 |
+
|
| 315 |
+
# Avatar Selection
|
| 316 |
+
self.avatar = AVATAR_PERSONAS.get(avatar_name, AVATAR_PERSONAS["Rat"])
|
| 317 |
+
|
| 318 |
+
logger.info(f"Initialized Language Learning Companion with {avatar_name}")
|
| 319 |
|
| 320 |
+
except Exception as e:
|
| 321 |
+
logger.error(f"Model initialization error: {e}")
|
| 322 |
+
st.error("Error loading models. Please try again later.")
|
| 323 |
+
|
| 324 |
+
def transcribe_audio(self, audio_path):
|
| 325 |
+
"""Transcribe audio using Whisper"""
|
| 326 |
try:
|
| 327 |
+
result = self.whisper_model.transcribe(audio_path)
|
| 328 |
+
logger.info(f"Successfully transcribed audio: {result['text']}")
|
| 329 |
+
return result['text']
|
| 330 |
except Exception as e:
|
| 331 |
+
logger.error(f"Transcription error: {e}")
|
| 332 |
+
st.error("Error transcribing audio.")
|
| 333 |
+
return None
|
| 334 |
|
| 335 |
def generate_response(self, user_input):
|
| 336 |
+
"""Generate educational response using Mistral"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
try:
|
| 338 |
+
prompt = f"""
|
| 339 |
+
Avatar: {self.avatar['name']}
|
| 340 |
+
Teaching Style: {self.avatar['teaching_style']}
|
| 341 |
+
Learning Approach: {self.avatar['learning_approach']}
|
| 342 |
+
|
| 343 |
+
User Input: {user_input}
|
| 344 |
+
|
| 345 |
+
Generate an encouraging, educational response that:
|
| 346 |
+
1. Addresses the user's input
|
| 347 |
+
2. Provides gentle language learning guidance
|
| 348 |
+
3. Maintains an engaging, child-friendly tone
|
| 349 |
+
"""
|
| 350 |
+
|
| 351 |
+
inputs = self.tokenizer(prompt, return_tensors="pt")
|
| 352 |
+
outputs = self.model.generate(**inputs, max_length=200)
|
| 353 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 354 |
|
| 355 |
+
logger.info(f"Generated response for input: {user_input}")
|
| 356 |
return response
|
| 357 |
+
|
| 358 |
except Exception as e:
|
| 359 |
+
logger.error(f"Response generation error: {e}")
|
| 360 |
+
st.error("Error generating response.")
|
| 361 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
+
def text_to_speech(self, text):
|
| 364 |
+
"""Convert text to speech"""
|
| 365 |
+
try:
|
| 366 |
+
self.tts_model.tts_to_file(text=text, file_path="response.wav")
|
| 367 |
+
logger.info("Successfully converted text to speech")
|
| 368 |
+
return "response.wav"
|
| 369 |
+
except Exception as e:
|
| 370 |
+
logger.error(f"Text-to-speech conversion error: {e}")
|
| 371 |
+
st.error("Error converting text to speech.")
|
| 372 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
+
def main():
|
| 375 |
+
st.title("Language Learning Companion")
|
|
|
|
|
|
|
|
|
|
| 376 |
|
| 377 |
+
# Avatar Selection
|
| 378 |
+
avatar_selection = st.selectbox(
|
| 379 |
+
"Choose Your Learning Buddy",
|
| 380 |
+
list(AVATAR_PERSONAS.keys())
|
| 381 |
+
)
|
| 382 |
|
| 383 |
+
# Initialize Companion
|
| 384 |
+
companion = LanguageLearningCompanion(avatar_selection)
|
| 385 |
|
| 386 |
+
# WebRTC Audio Capture
|
| 387 |
+
webrtc_ctx = webrtc_streamer(
|
| 388 |
+
key="language-companion",
|
| 389 |
+
mode=WebRtcMode.SENDRECV,
|
| 390 |
+
media_stream_constraints={"audio": True, "video": False}
|
| 391 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
|
| 393 |
+
if st.button("Transcribe and Respond"):
|
| 394 |
+
if webrtc_ctx.audio_receiver:
|
| 395 |
+
try:
|
| 396 |
+
# Placeholder for audio processing
|
| 397 |
+
# In a real implementation, you'd capture and process the audio
|
| 398 |
+
st.warning("Audio processing not fully implemented in this version")
|
| 399 |
+
except Exception as e:
|
| 400 |
+
logger.error(f"WebRTC audio processing error: {e}")
|
| 401 |
+
st.error("Error processing audio.")
|
| 402 |
|
| 403 |
if __name__ == "__main__":
|
| 404 |
main()
|