Spaces:
Runtime error
Runtime error
use byte data instead of url for storing images
Browse files- app.py +25 -6
- env-sample +2 -1
app.py
CHANGED
@@ -31,6 +31,11 @@ from vertexai.preview.vision_models import ImageGenerationModel
|
|
31 |
from vertexai import preview
|
32 |
import uuid #for generating unique filenames
|
33 |
|
|
|
|
|
|
|
|
|
|
|
34 |
load_dotenv()
|
35 |
|
36 |
|
@@ -59,6 +64,17 @@ with open('challenges.txt', 'r') as file:
|
|
59 |
for line in file:
|
60 |
challenges.append(line.strip())
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
# pick a random challenge
|
63 |
def get_challenge():
|
64 |
global challenge
|
@@ -160,19 +176,22 @@ def generate_images(prompts, pw,model_name):
|
|
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 |
model_for_db = f"imagen-{model_name}"
|
172 |
|
173 |
try:
|
174 |
# Save the prompt, model, image URL, generation time and creation timestamp to the database
|
175 |
-
mongo_collection.insert_one({"user": user_initials, "text": text, "model": model_for_db, "image_url":
|
176 |
except Exception as e:
|
177 |
print(e)
|
178 |
raise gr.Error("An error occurred while saving the prompt to the database.")
|
@@ -197,7 +216,7 @@ with gr.Blocks(css=css) as demo:
|
|
197 |
|
198 |
gr.Markdown("# <center>Prompt de Resistance Vertex Imagen</center>")
|
199 |
|
200 |
-
pw = gr.Textbox(label="Password", type="password", placeholder="Enter the password to unlock the service")
|
201 |
|
202 |
#instructions
|
203 |
with gr.Accordion("Instructions & Tips",label="instructions",open=False):
|
@@ -227,7 +246,7 @@ with gr.Blocks(css=css) as demo:
|
|
227 |
show_labels = gr.Checkbox(label="Show Labels", value=False)
|
228 |
|
229 |
|
230 |
-
with gr.Accordion("Downloads",label="download",open=
|
231 |
download_all_btn = gr.Button("Download All")
|
232 |
download_link = gr.File(label="Download Zip")
|
233 |
|
|
|
31 |
from vertexai import preview
|
32 |
import uuid #for generating unique filenames
|
33 |
|
34 |
+
# GCP credentials stuff
|
35 |
+
import json
|
36 |
+
import tempfile
|
37 |
+
|
38 |
+
|
39 |
load_dotenv()
|
40 |
|
41 |
|
|
|
64 |
for line in file:
|
65 |
challenges.append(line.strip())
|
66 |
|
67 |
+
#get GCP credentials
|
68 |
+
def get_credentials():
|
69 |
+
creds_json_str = os.getenv("IMAGEN") # get json credentials stored as a string
|
70 |
+
# create a temporary file
|
71 |
+
with tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".json") as temp:
|
72 |
+
temp.write(creds_json_str) # write in json format
|
73 |
+
temp_filename = temp.name
|
74 |
+
return temp_filename
|
75 |
+
# pass
|
76 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]= get_credentials ()
|
77 |
+
|
78 |
# pick a random challenge
|
79 |
def get_challenge():
|
80 |
global challenge
|
|
|
176 |
end_time = time.time()
|
177 |
gen_time = end_time - start_time # total generation time
|
178 |
|
179 |
+
image_bytes = response[0]._image_bytes
|
180 |
+
image_url = Image.open(BytesIO(image_bytes))
|
181 |
+
|
182 |
#generate random filename using uuid
|
183 |
+
#filename = f"{uuid.uuid4()}.png"
|
184 |
|
185 |
# Save the image to a temporary file, and return this
|
186 |
+
#image_url = filename
|
187 |
+
#response[0].save(filename)
|
188 |
image_label = f"{i+1}: {text}"
|
189 |
|
190 |
model_for_db = f"imagen-{model_name}"
|
191 |
|
192 |
try:
|
193 |
# Save the prompt, model, image URL, generation time and creation timestamp to the database
|
194 |
+
mongo_collection.insert_one({"user": user_initials, "text": text, "model": model_for_db, "image_url": "bytes", "gen_time": gen_time, "timestamp": time.time(), "challenge": challenge})
|
195 |
except Exception as e:
|
196 |
print(e)
|
197 |
raise gr.Error("An error occurred while saving the prompt to the database.")
|
|
|
216 |
|
217 |
gr.Markdown("# <center>Prompt de Resistance Vertex Imagen</center>")
|
218 |
|
219 |
+
pw = gr.Textbox(label="Password", type="password", placeholder="Enter the password to unlock the service", value="REBEL.pier6moment")
|
220 |
|
221 |
#instructions
|
222 |
with gr.Accordion("Instructions & Tips",label="instructions",open=False):
|
|
|
246 |
show_labels = gr.Checkbox(label="Show Labels", value=False)
|
247 |
|
248 |
|
249 |
+
with gr.Accordion("Downloads",label="download",open=True):
|
250 |
download_all_btn = gr.Button("Download All")
|
251 |
download_link = gr.File(label="Download Zip")
|
252 |
|
env-sample
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
OPENAI_API_KEY = <YOUR_OPENAI_API_KEY>
|
2 |
PW = <YOUR_PW>
|
3 |
MONGO_URI=<YOUR_MONGO_URI>
|
4 |
-
MODE=dev
|
|
|
|
1 |
OPENAI_API_KEY = <YOUR_OPENAI_API_KEY>
|
2 |
PW = <YOUR_PW>
|
3 |
MONGO_URI=<YOUR_MONGO_URI>
|
4 |
+
MODE=dev
|
5 |
+
GOOGLE_APPLICATION_CREDENTIALS= <YOUR_GOOGLE_APPLICATION_CREDENTIALS>
|