File size: 1,560 Bytes
977f070
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import uuid
import streamlit as st
from dotenv import load_dotenv
from openai import OpenAI
from elevenlabs import generate, play, set_api_key

# Load API keys
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")

# Initialize OpenAI and ElevenLabs clients
openai_client = OpenAI(api_key=OPENAI_API_KEY)
set_api_key(ELEVENLABS_API_KEY)

# Streamlit UI
st.title("Explain Like I'm Five (with Voice!)")

topic = st.text_input("What do you want me to explain like you're five?", "")

if st.button("Explain with Voice"):
    if topic:
        with st.spinner(f"Asking my smart brain to explain '{topic}' simply..."):
            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."
            response = openai_client.chat.completions.create(
                model="gpt-4o",
                messages=[
                    {"role": "user", "content": prompt}
                ]
            )
            explanation = response.choices[0].message.content

        st.subheader("Here's how a five-year-old might understand it:")
        st.info(explanation)

        with st.spinner("Generating the voice for the explanation..."):
            audio = generate(text=explanation, voice="Bella", model="eleven_multilingual_v2")

        st.subheader("Listen to the explanation:")
        st.audio(audio, format="audio/mpeg")

    else:
        st.warning("Please enter a topic to be explained.")