Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,24 @@ import requests
|
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def generate_image(api_key, prompt):
|
| 8 |
openai.api_key = api_key
|
| 9 |
response = openai.Image.create(
|
|
@@ -12,33 +30,18 @@ def generate_image(api_key, prompt):
|
|
| 12 |
size="1024x1024"
|
| 13 |
)
|
| 14 |
image_url = response['data'][0]['url']
|
| 15 |
-
|
| 16 |
-
# Download the image and open it with PIL
|
| 17 |
response = requests.get(image_url)
|
| 18 |
image = Image.open(BytesIO(response.content))
|
| 19 |
-
|
| 20 |
return image
|
| 21 |
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
openai.api_key = api_key
|
| 27 |
-
response = openai.Completion.create(
|
| 28 |
-
model="gpt-3.5-turbo",
|
| 29 |
-
prompt=f"Title: {story_title}\n\nIn a fantasy world, {names} embarked on an absurd and funny journey. {names}",
|
| 30 |
-
max_tokens=150
|
| 31 |
-
)
|
| 32 |
-
story = response.choices[0].text.strip()
|
| 33 |
-
|
| 34 |
-
# Create a DALLE prompt using the story
|
| 35 |
-
dalle_prompt = create_dalle_prompt(story, story_title)
|
| 36 |
-
images = [generate_image(api_key, f"{dalle_prompt} - Scene {i}") for i in range(1, 5)]
|
| 37 |
-
|
| 38 |
return story, *images
|
| 39 |
|
| 40 |
iface = gr.Interface(
|
| 41 |
-
fn=
|
| 42 |
inputs=[
|
| 43 |
gr.components.Textbox(label="OpenAI API Key", type="password"),
|
| 44 |
gr.components.Textbox(label="Enter names (comma-separated for multiple)"),
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
+
def generate_story(api_key, names, story_title):
|
| 8 |
+
openai.api_key = api_key
|
| 9 |
+
response = openai.Completion.create(
|
| 10 |
+
engine="gpt-3.5-turbo",
|
| 11 |
+
prompt=f"Title: {story_title}\n\nIn a fantasy world, {names} embarked on an absurd and funny journey. {names}",
|
| 12 |
+
max_tokens=150
|
| 13 |
+
)
|
| 14 |
+
story = response.choices[0].text.strip()
|
| 15 |
+
return story
|
| 16 |
+
|
| 17 |
+
def create_dalle_prompt(story, story_title):
|
| 18 |
+
return f"Visual representation of the fantasy story titled '{story_title}': {story}"
|
| 19 |
+
|
| 20 |
+
def generate_dalle_images(api_key, prompt):
|
| 21 |
+
openai.api_key = api_key
|
| 22 |
+
images = [generate_image(api_key, f"{prompt} - Scene {i}") for i in range(1, 5)]
|
| 23 |
+
return images
|
| 24 |
+
|
| 25 |
def generate_image(api_key, prompt):
|
| 26 |
openai.api_key = api_key
|
| 27 |
response = openai.Image.create(
|
|
|
|
| 30 |
size="1024x1024"
|
| 31 |
)
|
| 32 |
image_url = response['data'][0]['url']
|
|
|
|
|
|
|
| 33 |
response = requests.get(image_url)
|
| 34 |
image = Image.open(BytesIO(response.content))
|
|
|
|
| 35 |
return image
|
| 36 |
|
| 37 |
+
def main(api_key, names, story_title):
|
| 38 |
+
story = generate_story(api_key, names, story_title)
|
| 39 |
+
prompt = create_dalle_prompt(story, story_title)
|
| 40 |
+
images = generate_dalle_images(api_key, prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return story, *images
|
| 42 |
|
| 43 |
iface = gr.Interface(
|
| 44 |
+
fn=main,
|
| 45 |
inputs=[
|
| 46 |
gr.components.Textbox(label="OpenAI API Key", type="password"),
|
| 47 |
gr.components.Textbox(label="Enter names (comma-separated for multiple)"),
|