Update src/streamlit_app.py
Browse files- src/streamlit_app.py +5 -27
src/streamlit_app.py
CHANGED
@@ -3,15 +3,7 @@ from transformers import pipeline
|
|
3 |
from gtts import gTTS
|
4 |
import os
|
5 |
|
6 |
-
# Load
|
7 |
-
@st.cache_data(show_spinner="Loading Whisper model...")
|
8 |
-
def load_whisper():
|
9 |
-
import whisper
|
10 |
-
return whisper.load_model("tiny")
|
11 |
-
|
12 |
-
asr_model = load_whisper()
|
13 |
-
|
14 |
-
# Load a small instruction-tuned model for child-safe answers
|
15 |
@st.cache_resource(show_spinner="Loading language model...")
|
16 |
def load_llm():
|
17 |
return pipeline("text-generation",
|
@@ -23,7 +15,7 @@ def load_llm():
|
|
23 |
|
24 |
llm = load_llm()
|
25 |
|
26 |
-
# Convert
|
27 |
def speak(text, filename="response.mp3"):
|
28 |
tts = gTTS(text)
|
29 |
tts.save(filename)
|
@@ -36,26 +28,12 @@ def speak(text, filename="response.mp3"):
|
|
36 |
st.set_page_config(page_title="AI Learning Buddy", page_icon="🧸")
|
37 |
st.title("🧸 AI Learning Buddy for Kids (4–7)")
|
38 |
|
39 |
-
st.markdown("Ask a question
|
40 |
-
|
41 |
-
input_type = st.radio("Choose input method:", ["Text", "Voice"])
|
42 |
-
|
43 |
-
user_input = ""
|
44 |
|
45 |
-
|
46 |
-
user_input = st.text_input("Type your question here:")
|
47 |
-
else:
|
48 |
-
audio_file = st.file_uploader("Upload a voice file (wav/mp3)", type=["wav", "mp3"])
|
49 |
-
if audio_file:
|
50 |
-
with open("temp_audio.wav", "wb") as f:
|
51 |
-
f.write(audio_file.read())
|
52 |
-
result = asr_model.transcribe("temp_audio.wav")
|
53 |
-
user_input = result["text"]
|
54 |
-
st.success(f"You said: {user_input}")
|
55 |
-
os.remove("temp_audio.wav")
|
56 |
|
57 |
if st.button("Ask the Buddy") and user_input:
|
58 |
-
prompt = f"You are a
|
59 |
result = llm(prompt)[0]["generated_text"]
|
60 |
answer = result.split("Answer:")[-1].strip()
|
61 |
st.markdown(f"**AI Buddy says:** {answer}")
|
|
|
3 |
from gtts import gTTS
|
4 |
import os
|
5 |
|
6 |
+
# Load small language model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
@st.cache_resource(show_spinner="Loading language model...")
|
8 |
def load_llm():
|
9 |
return pipeline("text-generation",
|
|
|
15 |
|
16 |
llm = load_llm()
|
17 |
|
18 |
+
# Convert response to speech
|
19 |
def speak(text, filename="response.mp3"):
|
20 |
tts = gTTS(text)
|
21 |
tts.save(filename)
|
|
|
28 |
st.set_page_config(page_title="AI Learning Buddy", page_icon="🧸")
|
29 |
st.title("🧸 AI Learning Buddy for Kids (4–7)")
|
30 |
|
31 |
+
st.markdown("Ask a question and let the AI Buddy respond in a fun, friendly voice!")
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
user_input = st.text_input("Type your question here (e.g. What is 2 + 3?):")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
if st.button("Ask the Buddy") and user_input:
|
36 |
+
prompt = f"You are a friendly teacher for a 5-year-old. Question: {user_input} Answer:"
|
37 |
result = llm(prompt)[0]["generated_text"]
|
38 |
answer = result.split("Answer:")[-1].strip()
|
39 |
st.markdown(f"**AI Buddy says:** {answer}")
|