import streamlit as st from transformers import pipeline from gtts import gTTS import os # Load a fully public language model @st.cache_resource(show_spinner="Loading language model...") def load_llm(): return pipeline("text2text-generation", model="google/flan-t5-base", tokenizer="google/flan-t5-base") llm = load_llm() # Text-to-speech function 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) # Streamlit UI st.set_page_config(page_title="AI Learning Buddy", page_icon="🧸") st.title("🧸 AI Learning Buddy for Kids (Ages 4–7)") st.markdown("Ask your question below and the AI Buddy will answer in a fun, friendly voice!") user_input = st.text_input("Type your question (e.g., What's 2 + 3?):") if st.button("Ask the Buddy") and user_input: prompt = f"Explain to a 5-year-old: {user_input}" result = llm(prompt)[0]["generated_text"] st.markdown(f"**AI Buddy says:** {result}") speak(result)