siviku commited on
Commit
4c190fa
·
verified ·
1 Parent(s): 9becbf3

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +14 -11
src/streamlit_app.py CHANGED
@@ -3,33 +3,36 @@ from transformers import pipeline
3
  from gtts import gTTS
4
  import os
5
 
6
- # Load small public instruction model
7
  @st.cache_resource(show_spinner="Loading AI Buddy...")
8
  def load_llm():
9
- return pipeline("text2text-generation",
10
- model="google/flan-t5-small",
11
- tokenizer="google/flan-t5-small")
 
 
 
12
 
13
  llm = load_llm()
14
 
15
- # Text-to-speech
16
  def speak(text, filename="response.mp3"):
17
  tts = gTTS(text)
18
  tts.save(filename)
19
  audio_file = open(filename, "rb")
20
- st.audio(audio_bytes := audio_file.read(), format="audio/mp3")
21
  os.remove(filename)
22
 
23
  # Streamlit UI
24
  st.set_page_config(page_title="AI Learning Buddy", page_icon="🧸")
25
  st.title("🧸 AI Learning Buddy (Ages 4–7)")
 
26
 
27
- st.markdown("Type a fun question and hear the AI Buddy reply!")
28
-
29
- user_input = st.text_input("Ask your question:")
30
 
31
  if st.button("Ask the Buddy") and user_input:
32
  prompt = f"Explain to a 5-year-old: {user_input}"
33
  result = llm(prompt)[0]["generated_text"]
34
- st.markdown(f"**AI Buddy says:** {result}")
35
- speak(result)
 
 
3
  from gtts import gTTS
4
  import os
5
 
6
+ # Load a public, PyTorch-compatible, conversational model
7
  @st.cache_resource(show_spinner="Loading AI Buddy...")
8
  def load_llm():
9
+ return pipeline("text-generation",
10
+ model="declare-lab/flan-alpaca-base",
11
+ tokenizer="declare-lab/flan-alpaca-base",
12
+ max_new_tokens=100,
13
+ do_sample=True,
14
+ temperature=0.7)
15
 
16
  llm = load_llm()
17
 
18
+ # Text-to-speech function
19
  def speak(text, filename="response.mp3"):
20
  tts = gTTS(text)
21
  tts.save(filename)
22
  audio_file = open(filename, "rb")
23
+ st.audio(audio_file.read(), format="audio/mp3")
24
  os.remove(filename)
25
 
26
  # Streamlit UI
27
  st.set_page_config(page_title="AI Learning Buddy", page_icon="🧸")
28
  st.title("🧸 AI Learning Buddy (Ages 4–7)")
29
+ st.markdown("Ask anything fun or educational and hear your buddy talk!")
30
 
31
+ user_input = st.text_input("What would you like to ask?")
 
 
32
 
33
  if st.button("Ask the Buddy") and user_input:
34
  prompt = f"Explain to a 5-year-old: {user_input}"
35
  result = llm(prompt)[0]["generated_text"]
36
+ answer = result.split(":")[-1].strip()
37
+ st.markdown(f"**AI Buddy says:** {answer}")
38
+ speak(answer)