Spaces:
Sleeping
Sleeping
ageraustine
commited on
Commit
•
2c703f6
1
Parent(s):
fa282c1
use DALLE-3
Browse files
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
-
from langchain.llms import OpenAI
|
3 |
from langchain.prompts import PromptTemplate
|
4 |
from langchain.chains import LLMChain
|
5 |
-
import
|
6 |
import os
|
7 |
import re
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
|
12 |
# Create a prompt template
|
13 |
template = """
|
@@ -22,17 +22,19 @@ Story:
|
|
22 |
prompt = PromptTemplate(template=template, input_variables=["topic"])
|
23 |
|
24 |
# Create an LLMChain
|
25 |
-
llm =
|
26 |
story_chain = LLMChain(llm=llm, prompt=prompt)
|
27 |
|
28 |
def generate_image(prompt):
|
29 |
try:
|
30 |
-
response =
|
|
|
31 |
prompt=prompt,
|
|
|
|
|
32 |
n=1,
|
33 |
-
size="512x512"
|
34 |
)
|
35 |
-
return response
|
36 |
except Exception as e:
|
37 |
print(f"Error generating image: {e}")
|
38 |
return None
|
@@ -49,26 +51,26 @@ def generate_story_with_images(topic):
|
|
49 |
while len(paragraphs) < 3:
|
50 |
paragraphs.append("...")
|
51 |
|
52 |
-
# Generate
|
53 |
-
|
54 |
for paragraph in paragraphs:
|
55 |
image_url = generate_image(paragraph)
|
56 |
-
|
57 |
|
58 |
-
return
|
59 |
|
60 |
# Create the Gradio interface
|
61 |
def gradio_interface(topic):
|
62 |
try:
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
except Exception as e:
|
70 |
print(f"Error in gradio_interface: {e}")
|
71 |
-
return ["An error occurred while generating the story."] + [""] * 5
|
72 |
|
73 |
iface = gr.Interface(
|
74 |
fn=gradio_interface,
|
@@ -82,7 +84,7 @@ iface = gr.Interface(
|
|
82 |
gr.Image(label="Image 3", type="filepath"),
|
83 |
],
|
84 |
title="Story Generator with Images",
|
85 |
-
description="Enter a topic, and the AI will generate a short story with
|
86 |
)
|
87 |
|
88 |
# Launch the Gradio app
|
|
|
1 |
import gradio as gr
|
2 |
+
from langchain.llms import OpenAI as LangChainOpenAI
|
3 |
from langchain.prompts import PromptTemplate
|
4 |
from langchain.chains import LLMChain
|
5 |
+
from openai import OpenAI
|
6 |
import os
|
7 |
import re
|
8 |
|
9 |
+
# Initialize OpenAI client
|
10 |
+
client = OpenAI()
|
11 |
|
12 |
# Create a prompt template
|
13 |
template = """
|
|
|
22 |
prompt = PromptTemplate(template=template, input_variables=["topic"])
|
23 |
|
24 |
# Create an LLMChain
|
25 |
+
llm = LangChainOpenAI(temperature=0.7)
|
26 |
story_chain = LLMChain(llm=llm, prompt=prompt)
|
27 |
|
28 |
def generate_image(prompt):
|
29 |
try:
|
30 |
+
response = client.images.generate(
|
31 |
+
model="dall-e-3",
|
32 |
prompt=prompt,
|
33 |
+
size="1024x1024",
|
34 |
+
quality="standard",
|
35 |
n=1,
|
|
|
36 |
)
|
37 |
+
return response.data[0].url
|
38 |
except Exception as e:
|
39 |
print(f"Error generating image: {e}")
|
40 |
return None
|
|
|
51 |
while len(paragraphs) < 3:
|
52 |
paragraphs.append("...")
|
53 |
|
54 |
+
# Generate images for each paragraph
|
55 |
+
image_urls = []
|
56 |
for paragraph in paragraphs:
|
57 |
image_url = generate_image(paragraph)
|
58 |
+
image_urls.append(image_url)
|
59 |
|
60 |
+
return paragraphs, image_urls
|
61 |
|
62 |
# Create the Gradio interface
|
63 |
def gradio_interface(topic):
|
64 |
try:
|
65 |
+
paragraphs, image_urls = generate_story_with_images(topic)
|
66 |
+
return [
|
67 |
+
paragraphs[0], image_urls[0],
|
68 |
+
paragraphs[1], image_urls[1],
|
69 |
+
paragraphs[2], image_urls[2]
|
70 |
+
]
|
71 |
except Exception as e:
|
72 |
print(f"Error in gradio_interface: {e}")
|
73 |
+
return ["An error occurred while generating the story and images."] + [""] * 5
|
74 |
|
75 |
iface = gr.Interface(
|
76 |
fn=gradio_interface,
|
|
|
84 |
gr.Image(label="Image 3", type="filepath"),
|
85 |
],
|
86 |
title="Story Generator with Images",
|
87 |
+
description="Enter a topic, and the AI will generate a short story with an image for each paragraph."
|
88 |
)
|
89 |
|
90 |
# Launch the Gradio app
|