Spaces:
Runtime error
Runtime error
add gen time and timestamp to DB.
Browse filesLabel toggle not working properly when starting as false
app.py
CHANGED
@@ -19,7 +19,7 @@ from dotenv import load_dotenv
|
|
19 |
# stats stuff
|
20 |
from pymongo.mongo_client import MongoClient
|
21 |
from pymongo.server_api import ServerApi
|
22 |
-
|
23 |
|
24 |
|
25 |
|
@@ -47,21 +47,17 @@ mongo_client = MongoClient(uri, server_api=ServerApi('1'))
|
|
47 |
mongo_db = mongo_client.pdr
|
48 |
mongo_collection = mongo_db["images"]
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
mongo_client.admin.command('ping')
|
53 |
-
print("Pinged your deployment. You successfully connected to MongoDB!")
|
54 |
-
except Exception as e:
|
55 |
-
print(e)
|
56 |
-
|
57 |
-
# image_paths_global = []
|
58 |
-
# image_labels_global = []
|
59 |
|
60 |
def update_labels(show_labels):
|
61 |
if show_labels:
|
62 |
-
return [(path, label) for path, label in zip(image_paths_global, image_labels_global)]
|
|
|
63 |
else:
|
64 |
-
return [(path, "") for path in image_paths_global] # Empty string as label to hide them
|
|
|
|
|
65 |
|
66 |
def generate_images_wrapper(prompts, pw, model, show_labels):
|
67 |
global image_paths_global, image_labels_global
|
@@ -128,20 +124,24 @@ def generate_images(prompts, pw, model):
|
|
128 |
users.append(user_initials) # Append user initials to the list
|
129 |
|
130 |
try:
|
131 |
-
|
132 |
-
|
|
|
|
|
133 |
prompt=text,
|
134 |
model=model, # dall-e-2 or dall-e-3
|
135 |
quality="standard", # standard or hd
|
136 |
size="512x512" if model == "dall-e-2" else "1024x1024", # varies for dalle-2 and dalle-3
|
137 |
n=1, # Number of images to generate
|
138 |
)
|
|
|
|
|
139 |
|
140 |
image_url = response.data[0].url
|
141 |
image_label = f"User: {user_initials}, Prompt: {text}" # Creating a label for the image including user initials
|
142 |
|
143 |
try:
|
144 |
-
mongo_collection.insert_one({"user": user_initials, "text": text, "model": model, "image_url": image_url})
|
145 |
except Exception as e:
|
146 |
print(e)
|
147 |
raise gr.Error("An error occurred while saving the prompt to the database.")
|
@@ -164,8 +164,9 @@ with gr.Blocks() as demo:
|
|
164 |
text = gr.Textbox(label="What do you want to create?",
|
165 |
placeholder="Enter your text and then click on the \"Image Generate\" button")
|
166 |
|
167 |
-
model = gr.Dropdown(choices=["dall-e-2", "dall-e-3"], label="Model", value="dall-e-
|
168 |
-
|
|
|
169 |
btn = gr.Button("Generate Images")
|
170 |
output_images = gr.Gallery(label="Image Outputs", show_label=True, columns=[3], rows=[1], object_fit="contain",
|
171 |
height="auto", allow_preview=False)
|
|
|
19 |
# stats stuff
|
20 |
from pymongo.mongo_client import MongoClient
|
21 |
from pymongo.server_api import ServerApi
|
22 |
+
import time # Make sure to import the time module
|
23 |
|
24 |
|
25 |
|
|
|
47 |
mongo_db = mongo_client.pdr
|
48 |
mongo_collection = mongo_db["images"]
|
49 |
|
50 |
+
image_labels_global = []
|
51 |
+
image_paths_global = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
def update_labels(show_labels):
|
54 |
if show_labels:
|
55 |
+
# return [(path, label) for path, label in zip(image_paths_global, image_labels_global)]
|
56 |
+
updated_gallery = [(path, label) for path, label in zip(image_paths_global, image_labels_global)]
|
57 |
else:
|
58 |
+
# return [(path, "") for path in image_paths_global] # Empty string as label to hide them
|
59 |
+
updated_gallery = [(path, "") for path in image_paths_global] # Empty string as label to hide them
|
60 |
+
return updated_gallery
|
61 |
|
62 |
def generate_images_wrapper(prompts, pw, model, show_labels):
|
63 |
global image_paths_global, image_labels_global
|
|
|
124 |
users.append(user_initials) # Append user initials to the list
|
125 |
|
126 |
try:
|
127 |
+
openai_client = OpenAI(api_key=openai_key)
|
128 |
+
start_time = time.time()
|
129 |
+
|
130 |
+
response = openai_client.images.generate(
|
131 |
prompt=text,
|
132 |
model=model, # dall-e-2 or dall-e-3
|
133 |
quality="standard", # standard or hd
|
134 |
size="512x512" if model == "dall-e-2" else "1024x1024", # varies for dalle-2 and dalle-3
|
135 |
n=1, # Number of images to generate
|
136 |
)
|
137 |
+
end_time = time.time()
|
138 |
+
gen_time = end_time - start_time # total generation time
|
139 |
|
140 |
image_url = response.data[0].url
|
141 |
image_label = f"User: {user_initials}, Prompt: {text}" # Creating a label for the image including user initials
|
142 |
|
143 |
try:
|
144 |
+
mongo_collection.insert_one({"user": user_initials, "text": text, "model": model, "image_url": image_url, "gen_time": gen_time, "timestamp": time.time()})
|
145 |
except Exception as e:
|
146 |
print(e)
|
147 |
raise gr.Error("An error occurred while saving the prompt to the database.")
|
|
|
164 |
text = gr.Textbox(label="What do you want to create?",
|
165 |
placeholder="Enter your text and then click on the \"Image Generate\" button")
|
166 |
|
167 |
+
model = gr.Dropdown(choices=["dall-e-2", "dall-e-3"], label="Model", value="dall-e-3")
|
168 |
+
#MH TODO: not toggling properly
|
169 |
+
show_labels = gr.Checkbox(label="Show Image Labels", value=True)
|
170 |
btn = gr.Button("Generate Images")
|
171 |
output_images = gr.Gallery(label="Image Outputs", show_label=True, columns=[3], rows=[1], object_fit="contain",
|
172 |
height="auto", allow_preview=False)
|