Spaces:
Runtime error
Runtime error
jamescalam
commited on
Commit
•
ea8a830
1
Parent(s):
49bbcba
Update app.py
Browse files
app.py
CHANGED
@@ -60,16 +60,22 @@ def encode_text(text: str):
|
|
60 |
return text_embeds
|
61 |
|
62 |
def prompt_query(text: str):
|
|
|
63 |
embeds = encode_text(text)
|
64 |
try:
|
|
|
65 |
xc = index.query(embeds, top_k=30, include_metadata=True)
|
|
|
66 |
except Exception as e:
|
67 |
print(f"Error during query: {e}")
|
68 |
# reinitialize connection
|
|
|
69 |
pinecone.init(api_key=PINECONE_KEY, environment='us-west1-gcp')
|
70 |
index2 = pinecone.Index(index_id)
|
71 |
try:
|
|
|
72 |
xc = index2.query(embeds, top_k=30, include_metadata=True)
|
|
|
73 |
except Exception as e:
|
74 |
raise ValueError(e)
|
75 |
prompts = [
|
@@ -95,7 +101,9 @@ def diffuse(text: str):
|
|
95 |
added_gcp = False
|
96 |
# push to storage
|
97 |
try:
|
|
|
98 |
blob = bucket.blob(f'images/{_id}.png')
|
|
|
99 |
blob.upload_from_filename(f'{_id}.png')
|
100 |
added_gcp = True
|
101 |
# add embedding and metadata to Pinecone
|
@@ -105,12 +113,17 @@ def diffuse(text: str):
|
|
105 |
'image_url': f'images/{_id}.png'
|
106 |
}
|
107 |
try:
|
|
|
108 |
index.upsert([(_id, embeds, meta)])
|
|
|
109 |
except Exception as e:
|
110 |
try:
|
|
|
111 |
pinecone.init(api_key=PINECONE_KEY, environment='us-west1-gcp')
|
112 |
index2 = pinecone.Index(index_id)
|
|
|
113 |
index2.upsert([(_id, embeds, meta)])
|
|
|
114 |
except Exception as e:
|
115 |
print(f"PINECONE_ERROR: {e}")
|
116 |
except Exception as e:
|
@@ -137,8 +150,10 @@ def test_image(_id, image):
|
|
137 |
return False
|
138 |
|
139 |
def prompt_image(text: str):
|
|
|
140 |
embeds = encode_text(text)
|
141 |
try:
|
|
|
142 |
xc = index.query(embeds, top_k=9, include_metadata=True)
|
143 |
except Exception as e:
|
144 |
print(f"Error during query: {e}")
|
@@ -146,6 +161,7 @@ def prompt_image(text: str):
|
|
146 |
pinecone.init(api_key=PINECONE_KEY, environment='us-west1-gcp')
|
147 |
index2 = pinecone.Index(index_id)
|
148 |
try:
|
|
|
149 |
xc = index2.query(embeds, top_k=9, include_metadata=True)
|
150 |
except Exception as e:
|
151 |
raise ValueError(e)
|
@@ -155,15 +171,21 @@ def prompt_image(text: str):
|
|
155 |
scores = [match['score'] for match in xc['matches']]
|
156 |
ids = [match['id'] for match in xc['matches']]
|
157 |
images = []
|
|
|
158 |
for _id, image_url in zip(ids, image_urls):
|
159 |
try:
|
|
|
160 |
blob = bucket.blob(image_url).download_as_string()
|
|
|
161 |
blob_bytes = io.BytesIO(blob)
|
162 |
im = Image.open(blob_bytes)
|
|
|
163 |
if test_image(_id, im):
|
164 |
images.append(im)
|
|
|
165 |
else:
|
166 |
images.append(missing_im)
|
|
|
167 |
except ValueError:
|
168 |
print(f"ValueError: '{image_url}'")
|
169 |
return images, scores
|
@@ -185,6 +207,7 @@ def set_images(text: str):
|
|
185 |
else:
|
186 |
print("NO MATCH FOUND")
|
187 |
diffuse(text)
|
|
|
188 |
images, scores = prompt_image(text)
|
189 |
return gr.Gallery.update(value=images)
|
190 |
|
|
|
60 |
return text_embeds
|
61 |
|
62 |
def prompt_query(text: str):
|
63 |
+
print(f"Running prompt_query('{text}')")
|
64 |
embeds = encode_text(text)
|
65 |
try:
|
66 |
+
print("Try query pinecone")
|
67 |
xc = index.query(embeds, top_k=30, include_metadata=True)
|
68 |
+
print("query successful")
|
69 |
except Exception as e:
|
70 |
print(f"Error during query: {e}")
|
71 |
# reinitialize connection
|
72 |
+
print("Try reinitialize Pinecone connection")
|
73 |
pinecone.init(api_key=PINECONE_KEY, environment='us-west1-gcp')
|
74 |
index2 = pinecone.Index(index_id)
|
75 |
try:
|
76 |
+
print("Now try querying pinecone again")
|
77 |
xc = index2.query(embeds, top_k=30, include_metadata=True)
|
78 |
+
print("query successful")
|
79 |
except Exception as e:
|
80 |
raise ValueError(e)
|
81 |
prompts = [
|
|
|
101 |
added_gcp = False
|
102 |
# push to storage
|
103 |
try:
|
104 |
+
print("try push to Cloud Storage")
|
105 |
blob = bucket.blob(f'images/{_id}.png')
|
106 |
+
print("try upload_from_filename")
|
107 |
blob.upload_from_filename(f'{_id}.png')
|
108 |
added_gcp = True
|
109 |
# add embedding and metadata to Pinecone
|
|
|
113 |
'image_url': f'images/{_id}.png'
|
114 |
}
|
115 |
try:
|
116 |
+
print("now try upsert to pinecone")
|
117 |
index.upsert([(_id, embeds, meta)])
|
118 |
+
print("upsert successful")
|
119 |
except Exception as e:
|
120 |
try:
|
121 |
+
print("hit exception, now trying to reinit Pinecone connection")
|
122 |
pinecone.init(api_key=PINECONE_KEY, environment='us-west1-gcp')
|
123 |
index2 = pinecone.Index(index_id)
|
124 |
+
print(f"reconnected to pinecone '{index_id}' index")
|
125 |
index2.upsert([(_id, embeds, meta)])
|
126 |
+
print("upsert successful")
|
127 |
except Exception as e:
|
128 |
print(f"PINECONE_ERROR: {e}")
|
129 |
except Exception as e:
|
|
|
150 |
return False
|
151 |
|
152 |
def prompt_image(text: str):
|
153 |
+
print(f"prompt_image('{text}')")
|
154 |
embeds = encode_text(text)
|
155 |
try:
|
156 |
+
print("try query pinecone")
|
157 |
xc = index.query(embeds, top_k=9, include_metadata=True)
|
158 |
except Exception as e:
|
159 |
print(f"Error during query: {e}")
|
|
|
161 |
pinecone.init(api_key=PINECONE_KEY, environment='us-west1-gcp')
|
162 |
index2 = pinecone.Index(index_id)
|
163 |
try:
|
164 |
+
print("try query pinecone after reinit")
|
165 |
xc = index2.query(embeds, top_k=9, include_metadata=True)
|
166 |
except Exception as e:
|
167 |
raise ValueError(e)
|
|
|
171 |
scores = [match['score'] for match in xc['matches']]
|
172 |
ids = [match['id'] for match in xc['matches']]
|
173 |
images = []
|
174 |
+
print("Begin looping through (ids, image_urls)")
|
175 |
for _id, image_url in zip(ids, image_urls):
|
176 |
try:
|
177 |
+
print("download_as_string from GCP")
|
178 |
blob = bucket.blob(image_url).download_as_string()
|
179 |
+
print("downloaded successfully")
|
180 |
blob_bytes = io.BytesIO(blob)
|
181 |
im = Image.open(blob_bytes)
|
182 |
+
print("image opened successfully")
|
183 |
if test_image(_id, im):
|
184 |
images.append(im)
|
185 |
+
print("image accessible")
|
186 |
else:
|
187 |
images.append(missing_im)
|
188 |
+
print("image NOT accessible")
|
189 |
except ValueError:
|
190 |
print(f"ValueError: '{image_url}'")
|
191 |
return images, scores
|
|
|
207 |
else:
|
208 |
print("NO MATCH FOUND")
|
209 |
diffuse(text)
|
210 |
+
print(f"diffusion for '{text}' complete")
|
211 |
images, scores = prompt_image(text)
|
212 |
return gr.Gallery.update(value=images)
|
213 |
|