File size: 5,419 Bytes
cc8640e
60d035f
928626e
8da67e2
60d035f
642c2b0
1ac55e5
 
cc8640e
 
 
 
 
 
 
8da67e2
 
 
 
60d035f
 
 
cc8640e
 
 
60d035f
0a48ded
 
74fa26b
 
 
 
 
 
 
cc8640e
 
 
 
 
 
 
8da67e2
cc8640e
 
 
 
 
 
 
 
 
 
 
 
 
8da67e2
cc8640e
 
8da67e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60d035f
 
 
 
1956df9
 
 
 
 
 
 
 
 
 
 
 
60d035f
 
 
 
cc8640e
 
c2e34cb
cc8640e
 
 
 
0a48ded
cc8640e
1ac55e5
 
 
 
 
 
 
cc8640e
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import streamlit as st
from transformers import pipeline, AutoModelForCTC, AutoProcessor, Wav2Vec2ForCTC
import pyttsx3
import random
import torchaudio
import torch  # Add this import statement to fix the NameError
from gtts import gTTS
import os

# Set up the app
st.set_page_config(page_title="ChiliDron", layout="wide", page_icon="🎨")

# Load Hugging Face models using cache
@st.cache_resource
def load_storytelling_model():
    return pipeline("text-generation", model="bookbot/gpt2-indo-medium-kids-stories")

@st.cache_resource
def load_phoneme_model():
    model = Wav2Vec2ForCTC.from_pretrained("mirfan899/kids_phoneme_sm_model")
    processor = AutoProcessor.from_pretrained("mirfan899/kids_phoneme_sm_model")
    return model, processor

# Load models
story_generator = load_storytelling_model()
phoneme_model, phoneme_processor = load_phoneme_model()

# Initialize TTS engine
def initialize_tts():
    try:
        engine = pyttsx3.init()
        return engine
    except Exception as e:
        st.error(f"Error initializing text-to-speech engine: {e}")
        return None

# App title and description
st.title("ChiliDron")
st.subheader("A fun, interactive, and educational app for kids!")

# Sidebar navigation
st.sidebar.title("Navigate")
options = st.sidebar.radio("Go to", ["Interactive Storyteller", "Joke and Fun Facts Generator", "Phoneme Practice", "Guided Meditation"])

# Interactive Storyteller
if options == "Interactive Storyteller":
    st.header("📝 Interactive Storyteller")
    st.write("Create your own story by selecting characters and plot points!")

    character = st.selectbox("Choose a character:", ["A Brave Knight", "A Clever Princess", "A Funny Alien", "A Curious Scientist"])
    setting = st.selectbox("Choose a setting:", ["In a magical forest", "On a distant planet", "In an ancient castle", "At a science lab"])
    plot = st.selectbox("Choose a plot point:", ["Finds a mysterious map", "Discovers a secret passage", "Meets a talking animal", "Invents a cool gadget"])

    if st.button("Create Story"):
        with st.spinner("Generating your story..."):
            prompt = f"Once upon a time, {character} was {setting} when they {plot}. And then,"
            story = story_generator(prompt, max_length=500, num_return_sequences=1)
            st.success(story[0]['generated_text'])

# Joke and Fun Facts Generator
elif options == "Joke and Fun Facts Generator":
    st.header("🤣 Joke and Fun Facts Generator")
    st.write("Get ready to laugh and learn with random jokes and fun facts!")

    jokes = [
        "Why did the scarecrow win an award? Because he was outstanding in his field!",
        "Why don’t scientists trust atoms? Because they make up everything!",
        "What do you get if you cross a vampire with a snowman? Frostbite!"
    ]

    fun_facts = [
        "Did you know that honey never spoils? Archaeologists have found pots of honey in ancient Egyptian tombs that are over 3,000 years old and still perfectly edible.",
        "A day on Venus is longer than a year on Venus! It takes about 243 Earth days for Venus to rotate once, but only 225 Earth days to orbit the Sun.",
        "Octopuses have three hearts and blue blood!"
    ]

    if st.button("Tell me a joke!"):
        st.write(random.choice(jokes))

    if st.button("Tell me a fun fact!"):
        st.write(random.choice(fun_facts))

# Phoneme Practice
elif options == "Phoneme Practice":
    st.header("🔤 Phoneme Practice")
    st.write("Learn how to pronounce words correctly!")

    uploaded_audio = st.file_uploader("Upload an audio file to analyze phonemes", type=["wav", "mp3"])

    if uploaded_audio:
        with st.spinner("Analyzing phonemes..."):
            waveform, sample_rate = torchaudio.load(uploaded_audio)

            # Ensure mono audio by averaging channels if stereo
            if waveform.shape[0] > 1:
                waveform = waveform.mean(dim=0, keepdim=True)
            
            # Resample if needed
            if sample_rate != 16000:
                resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=16000)
                waveform = resampler(waveform)

            input_values = phoneme_processor(waveform.squeeze(), return_tensors="pt", sampling_rate=16000).input_values
            logits = phoneme_model(input_values).logits
            predicted_ids = torch.argmax(logits, dim=-1)
            transcription = phoneme_processor.batch_decode(predicted_ids)
            st.write(f"Transcription: {transcription[0]}")

# Guided Meditation for Kids
if options == "Guided Meditation":
    st.header("🧘‍♂️ Guided Meditation for Kids")
    st.write("Relax and listen to a calming meditation.")

    meditation_text = "Close your eyes and take a deep breath. Imagine you are in a peaceful forest, with birds singing and leaves rustling in the wind."

    if st.button("Play Meditation"):
        with st.spinner("Generating meditation audio..."):
            try:
                tts = gTTS(text=meditation_text, lang='en')
                tts.save('meditation.mp3')
                st.audio('meditation.mp3', format='audio/mp3')
            except Exception as e:
                st.error(f"Error generating or playing the audio: {e}")

# Footer
st.markdown("<footer style='text-align: center; padding-top: 20px;'>Made with ❤️ for Kids by ChiliDron Team</footer>", unsafe_allow_html=True)