demo / src /streamlit_app.py
siviku's picture
Update src/streamlit_app.py
edebd7a verified
raw
history blame
2.03 kB
import streamlit as st
from transformers import pipeline
from gtts import gTTS
import os
# Load Whisper model (tiny for lower memory use)
@st.cache_data(show_spinner="Loading Whisper model...")
def load_whisper():
import whisper
return whisper.load_model("tiny")
asr_model = load_whisper()
# Load a small instruction-tuned model for child-safe answers
@st.cache_resource(show_spinner="Loading language model...")
def load_llm():
return pipeline("text-generation",
model="tiiuae/falcon-7b-instruct",
tokenizer="tiiuae/falcon-7b-instruct",
max_new_tokens=100,
do_sample=True,
temperature=0.7)
llm = load_llm()
# Convert AI 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
st.set_page_config(page_title="AI Learning Buddy", page_icon="🧸")
st.title("🧸 AI Learning Buddy for Kids (4–7)")
st.markdown("Ask a question by typing or uploading your voice.")
input_type = st.radio("Choose input method:", ["Text", "Voice"])
user_input = ""
if input_type == "Text":
user_input = st.text_input("Type your question here:")
else:
audio_file = st.file_uploader("Upload a voice file (wav/mp3)", type=["wav", "mp3"])
if audio_file:
with open("temp_audio.wav", "wb") as f:
f.write(audio_file.read())
result = asr_model.transcribe("temp_audio.wav")
user_input = result["text"]
st.success(f"You said: {user_input}")
os.remove("temp_audio.wav")
if st.button("Ask the Buddy") and user_input:
prompt = f"You are a fun and friendly teacher for a 5-year-old. Question: {user_input} Answer:"
result = llm(prompt)[0]["generated_text"]
answer = result.split("Answer:")[-1].strip()
st.markdown(f"**AI Buddy says:** {answer}")
speak(answer)