Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
openai.api_key = os.environ.get('openai-api')
|
| 5 |
+
|
| 6 |
+
def generate_story(character1, character2, character3, character4, localizer, agefor, language):
|
| 7 |
+
agefor = min(max(agefor, 0), 20) # Ensure agefor is between 0 and 20
|
| 8 |
+
|
| 9 |
+
# Define the prompt
|
| 10 |
+
prompt = f'A story about {character1}, {character2}, {character3}, and {character4}, set in {localizer}.' \
|
| 11 |
+
f'The story is intended for a child aged {agefor}.'
|
| 12 |
+
|
| 13 |
+
# Define the request to the OpenAI API
|
| 14 |
+
response = openai.Completion.create(
|
| 15 |
+
engine="text-davinci-002",
|
| 16 |
+
prompt=prompt,
|
| 17 |
+
temperature=0.9,
|
| 18 |
+
max_tokens=1000
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Return the story
|
| 22 |
+
return response.choices[0].text.strip()
|
| 23 |
+
|
| 24 |
+
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown("Enter the details below and then click **Make me a story** to generate your story.")
|
| 26 |
+
|
| 27 |
+
with gr.Row():
|
| 28 |
+
character1 = gr.Textbox(label="Character 1")
|
| 29 |
+
character2 = gr.Textbox(label="Character 2")
|
| 30 |
+
character3 = gr.Textbox(label="Character 3")
|
| 31 |
+
character4 = gr.Textbox(label="Character 4")
|
| 32 |
+
with gr.Row():
|
| 33 |
+
content = gr.Textbox(label="What you want to hear about in this story", placeholder= "a giant elephant eating pink banana, a broken chair, ...")
|
| 34 |
+
with gr.Row():
|
| 35 |
+
localizer = gr.Textbox(placeholder="Location")
|
| 36 |
+
agefor = gr.Slider(minimum=0, maximum=20, step=1, default=10, label="Age of the child")
|
| 37 |
+
language = gr.Dropdown(choices=["Mandarin", "Cantonese", "French", "English", "Korean", "Italian", "German", "Russian"], label="Language of the story")
|
| 38 |
+
with gr.Row():
|
| 39 |
+
out = gr.Textbox()
|
| 40 |
+
|
| 41 |
+
btn = gr.Button("Make me a bed time story")
|
| 42 |
+
btn.click(fn=generate_story, inputs=[character1, character2, character3, character4, localizer, agefor, language], outputs=out)
|
| 43 |
+
|
| 44 |
+
demo.launch()
|
| 45 |
+
|