Spaces:
Sleeping
Sleeping
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.") |