Final-Project / app.py
Bitha's picture
Upload app.py
6da2908 verified
raw
history blame
4.05 kB
import numpy as np
import pandas as pd
from PIL import Image
from PIL import ImageFile
import urllib.request
from sklearn.metrics import pairwise_distances
from datetime import datetime
import streamlit as st
st.set_option('deprecation.showfileUploaderEncoding', False)
fashion_df = pd.read_csv("./fashion.csv")
boys_extracted_features = np.load('./Boys_ResNet_features.npy')
boys_Productids = np.load('./Boys_ResNet_feature_product_ids.npy')
girls_extracted_features = np.load('./Girls_ResNet_features.npy')
girls_Productids = np.load('./Girls_ResNet_feature_product_ids.npy')
men_extracted_features = np.load('./Men_ResNet_features.npy')
men_Productids = np.load('./Men_ResNet_feature_product_ids.npy')
women_extracted_features = np.load('./Women_ResNet_features.npy')
women_Productids = np.load('./Women_ResNet_feature_product_ids.npy')
fashion_df["ProductId"] = fashion_df["ProductId"].astype(str)
st.image("https://storage.googleapis.com/danacita-website-v3-prd/website_v3/images/biaya_bootcamp__kursus_hacktiv8_6.original.png")
st.markdown('---')
st.subheader('FashClass - HCK-14 Final Project')
st.write('Name :')
st.write('1. Anjas Fajar Maulana (Data Science)')
st.write('2. Fazrin Muhammad (Data Analyst)')
st.write('3. Naufal Andika Ramadhan (Data Engineer)')
st.write('4. Salsa Sabitha Hurriyah (Data Science)')
st.write('---')
def load_data(file_path):
return pd.read_csv(file_path)
# Path to the CSV file
file_path = "fashion.csv"
# Load the data
data = load_data(file_path)
# Display the data using Streamlit
st.write("### List of Product")
st.write(data)
st.write('---')
def get_similar_products_cnn(product_id, num_results):
if(fashion_df[fashion_df['ProductId']==product_id]['Gender'].values[0]=="Boys"):
extracted_features = boys_extracted_features
Productids = boys_Productids
elif(fashion_df[fashion_df['ProductId']==product_id]['Gender'].values[0]=="Girls"):
extracted_features = girls_extracted_features
Productids = girls_Productids
elif(fashion_df[fashion_df['ProductId']==product_id]['Gender'].values[0]=="Men"):
extracted_features = men_extracted_features
Productids = men_Productids
elif(fashion_df[fashion_df['ProductId']==product_id]['Gender'].values[0]=="Women"):
extracted_features = women_extracted_features
Productids = women_Productids
Productids = list(Productids)
doc_id = Productids.index(product_id)
pairwise_dist = pairwise_distances(extracted_features, extracted_features[doc_id].reshape(1,-1))
indices = np.argsort(pairwise_dist.flatten())[0:num_results+1]
pdists = np.sort(pairwise_dist.flatten())[0:num_results+1]
st.write("""
#### input item details
""")
ip_row = fashion_df[['ImageURL','ProductTitle']].loc[fashion_df['ProductId']==Productids[indices[0]]]
for indx, row in ip_row.iterrows():
image = Image.open(urllib.request.urlopen(row['ImageURL']))
image = image.resize((224,224))
st.image(image)
st.write(f"Product Title: {row['ProductTitle']}")
st.write(f"""
#### Top {num_results-1} Recommended items
""")
for i in range(1,len(indices)):
rows = fashion_df[['ImageURL','ProductTitle']].loc[fashion_df['ProductId']==Productids[indices[i]]]
for indx, row in rows.iterrows():
#image = Image.open(Image(url=row['ImageURL'], width = 224, height = 224,embed=True))
image = Image.open(urllib.request.urlopen(row['ImageURL']))
image = image.resize((224,224))
st.image(image)
st.write(f"Product Title: {row['ProductTitle']}")
st.write(f"Euclidean Distance from input image: {pdists[i]}")
st.write("""
## FashClass Recommendation
"""
)
user_input1 = st.text_input("Enter the item id")
user_input2 = st.text_input("Enter number of products to be recommended")
button = st.button('Generate recommendations')
if button:
get_similar_products_cnn(str(user_input1), int(user_input2))