Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
from openai import OpenAI | |
import tempfile | |
client = OpenAI(api_key=os.environ['OPENAI_API_KEY']) | |
def generate_story(password, topic, language): | |
if password != os.environ.get("PASSWORD"): | |
return "Incorrect password!", None | |
prompt = f"""You are a professional storyteller. Your task is to take any given topic and creatively transform it | |
into an engaging, captivating, and imaginative fairy tale, preserving the core elements and characteristics of the | |
original subject. Your fairy tale should have a clear structure: | |
Set the scene, describe the setting, and introduce the main characters. | |
Narrate the adventures, challenges, or conflicts faced by the characters. | |
Present a resolution or moral lesson derived from the characters' experiences. | |
Use a vivid, fluid, and warm narrative style that draws the audience into the story. | |
Now, create a fairy tale based on the following topic in {language}: | |
Topic: {topic} | |
""" | |
response = client.chat.completions.create( | |
model="gpt-4.5-preview", | |
messages=[{"role": "user", "content": prompt}], | |
max_tokens=2048, | |
temperature=0.5 | |
) | |
story = response.choices[0].message.content.strip() | |
audio_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") | |
with client.audio.speech.with_streaming_response.create( | |
model="gpt-4o-mini-tts", | |
voice="fable", | |
input=story, | |
) as tts_response: | |
tts_response.stream_to_file(audio_file.name) | |
return story, audio_file.name | |
iface = gr.Interface( | |
fn=generate_story, | |
inputs=[ | |
gr.Textbox(label="Password", type="password", placeholder="Enter your password..."), | |
gr.Textbox(label="Story Topic", placeholder="e.g., The adventures of a brave rabbit..."), | |
gr.Radio(label="Language", choices=["French", "English", "Turkish"], value="French") | |
], | |
outputs=[ | |
gr.Textbox(label="Generated Story"), | |
gr.Audio(label="Listen to the Story", type="filepath") | |
], | |
title="✨ UmaiMother ✨", | |
description="Please enter the correct password, specify the story topic, and choose a language." | |
) | |
iface.launch() | |