File size: 2,187 Bytes
d9dbb73
919fac1
f31b8c7
df4b649
919fac1
f31b8c7
919fac1
f689c94
d9dbb73
 
df4b649
d9dbb73
 
 
 
818da35
0c14c70
818da35
0c14c70
818da35
0c14c70
818da35
 
 
 
 
 
 
d9dbb73
f31b8c7
818da35
919fac1
818da35
d9dbb73
919fac1
 
df4b649
 
f10792d
 
 
5786b8d
f10792d
 
 
df4b649
 
919fac1
f689c94
919fac1
 
818da35
5b876e8
0060103
0c14c70
818da35
df4b649
 
 
 
818da35
df4b649
919fac1
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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()