roy214 commited on
Commit
92711bf
·
verified ·
1 Parent(s): 4315a06

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +41 -26
src/streamlit_app.py CHANGED
@@ -12,45 +12,60 @@ import torch
12
  from transformers import CLIPModel, CLIPProcessor
13
  from huggingface_hub import hf_hub_download, snapshot_download
14
  import json
 
15
 
16
  # Khởi tạo client S3 với thông tin cấu hình từ secrets
17
  s3 = boto3.client('s3')
18
 
 
19
  def get_image_from_s3(bucket_name, img_id):
20
  try:
21
  # Trả về URL S3 trực tiếp cho ảnh
22
- return f"https://{bucket_name}.s3.amazonaws.com/{img_id}.jpg"
23
- except NoCredentialsError:
24
- st.error("Credentials not available.")
25
- return None
26
  except Exception as e:
27
- st.error(f"Error fetching image: {e}")
28
  return None
29
 
30
  def show_img(img_id, score=None, col=None):
31
- # Lấy ảnh từ S3
32
- img = get_image_from_s3(bucket_name, img_id)
 
 
 
 
 
 
 
 
 
33
 
34
- if img:
35
- img_style = style[style['id'] == img_id]
36
 
37
- if not img_style.empty:
38
- parts = []
39
- parts.append(str(img_style['gender'].values[0]))
40
- parts.append(str(img_style['masterCategory'].values[0]))
41
- parts.append(str(img_style['subCategory'].values[0]))
42
- parts.append(str(img_style['articleType'].values[0]))
43
- parts.append(str(img_style['baseColour'].values[0]))
44
- parts.append(str(img_style['year'].values[0]))
45
- parts.append(str(img_style['usage'].values[0]))
46
- parts.append(str(img_style['productDisplayName'].values[0]))
47
-
48
- text = '- '.join(parts)
49
- if score: text += f'\n Score: {score}'
50
-
51
- # Hiển thị ảnh trong cột
52
- if col:
53
- col.image(img, caption=text, use_container_width=True)
 
 
 
 
 
 
54
 
55
  def search_faiss(model, processor, index, id_map, prompt, top_k=5, device='cpu'):
56
  inputs = processor(text=[prompt], return_tensors='pt', padding=True).to(device)
 
12
  from transformers import CLIPModel, CLIPProcessor
13
  from huggingface_hub import hf_hub_download, snapshot_download
14
  import json
15
+ import requests
16
 
17
  # Khởi tạo client S3 với thông tin cấu hình từ secrets
18
  s3 = boto3.client('s3')
19
 
20
+
21
  def get_image_from_s3(bucket_name, img_id):
22
  try:
23
  # Trả về URL S3 trực tiếp cho ảnh
24
+ img_url = f"https://{bucket_name}.s3.amazonaws.com/{img_id}.jpg"
25
+ return img_url
 
 
26
  except Exception as e:
27
+ st.error(f"Error constructing image URL: {e}")
28
  return None
29
 
30
  def show_img(img_id, score=None, col=None):
31
+ # Lấy URL ảnh từ S3
32
+ img_url = get_image_from_s3(bucket_name, img_id)
33
+
34
+ if img_url:
35
+ try:
36
+ # Tải ảnh từ URL S3
37
+ response = requests.get(img_url)
38
+ response.raise_for_status() # Kiểm tra nếu có lỗi trong quá trình tải ảnh
39
+
40
+ # Mở ảnh từ dữ liệu trong bộ nhớ
41
+ img = Image.open(BytesIO(response.content))
42
 
43
+ # Lấy thông tin style từ img_id (giả sử bạn có một dataframe style)
44
+ img_style = style[style['id'] == img_id]
45
 
46
+ if not img_style.empty:
47
+ parts = []
48
+ parts.append(str(img_style['gender'].values[0]))
49
+ parts.append(str(img_style['masterCategory'].values[0]))
50
+ parts.append(str(img_style['subCategory'].values[0]))
51
+ parts.append(str(img_style['articleType'].values[0]))
52
+ parts.append(str(img_style['baseColour'].values[0]))
53
+ parts.append(str(img_style['year'].values[0]))
54
+ parts.append(str(img_style['usage'].values[0]))
55
+ parts.append(str(img_style['productDisplayName'].values[0]))
56
+
57
+ text = '- '.join(parts)
58
+ if score:
59
+ text += f'\n Score: {score}'
60
+
61
+ # Hiển thị ảnh trong cột
62
+ if col:
63
+ col.image(img, caption=text, use_container_width=True)
64
+
65
+ except requests.exceptions.RequestException as e:
66
+ st.error(f"Error fetching image: {e}")
67
+ except Exception as e:
68
+ st.error(f"Error processing image: {e}")
69
 
70
  def search_faiss(model, processor, index, id_map, prompt, top_k=5, device='cpu'):
71
  inputs = processor(text=[prompt], return_tensors='pt', padding=True).to(device)