Blessin commited on
Commit
87da903
·
1 Parent(s): aa9f188

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -5
app.py CHANGED
@@ -1,7 +1,25 @@
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
  messages = [
7
  {"role": "system", "content": "You are a magical storyteller crafting a children's fantasy tale."},
@@ -14,17 +32,25 @@ def generate_story(names, story_title, api_key):
14
  )
15
 
16
  story = response.choices[0].message['content']
17
- return story
18
-
 
 
19
 
20
  iface = gr.Interface(
21
- fn=generate_story,
22
  inputs=[
23
  gr.components.Textbox(label="Enter names (comma-separated for multiple)"),
24
  gr.components.Textbox(label="Enter story title"),
25
  gr.components.Textbox(label="OpenAI API Key", type="password")
26
  ],
27
- outputs="text",
 
 
 
 
 
 
28
  live=True
29
  )
30
 
 
1
  import gradio as gr
2
  import openai
3
+ import requests
4
+ from PIL import Image
5
+ from io import BytesIO
6
 
7
+ def create_dalle_prompt(story_title, names):
8
+ return f"Fantasy illustration of '{story_title}' featuring {names} in a magical adventure."
9
+
10
+ def generate_image(api_key, prompt):
11
+ openai.api_key = api_key
12
+ response = openai.Image.create(
13
+ prompt=prompt,
14
+ n=1,
15
+ size="1024x1024"
16
+ )
17
+ image_url = response['data'][0]['url']
18
+ response = requests.get(image_url)
19
+ image = Image.open(BytesIO(response.content))
20
+ return image
21
+
22
+ def generate_story_and_images(names, story_title, api_key):
23
  openai.api_key = api_key
24
  messages = [
25
  {"role": "system", "content": "You are a magical storyteller crafting a children's fantasy tale."},
 
32
  )
33
 
34
  story = response.choices[0].message['content']
35
+ dalle_prompt = create_dalle_prompt(story_title, names)
36
+
37
+ images = [generate_image(api_key, f"{dalle_prompt} - Scene {i}") for i in range(1, 5)]
38
+ return story, *images
39
 
40
  iface = gr.Interface(
41
+ fn=generate_story_and_images,
42
  inputs=[
43
  gr.components.Textbox(label="Enter names (comma-separated for multiple)"),
44
  gr.components.Textbox(label="Enter story title"),
45
  gr.components.Textbox(label="OpenAI API Key", type="password")
46
  ],
47
+ outputs=[
48
+ "text",
49
+ gr.components.Image(label="Image 1", type="pil"),
50
+ gr.components.Image(label="Image 2", type="pil"),
51
+ gr.components.Image(label="Image 3", type="pil"),
52
+ gr.components.Image(label="Image 4", type="pil")
53
+ ],
54
  live=True
55
  )
56