Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- amazon.png +0 -0
- amazon_product.csv +0 -0
- app.py +58 -0
- download.png +0 -0
- requirements.txt +6 -0
amazon.png
ADDED
|
amazon_product.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import nltk
|
| 4 |
+
from nltk.stem.snowball import SnowballStemmer
|
| 5 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 6 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from PIL import Image
|
| 9 |
+
nltk.download('punkt')
|
| 10 |
+
|
| 11 |
+
# Download required NLTK data
|
| 12 |
+
try:
|
| 13 |
+
nltk.download('punkt')
|
| 14 |
+
except:
|
| 15 |
+
pass
|
| 16 |
+
|
| 17 |
+
# Load the dataset
|
| 18 |
+
data = pd.read_csv('amazon_product.csv')
|
| 19 |
+
|
| 20 |
+
# Remove unnecessary columns
|
| 21 |
+
data = data.drop('id', axis=1)
|
| 22 |
+
|
| 23 |
+
# tokenizer and stemmer
|
| 24 |
+
stemmer = SnowballStemmer('english')
|
| 25 |
+
def tokenize_and_stem(text):
|
| 26 |
+
tokens = nltk.word_tokenize(text.lower())
|
| 27 |
+
stems = [stemmer.stem(t) for t in tokens]
|
| 28 |
+
return stems
|
| 29 |
+
|
| 30 |
+
# stemmed tokens column
|
| 31 |
+
data['stemmed_tokens'] = data.apply(lambda row: tokenize_and_stem(row['Title'] + ' ' + row['Description']), axis=1)
|
| 32 |
+
|
| 33 |
+
# TF-IDF vectorizer and cosine similarity function
|
| 34 |
+
tfidf_vectorizer = TfidfVectorizer(tokenizer=tokenize_and_stem)
|
| 35 |
+
def cosine_sim(text1, text2):
|
| 36 |
+
# tfidf_matrix = tfidf_vectorizer.fit_transform([text1, text2])
|
| 37 |
+
text1_concatenated = ' '.join(text1)
|
| 38 |
+
text2_concatenated = ' '.join(text2)
|
| 39 |
+
tfidf_matrix = tfidf_vectorizer.fit_transform([text1_concatenated, text2_concatenated])
|
| 40 |
+
return cosine_similarity(tfidf_matrix)[0][1]
|
| 41 |
+
|
| 42 |
+
# search function
|
| 43 |
+
def search_products(query):
|
| 44 |
+
query_stemmed = tokenize_and_stem(query)
|
| 45 |
+
data['similarity'] = data['stemmed_tokens'].apply(lambda x: cosine_sim(query_stemmed, x))
|
| 46 |
+
results = data.sort_values(by=['similarity'], ascending=False).head(10)[['Title', 'Description', 'Category']]
|
| 47 |
+
return results
|
| 48 |
+
|
| 49 |
+
# web app
|
| 50 |
+
img = Image.open('download.png')
|
| 51 |
+
st.image(img,width=600)
|
| 52 |
+
st.title("Intelligent Product Finder for Amazon")
|
| 53 |
+
query = st.text_input("Enter Product Name")
|
| 54 |
+
sumbit = st.button('Search')
|
| 55 |
+
if sumbit:
|
| 56 |
+
res = search_products(query)
|
| 57 |
+
st.write(res)
|
| 58 |
+
|
download.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
numpy
|
| 3 |
+
nltk
|
| 4 |
+
scikit-learn
|
| 5 |
+
streamlit
|
| 6 |
+
Pillow
|