Bonosa2 commited on
Commit
f7081da
·
verified ·
1 Parent(s): 977f070

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -10
app.py CHANGED
@@ -4,6 +4,8 @@ import streamlit as st
4
  from dotenv import load_dotenv
5
  from openai import OpenAI
6
  from elevenlabs import generate, play, set_api_key
 
 
7
 
8
  # Load API keys
9
  load_dotenv()
@@ -19,23 +21,42 @@ st.title("Explain Like I'm Five (with Voice!)")
19
 
20
  topic = st.text_input("What do you want me to explain like you're five?", "")
21
 
 
 
 
 
 
 
22
  if st.button("Explain with Voice"):
23
  if topic:
 
 
 
 
24
  with st.spinner(f"Asking my smart brain to explain '{topic}' simply..."):
25
  prompt = f"Explain {topic} to a five-year-old using very simple words, short sentences, and a fun example or analogy that a young child can easily understand."
26
- response = openai_client.chat.completions.create(
27
- model="gpt-4o",
28
- messages=[
29
- {"role": "user", "content": prompt}
30
- ]
31
- )
32
- explanation = response.choices[0].message.content
33
 
34
- st.subheader("Here's how a five-year-old might understand it:")
35
- st.info(explanation)
 
 
 
 
 
 
 
 
 
36
 
37
  with st.spinner("Generating the voice for the explanation..."):
38
- audio = generate(text=explanation, voice="Bella", model="eleven_multilingual_v2")
39
 
40
  st.subheader("Listen to the explanation:")
41
  st.audio(audio, format="audio/mpeg")
 
4
  from dotenv import load_dotenv
5
  from openai import OpenAI
6
  from elevenlabs import generate, play, set_api_key
7
+ import json
8
+ import requests
9
 
10
  # Load API keys
11
  load_dotenv()
 
21
 
22
  topic = st.text_input("What do you want me to explain like you're five?", "")
23
 
24
+ def stream_openai_response(payload, headers):
25
+ with requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload, stream=True) as r:
26
+ for line in r.iter_lines():
27
+ if line and line.startswith(b"data: "):
28
+ yield line[len(b"data: "):].decode()
29
+
30
  if st.button("Explain with Voice"):
31
  if topic:
32
+ st.subheader("Here's how a five-year-old might understand it:")
33
+ explanation_box = st.empty()
34
+ full_explanation = ""
35
+
36
  with st.spinner(f"Asking my smart brain to explain '{topic}' simply..."):
37
  prompt = f"Explain {topic} to a five-year-old using very simple words, short sentences, and a fun example or analogy that a young child can easily understand."
38
+ headers = {"Authorization": f"Bearer {OPENAI_API_KEY}"}
39
+ payload = {
40
+ "model": "gpt-4o",
41
+ "messages": [{"role": "user", "content": prompt}],
42
+ "temperature": 0.7,
43
+ "stream": True
44
+ }
45
 
46
+ for chunk in stream_openai_response(payload, headers):
47
+ if chunk.strip() == "[DONE]":
48
+ break
49
+ try:
50
+ parsed = json.loads(chunk)
51
+ delta = parsed['choices'][0]['delta'].get('content', '')
52
+ full_explanation += delta
53
+ explanation_box.markdown(full_explanation)
54
+ except json.JSONDecodeError:
55
+ st.warning(f"Non-JSON chunk received: {chunk}")
56
+ continue
57
 
58
  with st.spinner("Generating the voice for the explanation..."):
59
+ audio = generate(text=full_explanation, voice="Bella", model="eleven_multilingual_v2")
60
 
61
  st.subheader("Listen to the explanation:")
62
  st.audio(audio, format="audio/mpeg")