File size: 1,430 Bytes
1902db3
a715ccc
 
 
 
f30b5e5
edebd7a
a715ccc
edebd7a
f30b5e5
 
a715ccc
 
 
edebd7a
a715ccc
 
0286dd1
a715ccc
 
 
 
 
 
 
 
f30b5e5
a715ccc
f30b5e5
a715ccc
f30b5e5
a715ccc
f30b5e5
 
a715ccc
 
f30b5e5
a715ccc
 
f30b5e5
 
a715ccc
edebd7a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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)