Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,70 @@
|
|
| 1 |
-
import openai
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Use GPT-3.5 Turbo to get the response
|
| 12 |
-
response = openai.Completion.create(
|
| 13 |
-
engine="gpt-3.5-turbo",
|
| 14 |
prompt=prompt,
|
| 15 |
-
|
|
|
|
| 16 |
)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return
|
| 21 |
-
|
| 22 |
-
# Gradio Interface
|
| 23 |
-
def gradio_interface():
|
| 24 |
-
# Define input and output components for Gradio
|
| 25 |
-
input_component = gr.inputs.Textbox(lines=5, placeholder="Enter your question to God...")
|
| 26 |
-
output_component = gr.outputs.Textbox(label="God's Reply")
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
gradio_interface()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 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
|
| 13 |
+
response = openai.Image.create(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
prompt=prompt,
|
| 15 |
+
n=1,
|
| 16 |
+
size="1024x1024"
|
| 17 |
)
|
| 18 |
+
image_url = response['data'][0]['url']
|
| 19 |
+
response = requests.get(image_url)
|
| 20 |
+
image = Image.open(BytesIO(response.content))
|
| 21 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def generate_story_and_images(name, story_title, story_type, api_key):
|
| 24 |
+
openai.api_key = api_key
|
| 25 |
+
if story_type == "for children":
|
| 26 |
+
prompt_content = f"In a mystical kingdom, a brave adventurer named {name} discovers a magical object. Narrate their whimsical and heartwarming tale based on the title: '{story_title}'."
|
| 27 |
+
else:
|
| 28 |
+
prompt_content = f"In an odd city, {name} stumbles upon a series of bizarre events linked to the title '{story_title}'. Describe their hilariously absurd adventure."
|
| 29 |
+
|
| 30 |
+
messages = [
|
| 31 |
+
{"role": "system", "content": "You are a magical storyteller."},
|
| 32 |
+
{"role": "user", "content": prompt_content}
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
response = openai.ChatCompletion.create(
|
| 36 |
+
model="gpt-3.5-turbo",
|
| 37 |
+
messages=messages
|
| 38 |
)
|
| 39 |
+
|
| 40 |
+
story = response.choices[0].message['content']
|
| 41 |
+
|
| 42 |
+
# Split story into four paragraphs
|
| 43 |
+
paragraphs = story.split('\n')[:4]
|
| 44 |
+
|
| 45 |
+
# Create DALLE prompts for each paragraph
|
| 46 |
+
dalle_prompts = [create_dalle_prompts(story_title, para) for para in paragraphs]
|
| 47 |
+
|
| 48 |
+
images = [generate_image(api_key, prompt) for prompt in dalle_prompts]
|
| 49 |
+
return story, dalle_prompts, *images
|
| 50 |
|
| 51 |
+
iface = gr.Interface(
|
| 52 |
+
fn=generate_story_and_images,
|
| 53 |
+
inputs=[
|
| 54 |
+
gr.components.Textbox(label="Enter name (Example: Ella)"),
|
| 55 |
+
gr.components.Textbox(label="Enter story title (Example: The Enchanted Locket)"),
|
| 56 |
+
gr.components.Radio(["for children", "absurd and humorous"], label="Story Type"),
|
| 57 |
+
gr.components.Textbox(label="OpenAI API Key", type="password")
|
| 58 |
+
],
|
| 59 |
+
outputs=[
|
| 60 |
+
"text",
|
| 61 |
+
"text",
|
| 62 |
+
gr.components.Image(label="Image 1", type="pil"),
|
| 63 |
+
gr.components.Image(label="Image 2", type="pil"),
|
| 64 |
+
gr.components.Image(label="Image 3", type="pil"),
|
| 65 |
+
gr.components.Image(label="Image 4", type="pil")
|
| 66 |
+
],
|
| 67 |
+
live=True
|
| 68 |
+
)
|
| 69 |
|
| 70 |
+
iface.launch()
|
|
|