jamescalam commited on
Commit
d3ba2a4
β€’
1 Parent(s): 86206c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -15
app.py CHANGED
@@ -66,19 +66,15 @@ def prompt_query(text: str):
66
  print(f"Running prompt_query('{text}')")
67
  embeds = encode_text(text)
68
  try:
69
- print("Try query pinecone")
70
  xc = index.query(embeds, top_k=30, include_metadata=True)
71
- print("query successful")
72
  except Exception as e:
73
  print(f"Error during query: {e}")
74
  # reinitialize connection
75
- print("Try reinitialize Pinecone connection")
76
  pinecone.init(api_key=PINECONE_KEY, environment='us-west1-gcp')
77
  index2 = pinecone.Index(index_id)
78
  try:
79
- print("Now try querying pinecone again")
80
  xc = index2.query(embeds, top_k=30, include_metadata=True)
81
- print("query successful")
82
  except Exception as e:
83
  raise ValueError(e)
84
  scores = [round(match['score'], 2) for match in xc['matches']]
@@ -99,14 +95,11 @@ def test_image(_id, image):
99
  # delete corrupted file from pinecone and cloud
100
  index.delete(ids=[_id])
101
  bucket.blob(f"images/{_id}.png").delete()
102
- print(f"DELETED '{_id}'")
103
  return False
104
 
105
  def prompt_image(text: str):
106
- print(f"prompt_image('{text}')")
107
  embeds = encode_text(text)
108
  try:
109
- print("try query pinecone")
110
  xc = index.query(
111
  embeds, top_k=9, include_metadata=True,
112
  filter={"image_nsfw": {"$lt": 0.5}}
@@ -117,32 +110,26 @@ def prompt_image(text: str):
117
  pinecone.init(api_key=PINECONE_KEY, environment='us-west1-gcp')
118
  index2 = pinecone.Index(index_id)
119
  try:
120
- print("try query pinecone after reinit")
121
  xc = index2.query(
122
  embeds, top_k=9, include_metadata=True,
123
  filter={"image_nsfw": {"$lt": 0.5}}
124
  )
 
125
  except Exception as e:
126
  raise ValueError(e)
127
  scores = [match['score'] for match in xc['matches']]
128
  ids = [match['id'] for match in xc['matches']]
129
  images = []
130
- print("Begin looping through (ids, image_urls)")
131
  for _id in ids:
132
  try:
133
  image_url = f"images/{_id}.png"
134
- print("download_as_string from GCP")
135
  blob = bucket.blob(image_url).download_as_string()
136
- print("downloaded successfully")
137
  blob_bytes = io.BytesIO(blob)
138
  im = Image.open(blob_bytes)
139
- print("image opened successfully")
140
  if test_image(_id, im):
141
  images.append(im)
142
- print("image accessible")
143
  else:
144
  images.append(missing_im)
145
- print("image NOT accessible")
146
  except ValueError:
147
  print(f"ValueError: '{image_url}'")
148
  return images, scores
 
66
  print(f"Running prompt_query('{text}')")
67
  embeds = encode_text(text)
68
  try:
 
69
  xc = index.query(embeds, top_k=30, include_metadata=True)
 
70
  except Exception as e:
71
  print(f"Error during query: {e}")
72
  # reinitialize connection
 
73
  pinecone.init(api_key=PINECONE_KEY, environment='us-west1-gcp')
74
  index2 = pinecone.Index(index_id)
75
  try:
 
76
  xc = index2.query(embeds, top_k=30, include_metadata=True)
77
+ print("Reinitialized query successful")
78
  except Exception as e:
79
  raise ValueError(e)
80
  scores = [round(match['score'], 2) for match in xc['matches']]
 
95
  # delete corrupted file from pinecone and cloud
96
  index.delete(ids=[_id])
97
  bucket.blob(f"images/{_id}.png").delete()
 
98
  return False
99
 
100
  def prompt_image(text: str):
 
101
  embeds = encode_text(text)
102
  try:
 
103
  xc = index.query(
104
  embeds, top_k=9, include_metadata=True,
105
  filter={"image_nsfw": {"$lt": 0.5}}
 
110
  pinecone.init(api_key=PINECONE_KEY, environment='us-west1-gcp')
111
  index2 = pinecone.Index(index_id)
112
  try:
 
113
  xc = index2.query(
114
  embeds, top_k=9, include_metadata=True,
115
  filter={"image_nsfw": {"$lt": 0.5}}
116
  )
117
+ print("Reinitialized query successful")
118
  except Exception as e:
119
  raise ValueError(e)
120
  scores = [match['score'] for match in xc['matches']]
121
  ids = [match['id'] for match in xc['matches']]
122
  images = []
 
123
  for _id in ids:
124
  try:
125
  image_url = f"images/{_id}.png"
 
126
  blob = bucket.blob(image_url).download_as_string()
 
127
  blob_bytes = io.BytesIO(blob)
128
  im = Image.open(blob_bytes)
 
129
  if test_image(_id, im):
130
  images.append(im)
 
131
  else:
132
  images.append(missing_im)
 
133
  except ValueError:
134
  print(f"ValueError: '{image_url}'")
135
  return images, scores