Spaces:
Runtime error
Runtime error
toggle for prompt authors
Browse files
app.py
CHANGED
@@ -56,12 +56,21 @@ except Exception as e:
|
|
56 |
|
57 |
image_paths_global = []
|
58 |
|
59 |
-
|
|
|
60 |
global image_paths_global, image_labels_global
|
61 |
image_paths, image_labels = generate_images(prompts, pw, model)
|
62 |
image_paths_global = image_paths # Store paths globally
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
def download_image(url):
|
67 |
response = requests.get(url)
|
@@ -93,45 +102,51 @@ def download_all_images():
|
|
93 |
return zip_path
|
94 |
|
95 |
def generate_images(prompts, pw, model):
|
96 |
-
#
|
97 |
if pw != os.getenv("PW"):
|
98 |
-
# output an error message to the user in the gradio interface if password is invalid
|
99 |
raise gr.Error("Invalid password. Please try again.")
|
100 |
|
101 |
image_paths = [] # Initialize a list to hold paths of generated images
|
102 |
image_labels = [] # Initialize a list to hold labels of generated images
|
103 |
-
|
|
|
|
|
104 |
prompts_list = prompts.split(';')
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
try:
|
109 |
client = OpenAI(api_key=openai_key)
|
110 |
-
|
111 |
response = client.images.generate(
|
112 |
prompt=text,
|
113 |
model=model, # dall-e-2 or dall-e-3
|
114 |
quality="standard", # standard or hd
|
115 |
-
size="512x512" if model == "dall-e-2" else "1024x1024", # varies for dalle-2 and dalle-3
|
116 |
n=1, # Number of images to generate
|
117 |
)
|
118 |
|
119 |
image_url = response.data[0].url
|
120 |
-
image_label = f"Prompt: {text}" # Creating a label for the image
|
121 |
|
122 |
try:
|
123 |
-
mongo_collection.insert_one({"text": text, "model": model, "image_url": image_url})
|
124 |
except Exception as e:
|
125 |
print(e)
|
126 |
raise gr.Error("An error occurred while saving the prompt to the database.")
|
127 |
|
128 |
-
#
|
129 |
image_paths.append(image_url)
|
130 |
-
image_labels.append(image_label)
|
131 |
|
132 |
except Exception as error:
|
133 |
print(str(error))
|
134 |
-
raise gr.Error(f"An error occurred while generating the image for: {
|
135 |
|
136 |
return image_paths, image_labels # Return both image paths and labels
|
137 |
|
@@ -144,12 +159,14 @@ with gr.Blocks() as demo:
|
|
144 |
placeholder="Enter your text and then click on the \"Image Generate\" button")
|
145 |
|
146 |
model = gr.Dropdown(choices=["dall-e-2", "dall-e-3"], label="Model", value="dall-e-2")
|
|
|
147 |
btn = gr.Button("Generate Images")
|
148 |
output_images = gr.Gallery(label="Image Outputs", show_label=True, columns=[3], rows=[1], object_fit="contain",
|
149 |
height="auto", allow_preview=False)
|
150 |
|
151 |
text.submit(fn=generate_images_wrapper, inputs=[text, pw, model], outputs=output_images, api_name="generate_image")
|
152 |
-
btn.click(fn=generate_images_wrapper, inputs=[text, pw, model], outputs=output_images, api_name=False)
|
|
|
153 |
|
154 |
download_all_btn = gr.Button("Download All")
|
155 |
download_link = gr.File(label="Download Zip")
|
|
|
56 |
|
57 |
image_paths_global = []
|
58 |
|
59 |
+
|
60 |
+
def generate_images_wrapper(prompts, pw, model, show_labels):
|
61 |
global image_paths_global, image_labels_global
|
62 |
image_paths, image_labels = generate_images(prompts, pw, model)
|
63 |
image_paths_global = image_paths # Store paths globally
|
64 |
+
|
65 |
+
if show_labels:
|
66 |
+
image_labels_global = image_labels # Store labels globally if showing labels is enabled
|
67 |
+
else:
|
68 |
+
image_labels_global = [""] * len(image_labels) # Use empty labels if showing labels is disabled
|
69 |
+
|
70 |
+
# Modify the return statement to not use labels if show_labels is False
|
71 |
+
image_data = [(path, label if show_labels else "") for path, label in zip(image_paths, image_labels)]
|
72 |
+
|
73 |
+
return image_data # Return image paths with or without labels based on the toggle
|
74 |
|
75 |
def download_image(url):
|
76 |
response = requests.get(url)
|
|
|
102 |
return zip_path
|
103 |
|
104 |
def generate_images(prompts, pw, model):
|
105 |
+
# Check for a valid password
|
106 |
if pw != os.getenv("PW"):
|
|
|
107 |
raise gr.Error("Invalid password. Please try again.")
|
108 |
|
109 |
image_paths = [] # Initialize a list to hold paths of generated images
|
110 |
image_labels = [] # Initialize a list to hold labels of generated images
|
111 |
+
users = [] # Initialize a list to hold user initials
|
112 |
+
|
113 |
+
# Split the prompts string into individual prompts based on semicolon separation
|
114 |
prompts_list = prompts.split(';')
|
115 |
+
|
116 |
+
for entry in prompts_list:
|
117 |
+
entry_parts = entry.split('-', 1) # Split by the first dash found
|
118 |
+
if len(entry_parts) != 2:
|
119 |
+
raise gr.Error("Invalid prompt format. Please ensure it is in 'initials-prompt' format.")
|
120 |
+
|
121 |
+
user_initials, text = entry_parts[0].strip(), entry_parts[1].strip() # Extract user initials and the prompt
|
122 |
+
users.append(user_initials) # Append user initials to the list
|
123 |
|
124 |
try:
|
125 |
client = OpenAI(api_key=openai_key)
|
|
|
126 |
response = client.images.generate(
|
127 |
prompt=text,
|
128 |
model=model, # dall-e-2 or dall-e-3
|
129 |
quality="standard", # standard or hd
|
130 |
+
size="512x512" if model == "dall-e-2" else "1024x1024", # varies for dalle-2 and dalle-3
|
131 |
n=1, # Number of images to generate
|
132 |
)
|
133 |
|
134 |
image_url = response.data[0].url
|
135 |
+
image_label = f"User: {user_initials}, Prompt: {text}" # Creating a label for the image including user initials
|
136 |
|
137 |
try:
|
138 |
+
mongo_collection.insert_one({"user": user_initials, "text": text, "model": model, "image_url": image_url})
|
139 |
except Exception as e:
|
140 |
print(e)
|
141 |
raise gr.Error("An error occurred while saving the prompt to the database.")
|
142 |
|
143 |
+
# Append the image URL and label to their respective lists
|
144 |
image_paths.append(image_url)
|
145 |
+
image_labels.append(image_label)
|
146 |
|
147 |
except Exception as error:
|
148 |
print(str(error))
|
149 |
+
raise gr.Error(f"An error occurred while generating the image for: {entry}")
|
150 |
|
151 |
return image_paths, image_labels # Return both image paths and labels
|
152 |
|
|
|
159 |
placeholder="Enter your text and then click on the \"Image Generate\" button")
|
160 |
|
161 |
model = gr.Dropdown(choices=["dall-e-2", "dall-e-3"], label="Model", value="dall-e-2")
|
162 |
+
show_labels = gr.Checkbox(label="Show Image Labels", value=True) # Default is to show labels
|
163 |
btn = gr.Button("Generate Images")
|
164 |
output_images = gr.Gallery(label="Image Outputs", show_label=True, columns=[3], rows=[1], object_fit="contain",
|
165 |
height="auto", allow_preview=False)
|
166 |
|
167 |
text.submit(fn=generate_images_wrapper, inputs=[text, pw, model], outputs=output_images, api_name="generate_image")
|
168 |
+
# btn.click(fn=generate_images_wrapper, inputs=[text, pw, model], outputs=output_images, api_name=False)
|
169 |
+
btn.click(fn=generate_images_wrapper, inputs=[text, pw, model, show_labels], outputs=output_images, api_name=False)
|
170 |
|
171 |
download_all_btn = gr.Button("Download All")
|
172 |
download_link = gr.File(label="Download Zip")
|