uploaded_app and requirements file
Browse files- app.py +72 -0
- requirements.txt +10 -0
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
import cv2
|
4 |
+
import albumentations as A
|
5 |
+
import torch.nn.functional as F
|
6 |
+
import pandas as pd
|
7 |
+
import numpy as np
|
8 |
+
import pickle
|
9 |
+
from pathlib import Path
|
10 |
+
from model.clip_model import CLIPModel
|
11 |
+
st.title("Product Image to description Prediction in E-commerce")
|
12 |
+
|
13 |
+
def get_emebddings(file_path):
|
14 |
+
with open(file_path,'rb') as file:
|
15 |
+
data = pickle.load(file)
|
16 |
+
|
17 |
+
return data
|
18 |
+
|
19 |
+
|
20 |
+
# def find_text_matches(model,text_emebddings,)
|
21 |
+
|
22 |
+
|
23 |
+
embeddings_data_path = Path("./data/embeddings.pkl")
|
24 |
+
image_caption_path = Path("./data/image_details.csv")
|
25 |
+
model_path = Path('./model/best.pt')
|
26 |
+
clip_model = CLIPModel().to('cpu')
|
27 |
+
clip_model.load_state_dict(torch.load(model_path,map_location='cpu'))
|
28 |
+
embeddings = get_emebddings(embeddings_data_path)
|
29 |
+
caption_df = pd.read_csv(image_caption_path)
|
30 |
+
# print(caption_df.head())
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
def find_text_matches(model, text_emebddings, image_path,actual_captions,max_out=4):
|
35 |
+
item={}
|
36 |
+
image = cv2.imread(image_path)
|
37 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
38 |
+
transform = A.Compose([
|
39 |
+
A.Resize(224,224,always_apply=True),
|
40 |
+
A.Normalize(max_pixel_value=255.0,always_apply=True)
|
41 |
+
])
|
42 |
+
trans_image = transform(image=image)['image']
|
43 |
+
item['image'] = torch.tensor(trans_image).permute(2,0,1).float().unsqueeze(0)
|
44 |
+
|
45 |
+
#Prediction
|
46 |
+
with torch.no_grad():
|
47 |
+
image_features = model.image_encoder(item['image'].to('cpu'))
|
48 |
+
image_embeddings = model.image_projection(image_features)
|
49 |
+
image_embeddings_n = F.normalize(image_embeddings,p=2,dim=-1)
|
50 |
+
text_embeddings_n = F.normalize(text_emebddings,p=2,dim=-1)
|
51 |
+
dot_similarity = text_embeddings_n @ image_embeddings_n.T
|
52 |
+
values,indices = torch.topk(dot_similarity.T.cpu() ,k=20)
|
53 |
+
matches = [actual_captions[idx] for idx in indices[::5]]
|
54 |
+
return matches
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
st.subheader("Select the Image from Given files path")
|
59 |
+
images = ("./images/0108775015.jpg","./images/0120129014.jpg","./images/0187949019.jpg","./images/0203595036.jpg","./images/0212629031.jpg","./images/0212629048.jpg","./images/0237347052.jpg")
|
60 |
+
image = st.selectbox("images",images)
|
61 |
+
st.subheader("Selected Image")
|
62 |
+
st.image(image)
|
63 |
+
ok = st.button("Predict")
|
64 |
+
if ok:
|
65 |
+
# st.write("true")
|
66 |
+
st.write("Predicted Product Description")
|
67 |
+
matches = find_text_matches(clip_model,embeddings,image,caption_df['caption'].values)
|
68 |
+
for i in matches:
|
69 |
+
st.write(i)
|
70 |
+
|
71 |
+
|
72 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
opencv-python==4.8.0.74
|
2 |
+
timm==0.9.7
|
3 |
+
torch==2.0.0
|
4 |
+
pandas==2.0.3
|
5 |
+
numpy==1.23.5
|
6 |
+
albumentations==1.3.1
|
7 |
+
matplotlib==3.7.2
|
8 |
+
tqdm==4.66.1
|
9 |
+
transformers==4.33.0
|
10 |
+
streamlit==1.28.2
|