Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import uuid
|
3 |
+
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()
|
10 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
11 |
+
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
12 |
+
|
13 |
+
# Initialize OpenAI and ElevenLabs clients
|
14 |
+
openai_client = OpenAI(api_key=OPENAI_API_KEY)
|
15 |
+
set_api_key(ELEVENLABS_API_KEY)
|
16 |
+
|
17 |
+
# Streamlit UI
|
18 |
+
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")
|
42 |
+
|
43 |
+
else:
|
44 |
+
st.warning("Please enter a topic to be explained.")
|