Spaces:
Runtime error
Runtime error
image gen with downloading and temp file removal
Browse files- app.py +47 -14
- env-sample +1 -2
app.py
CHANGED
|
@@ -23,11 +23,13 @@ import time
|
|
| 23 |
# countdown stuff
|
| 24 |
from datetime import datetime, timedelta
|
| 25 |
|
|
|
|
| 26 |
from google.cloud import aiplatform
|
| 27 |
import vertexai
|
| 28 |
# from vertexai.preview.generative_models import GenerativeModel
|
| 29 |
from vertexai.preview.vision_models import ImageGenerationModel
|
| 30 |
from vertexai import preview
|
|
|
|
| 31 |
|
| 32 |
load_dotenv()
|
| 33 |
|
|
@@ -92,10 +94,11 @@ def zip_images(image_paths_and_labels):
|
|
| 92 |
zip_file_path = tempfile.NamedTemporaryFile(delete=False, suffix='.zip').name
|
| 93 |
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
|
| 94 |
for image_url, _ in image_paths_and_labels:
|
| 95 |
-
image_content = download_image(image_url)
|
|
|
|
| 96 |
random_filename = ''.join(random.choices(string.ascii_letters + string.digits, k=10)) + ".png"
|
| 97 |
# Write the image content to the zip file with the random filename
|
| 98 |
-
zipf.writestr(
|
| 99 |
return zip_file_path
|
| 100 |
|
| 101 |
|
|
@@ -107,6 +110,12 @@ def download_all_images():
|
|
| 107 |
zip_path = zip_images(image_paths_and_labels)
|
| 108 |
image_paths_global = [] # Reset the global variable
|
| 109 |
image_labels_global = [] # Reset the global variable
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
return zip_path
|
| 111 |
|
| 112 |
def generate_images(prompts, pw):
|
|
@@ -138,17 +147,41 @@ def generate_images(prompts, pw):
|
|
| 138 |
prompt_w_challenge = f"{challenge}: {text}"
|
| 139 |
print(prompt_w_challenge)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
#custom css
|
| 154 |
css = """
|
|
@@ -160,7 +193,7 @@ css = """
|
|
| 160 |
|
| 161 |
with gr.Blocks(css=css) as demo:
|
| 162 |
|
| 163 |
-
gr.Markdown("# <center>Prompt de Resistance
|
| 164 |
|
| 165 |
pw = gr.Textbox(label="Password", type="password", placeholder="Enter the password to unlock the service", value="REBEL.pier6moment")
|
| 166 |
|
|
|
|
| 23 |
# countdown stuff
|
| 24 |
from datetime import datetime, timedelta
|
| 25 |
|
| 26 |
+
|
| 27 |
from google.cloud import aiplatform
|
| 28 |
import vertexai
|
| 29 |
# from vertexai.preview.generative_models import GenerativeModel
|
| 30 |
from vertexai.preview.vision_models import ImageGenerationModel
|
| 31 |
from vertexai import preview
|
| 32 |
+
import uuid #for generating unique filenames
|
| 33 |
|
| 34 |
load_dotenv()
|
| 35 |
|
|
|
|
| 94 |
zip_file_path = tempfile.NamedTemporaryFile(delete=False, suffix='.zip').name
|
| 95 |
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
|
| 96 |
for image_url, _ in image_paths_and_labels:
|
| 97 |
+
# image_content = download_image(image_url)
|
| 98 |
+
image_content = open(image_url, "rb").read()
|
| 99 |
random_filename = ''.join(random.choices(string.ascii_letters + string.digits, k=10)) + ".png"
|
| 100 |
# Write the image content to the zip file with the random filename
|
| 101 |
+
zipf.writestr(image_url, image_content)
|
| 102 |
return zip_file_path
|
| 103 |
|
| 104 |
|
|
|
|
| 110 |
zip_path = zip_images(image_paths_and_labels)
|
| 111 |
image_paths_global = [] # Reset the global variable
|
| 112 |
image_labels_global = [] # Reset the global variable
|
| 113 |
+
|
| 114 |
+
# delete all local images
|
| 115 |
+
for image_path, _ in image_paths_and_labels:
|
| 116 |
+
os.remove(image_path)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
return zip_path
|
| 120 |
|
| 121 |
def generate_images(prompts, pw):
|
|
|
|
| 147 |
prompt_w_challenge = f"{challenge}: {text}"
|
| 148 |
print(prompt_w_challenge)
|
| 149 |
|
| 150 |
+
start_time = time.time()
|
| 151 |
+
|
| 152 |
+
try:
|
| 153 |
+
#what model to use?
|
| 154 |
+
model = ImageGenerationModel.from_pretrained("imagegeneration@002")
|
| 155 |
+
response = model.generate_images(
|
| 156 |
+
prompt=prompt_w_challenge,
|
| 157 |
+
number_of_images=1,
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
end_time = time.time()
|
| 161 |
+
gen_time = end_time - start_time # total generation time
|
| 162 |
+
|
| 163 |
+
#generate random filename using uuid
|
| 164 |
+
filename = f"{uuid.uuid4()}.png"
|
| 165 |
+
|
| 166 |
+
# Save the image to a temporary file, and return this
|
| 167 |
+
image_url = filename
|
| 168 |
+
response[0].save(filename)
|
| 169 |
+
image_label = f"{i+1}: {text}"
|
| 170 |
+
|
| 171 |
+
try:
|
| 172 |
+
# Save the prompt, model, image URL, generation time and creation timestamp to the database
|
| 173 |
+
mongo_collection.insert_one({"user": user_initials, "text": text, "model": "imagen", "image_url": image_url, "gen_time": gen_time, "timestamp": time.time(), "challenge": challenge})
|
| 174 |
+
except Exception as e:
|
| 175 |
+
print(e)
|
| 176 |
+
raise gr.Error("An error occurred while saving the prompt to the database.")
|
| 177 |
+
|
| 178 |
+
# Append the image URL and label to their respective lists
|
| 179 |
+
image_paths.append(image_url)
|
| 180 |
+
image_labels.append(image_label)
|
| 181 |
+
except Exception as e:
|
| 182 |
+
print(e)
|
| 183 |
+
raise gr.Error(f"An error occurred while generating the image for: {entry}")
|
| 184 |
+
return image_paths, image_labels
|
| 185 |
|
| 186 |
#custom css
|
| 187 |
css = """
|
|
|
|
| 193 |
|
| 194 |
with gr.Blocks(css=css) as demo:
|
| 195 |
|
| 196 |
+
gr.Markdown("# <center>Prompt de Resistance Vertex Imagen</center>")
|
| 197 |
|
| 198 |
pw = gr.Textbox(label="Password", type="password", placeholder="Enter the password to unlock the service", value="REBEL.pier6moment")
|
| 199 |
|
env-sample
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
OPENAI_API_KEY = <YOUR_OPENAI_API_KEY>
|
| 2 |
PW = <YOUR_PW>
|
| 3 |
MONGO_URI=<YOUR_MONGO_URI>
|
| 4 |
-
MODE=dev
|
| 5 |
-
ANTHROPIC_API_KEY=<YOUR_ANTHROPIC_API_KEY>
|
|
|
|
| 1 |
OPENAI_API_KEY = <YOUR_OPENAI_API_KEY>
|
| 2 |
PW = <YOUR_PW>
|
| 3 |
MONGO_URI=<YOUR_MONGO_URI>
|
| 4 |
+
MODE=dev
|
|
|