siviku commited on
Commit
c4f1406
·
verified ·
1 Parent(s): 1337293

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +11 -18
src/streamlit_app.py CHANGED
@@ -3,19 +3,16 @@ from transformers import pipeline
3
  from gtts import gTTS
4
  import os
5
 
6
- # Load a safe, instruction-tuned language model
7
  @st.cache_resource(show_spinner="Loading language model...")
8
  def load_llm():
9
- return pipeline("text-generation",
10
- model="mistralai/Mistral-7B-Instruct-v0.1",
11
- tokenizer="mistralai/Mistral-7B-Instruct-v0.1",
12
- max_new_tokens=100,
13
- do_sample=True,
14
- temperature=0.7)
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)
@@ -24,20 +21,16 @@ def speak(text, filename="response.mp3"):
24
  st.audio(audio_bytes, format="audio/mp3")
25
  os.remove(filename)
26
 
27
- # UI setup
28
  st.set_page_config(page_title="AI Learning Buddy", page_icon="🧸")
29
  st.title("🧸 AI Learning Buddy for Kids (Ages 4–7)")
30
 
31
- st.markdown("Ask anything fun or educational math, animals, colors, or stories!")
32
 
33
- # Input field
34
- user_input = st.text_input("Type your question:")
35
 
36
  if st.button("Ask the Buddy") and user_input:
37
- 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:"
38
  result = llm(prompt)[0]["generated_text"]
39
- answer = result.split("Answer:")[-1].strip()
40
-
41
- # Output
42
- st.markdown(f"**AI Buddy says:** {answer}")
43
- speak(answer)
 
3
  from gtts import gTTS
4
  import os
5
 
6
+ # Load a fully public language model
7
  @st.cache_resource(show_spinner="Loading language model...")
8
  def load_llm():
9
+ return pipeline("text2text-generation",
10
+ model="google/flan-t5-base",
11
+ tokenizer="google/flan-t5-base")
 
 
 
12
 
13
  llm = load_llm()
14
 
15
+ # Text-to-speech function
16
  def speak(text, filename="response.mp3"):
17
  tts = gTTS(text)
18
  tts.save(filename)
 
21
  st.audio(audio_bytes, format="audio/mp3")
22
  os.remove(filename)
23
 
24
+ # Streamlit UI
25
  st.set_page_config(page_title="AI Learning Buddy", page_icon="🧸")
26
  st.title("🧸 AI Learning Buddy for Kids (Ages 4–7)")
27
 
28
+ st.markdown("Ask your question below and the AI Buddy will answer in a fun, friendly voice!")
29
 
30
+ user_input = st.text_input("Type your question (e.g., What's 2 + 3?):")
 
31
 
32
  if st.button("Ask the Buddy") and user_input:
33
+ prompt = f"Explain to a 5-year-old: {user_input}"
34
  result = llm(prompt)[0]["generated_text"]
35
+ st.markdown(f"**AI Buddy says:** {result}")
36
+ speak(result)