Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
-
import requests
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from io import BytesIO
|
| 6 |
|
| 7 |
-
def generate_story(
|
| 8 |
openai.api_key = api_key
|
| 9 |
response = openai.Completion.create(
|
| 10 |
engine="gpt-3.5-turbo",
|
|
@@ -14,46 +11,14 @@ def generate_story(api_key, names, story_title):
|
|
| 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(
|
| 28 |
-
prompt=prompt,
|
| 29 |
-
n=1,
|
| 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=
|
| 45 |
inputs=[
|
| 46 |
-
gr.components.Textbox(label="OpenAI API Key", type="password"),
|
| 47 |
gr.components.Textbox(label="Enter names (comma-separated for multiple)"),
|
| 48 |
-
gr.components.Textbox(label="Enter story title")
|
| 49 |
-
|
| 50 |
-
outputs=[
|
| 51 |
-
"text",
|
| 52 |
-
gr.components.Image(label="Image 1", type="pil"),
|
| 53 |
-
gr.components.Image(label="Image 2", type="pil"),
|
| 54 |
-
gr.components.Image(label="Image 3", type="pil"),
|
| 55 |
-
gr.components.Image(label="Image 4", type="pil")
|
| 56 |
],
|
|
|
|
| 57 |
live=True
|
| 58 |
)
|
| 59 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def generate_story(names, story_title, api_key):
|
| 5 |
openai.api_key = api_key
|
| 6 |
response = openai.Completion.create(
|
| 7 |
engine="gpt-3.5-turbo",
|
|
|
|
| 11 |
story = response.choices[0].text.strip()
|
| 12 |
return story
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
iface = gr.Interface(
|
| 15 |
+
fn=generate_story,
|
| 16 |
inputs=[
|
|
|
|
| 17 |
gr.components.Textbox(label="Enter names (comma-separated for multiple)"),
|
| 18 |
+
gr.components.Textbox(label="Enter story title"),
|
| 19 |
+
gr.components.Textbox(label="OpenAI API Key", type="password")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
],
|
| 21 |
+
outputs="text",
|
| 22 |
live=True
|
| 23 |
)
|
| 24 |
|