Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,50 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
iface = gr.Interface(
|
| 10 |
-
fn=
|
| 11 |
inputs=[
|
| 12 |
-
gr.inputs.Textbox(label="
|
| 13 |
-
gr.inputs.Textbox(label="Enter
|
| 14 |
-
gr.inputs.Textbox(label="Enter
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|