Blessin commited on
Commit
23f26a5
·
1 Parent(s): c2956c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -13
app.py CHANGED
@@ -1,22 +1,50 @@
1
  import gradio as gr
 
2
 
3
- def create_story(noun1, adjective1, verb1, noun2, adjective2, verb2):
4
- story = (f"One day, a {adjective1} {noun1} {verb1} through the forest. "
5
- f"It came across a {adjective2} {noun2} and they quickly became friends. "
6
- f"Later, they both {verb2} happily ever after.")
7
- return story
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  iface = gr.Interface(
10
- fn=create_story,
11
  inputs=[
12
- gr.inputs.Textbox(label="Enter a noun"),
13
- gr.inputs.Textbox(label="Enter an adjective"),
14
- gr.inputs.Textbox(label="Enter a verb (past tense)"),
15
- gr.inputs.Textbox(label="Enter another noun"),
16
- gr.inputs.Textbox(label="Enter another adjective"),
17
- gr.inputs.Textbox(label="Enter another verb (past tense)")
 
 
 
 
18
  ],
19
- outputs="text",
20
  live=True
21
  )
22
 
 
1
  import gradio as gr
2
+ import openai
3
 
4
+ def generate_image(api_key, prompt):
5
+ # Initialize OpenAI with the provided API key
6
+ openai.api_key = api_key
7
+
8
+ # Generate the image
9
+ response = openai.Image.create(
10
+ prompt=prompt,
11
+ n=1,
12
+ size="1024x1024"
13
+ )
14
+ image_url = response['data'][0]['url']
15
+ return image_url
16
+
17
+ def generate_story(api_key, names, story_title):
18
+ # Initialize OpenAI with the provided API key
19
+ openai.api_key = api_key
20
+
21
+ # Generate the story
22
+ response = openai.Completion.create(
23
+ model="text-davinci-003",
24
+ prompt=f"Title: {story_title}\n\nIn a fantasy world, {names} embarked on an absurd and funny journey. {names}",
25
+ max_tokens=150
26
+ )
27
+ story = response.choices[0].text.strip()
28
+
29
+ # Generate 4 images inspired by the story title
30
+ images = [generate_image(api_key, f"Fantasy image inspired by {story_title} - Scene {i}") for i in range(1, 5)]
31
+
32
+ return story, *images
33
 
34
  iface = gr.Interface(
35
+ fn=generate_story,
36
  inputs=[
37
+ gr.inputs.Textbox(label="OpenAI API Key", type="password"),
38
+ gr.inputs.Textbox(label="Enter names (comma-separated for multiple)"),
39
+ gr.inputs.Textbox(label="Enter story title")
40
+ ],
41
+ outputs=[
42
+ "text",
43
+ gr.outputs.Image(label="Image 1"),
44
+ gr.outputs.Image(label="Image 2"),
45
+ gr.outputs.Image(label="Image 3"),
46
+ gr.outputs.Image(label="Image 4")
47
  ],
 
48
  live=True
49
  )
50