import streamlit as st from transformers import pipeline from gtts import gTTS import os # Load a safe, instruction-tuned language model @st.cache_resource(show_spinner="Loading language model...") def load_llm(): return pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1", tokenizer="mistralai/Mistral-7B-Instruct-v0.1", max_new_tokens=100, do_sample=True, temperature=0.7) llm = load_llm() # Convert response to speech def speak(text, filename="response.mp3"): tts = gTTS(text) tts.save(filename) audio_file = open(filename, "rb") audio_bytes = audio_file.read() st.audio(audio_bytes, format="audio/mp3") os.remove(filename) # UI setup st.set_page_config(page_title="AI Learning Buddy", page_icon="🧸") st.title("🧸 AI Learning Buddy for Kids (Ages 4–7)") st.markdown("Ask anything fun or educational — math, animals, colors, or stories!") # Input field user_input = st.text_input("Type your question:") if st.button("Ask the Buddy") and user_input: prompt = f"You are a kind, fun teacher for a 5-year-old child. Answer in a simple and cheerful tone.\n\nQuestion: {user_input}\nAnswer:" result = llm(prompt)[0]["generated_text"] answer = result.split("Answer:")[-1].strip() # Output st.markdown(f"**AI Buddy says:** {answer}") speak(answer)