Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,10 @@ import openai
|
|
| 3 |
import requests
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
return f"
|
| 9 |
|
| 10 |
def generate_image(api_key, prompt):
|
| 11 |
openai.api_key = api_key
|
|
@@ -19,11 +20,11 @@ def generate_image(api_key, prompt):
|
|
| 19 |
image = Image.open(BytesIO(response.content))
|
| 20 |
return image
|
| 21 |
|
| 22 |
-
def generate_story_and_images(
|
| 23 |
openai.api_key = api_key
|
| 24 |
messages = [
|
| 25 |
{"role": "system", "content": "You are a magical storyteller crafting a children's fantasy tale."},
|
| 26 |
-
{"role": "user", "content": f"In the mystical land of Fantasia,
|
| 27 |
]
|
| 28 |
|
| 29 |
response = openai.ChatCompletion.create(
|
|
@@ -32,15 +33,20 @@ def generate_story_and_images(names, story_title, api_key):
|
|
| 32 |
)
|
| 33 |
|
| 34 |
story = response.choices[0].message['content']
|
| 35 |
-
dalle_prompt = create_dalle_prompt(story_title, names)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return story, *images
|
| 39 |
|
| 40 |
iface = gr.Interface(
|
| 41 |
fn=generate_story_and_images,
|
| 42 |
inputs=[
|
| 43 |
-
gr.components.Textbox(label="Enter
|
| 44 |
gr.components.Textbox(label="Enter story title"),
|
| 45 |
gr.components.Textbox(label="OpenAI API Key", type="password")
|
| 46 |
],
|
|
|
|
| 3 |
import requests
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
+
import textwrap
|
| 7 |
|
| 8 |
+
def create_dalle_prompts(story_title, paragraph):
|
| 9 |
+
return f"Illustration of '{story_title}' - {textwrap.shorten(paragraph, width=50)}"
|
| 10 |
|
| 11 |
def generate_image(api_key, prompt):
|
| 12 |
openai.api_key = api_key
|
|
|
|
| 20 |
image = Image.open(BytesIO(response.content))
|
| 21 |
return image
|
| 22 |
|
| 23 |
+
def generate_story_and_images(name, story_title, api_key):
|
| 24 |
openai.api_key = api_key
|
| 25 |
messages = [
|
| 26 |
{"role": "system", "content": "You are a magical storyteller crafting a children's fantasy tale."},
|
| 27 |
+
{"role": "user", "content": f"In the mystical land of Fantasia, a young adventurer named {name} sets out on a quest filled with magic and wonder. Tell us their whimsical and heartwarming tale based on the title: '{story_title}'."}
|
| 28 |
]
|
| 29 |
|
| 30 |
response = openai.ChatCompletion.create(
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
story = response.choices[0].message['content']
|
|
|
|
| 36 |
|
| 37 |
+
# Split story into four paragraphs
|
| 38 |
+
paragraphs = story.split('\n')[:4]
|
| 39 |
+
|
| 40 |
+
# Create DALLE prompts for each paragraph
|
| 41 |
+
dalle_prompts = [create_dalle_prompts(story_title, para) for para in paragraphs]
|
| 42 |
+
|
| 43 |
+
images = [generate_image(api_key, prompt) for prompt in dalle_prompts]
|
| 44 |
return story, *images
|
| 45 |
|
| 46 |
iface = gr.Interface(
|
| 47 |
fn=generate_story_and_images,
|
| 48 |
inputs=[
|
| 49 |
+
gr.components.Textbox(label="Enter name"),
|
| 50 |
gr.components.Textbox(label="Enter story title"),
|
| 51 |
gr.components.Textbox(label="OpenAI API Key", type="password")
|
| 52 |
],
|