leo-bourrel commited on
Commit
4029b73
·
1 Parent(s): 82aae5c

feat: app download image from bucket

Browse files
src/app.py CHANGED
@@ -8,12 +8,13 @@ from data_models.sql_connection import get_db_connection
8
  from data_models.park_manager import ParkManager
9
  from data_models.image_manager import ImageManager
10
  from data_models.openai_manager import OpenAIManager
11
-
12
 
13
  engine, session = get_db_connection()
14
  park_manager = ParkManager()
15
  image_manager = ImageManager()
16
  openai_manager = OpenAIManager()
 
17
 
18
 
19
  def get_image_predictions(images: list[dict]) -> dict:
@@ -31,7 +32,7 @@ def get_image_predictions(images: list[dict]) -> dict:
31
  return predictions
32
 
33
 
34
- def image_gallery(images: list[dict], predictions:dict) -> None:
35
  """Display a gallery of images in a streamlit app
36
 
37
  Args:
@@ -42,9 +43,9 @@ def image_gallery(images: list[dict], predictions:dict) -> None:
42
  columns = st.columns(3)
43
  for index, image in enumerate(images):
44
  with columns[index % 3]:
45
- resized_image = resize_image(image["name"], target_w=1080, target_h=720)
46
  st.image(
47
- np.array(resized_image), width=200, caption=f"Image id #{image['id']}"
48
  )
49
  if not image["id"] in predictions or predictions[image["id"]] is None:
50
  st.write("No predictions available")
 
8
  from data_models.park_manager import ParkManager
9
  from data_models.image_manager import ImageManager
10
  from data_models.openai_manager import OpenAIManager
11
+ from cloud_storage import GoogleCloudStorage
12
 
13
  engine, session = get_db_connection()
14
  park_manager = ParkManager()
15
  image_manager = ImageManager()
16
  openai_manager = OpenAIManager()
17
+ gcs = GoogleCloudStorage("ialab-fr-7047e56bef0b.json")
18
 
19
 
20
  def get_image_predictions(images: list[dict]) -> dict:
 
32
  return predictions
33
 
34
 
35
+ def image_gallery(images: list[dict], predictions: dict) -> None:
36
  """Display a gallery of images in a streamlit app
37
 
38
  Args:
 
43
  columns = st.columns(3)
44
  for index, image in enumerate(images):
45
  with columns[index % 3]:
46
+ image_blob = gcs.download_blob("suad_park", image["name"])
47
  st.image(
48
+ image_blob, width=200, caption=f"Image id #{image['id']}"
49
  )
50
  if not image["id"] in predictions or predictions[image["id"]] is None:
51
  st.write("No predictions available")
src/cloud_storage/google_cloud_storage.py CHANGED
@@ -32,3 +32,18 @@ class GoogleCloudStorage(BaseStorage):
32
 
33
  print(f"File uploaded to gs://{bucket_name}/{bucket_file_path}")
34
  return f"gs://{bucket_name}/{bucket_file_path}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  print(f"File uploaded to gs://{bucket_name}/{bucket_file_path}")
34
  return f"gs://{bucket_name}/{bucket_file_path}"
35
+
36
+ def download_blob(self, bucket_name: str, source_blob_name: str) -> bytes:
37
+ """Downloads a blob from the bucket
38
+
39
+ Args:
40
+ bucket_name (str): Bucket name
41
+ source_blob_name (str): Name of the blob to download
42
+
43
+ Returns:
44
+ bytes: Content of the blob
45
+ """
46
+ bucket = self.client.get_bucket(bucket_name)
47
+ source_blob_name = source_blob_name.replace(f"gs://{bucket_name}/", "")
48
+ blob = bucket.blob(source_blob_name)
49
+ return blob.download_as_bytes()