productizationlabs
commited on
Commit
·
9c9ec1e
1
Parent(s):
b65c4b7
Upload app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,16 @@
|
|
1 |
import pandas as pd
|
2 |
from sklearn.metrics.pairwise import cosine_similarity
|
3 |
import gradio as gr
|
4 |
-
|
5 |
def find_similar_items(stock_code):
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
columns='StockCode',
|
19 |
-
values='Quantity',
|
20 |
-
aggfunc='sum'
|
21 |
-
)
|
22 |
-
|
23 |
-
# Update illustration of the matrix, 1 to represent customer have purchased item, 0 to represent customer haven't purchased.
|
24 |
-
CustomerID_Item_matrix = CustomerID_Item_matrix.applymap(lambda x: 1 if x > 0 else 0)
|
25 |
-
|
26 |
-
# Create Item to Item similarity matrix.
|
27 |
-
item_item_similarity_matrix = pd.DataFrame(
|
28 |
-
cosine_similarity(CustomerID_Item_matrix.T)
|
29 |
-
)
|
30 |
-
|
31 |
-
# Update index to corresponding Item Code (StockCode).
|
32 |
-
item_item_similarity_matrix.columns = CustomerID_Item_matrix.T.index
|
33 |
-
item_item_similarity_matrix['StockCode'] = CustomerID_Item_matrix.T.index
|
34 |
-
item_item_similarity_matrix = item_item_similarity_matrix.set_index('StockCode')
|
35 |
-
|
36 |
-
try:
|
37 |
-
# Convert stock_code to integer
|
38 |
-
stock_code = int(stock_code)
|
39 |
-
except ValueError:
|
40 |
-
return "Error: Invalid stock code."
|
41 |
-
|
42 |
-
try:
|
43 |
-
# Find the top 5 most similar items
|
44 |
-
top_5_similar_items = list(
|
45 |
-
item_item_similarity_matrix\
|
46 |
-
.loc[stock_code]\
|
47 |
-
.sort_values(ascending=False)\
|
48 |
-
.iloc[:5]\
|
49 |
-
.index
|
50 |
-
)
|
51 |
-
except KeyError:
|
52 |
-
return "Stock code not found.Please enter a valid stock code"
|
53 |
-
|
54 |
-
# Return the list of similar items of the given StockCode with item Description.
|
55 |
-
results_df = df1a.loc[df1a['StockCode'].isin(top_5_similar_items), 'Description'].drop_duplicates().to_frame().reset_index(drop=True)
|
56 |
-
results_list = results_df.to_string(header=False, index=False).split('\n')
|
57 |
-
results_list.insert(1, '-'*50) # Insert dotted line after the first item
|
58 |
-
return "\n".join(results_list)
|
59 |
-
|
60 |
-
# Set up the interface
|
61 |
-
stock_code_input = gr.inputs.Textbox(label="Enter Stock Code:")
|
62 |
-
output_table = gr.outputs.Textbox(label="Recommended Items")
|
63 |
-
|
64 |
-
gr.Interface(fn=find_similar_items, inputs=stock_code_input, outputs=output_table,theme=gr.themes.Default(primary_hue="slate")).launch()
|
|
|
1 |
import pandas as pd
|
2 |
from sklearn.metrics.pairwise import cosine_similarity
|
3 |
import gradio as gr
|
|
|
4 |
def find_similar_items(stock_code):
|
5 |
+
H='CustomerID';F=False;D=stock_code;C='StockCode'
|
6 |
+
try:I=pd.read_excel('IBCF_Online_Retail.xlsx')
|
7 |
+
except FileNotFoundError:return'Error: Excel file not found.'
|
8 |
+
E=I.dropna(subset=[H]);A=E.pivot_table(index=H,columns=C,values='Quantity',aggfunc='sum');A=A.applymap(lambda x:1 if x>0 else 0);B=pd.DataFrame(cosine_similarity(A.T));B.columns=A.T.index;B[C]=A.T.index;B=B.set_index(C)
|
9 |
+
try:D=int(D)
|
10 |
+
except ValueError:return'Error: Invalid stock code.'
|
11 |
+
try:J=list(B.loc[D].sort_values(ascending=F).iloc[:5].index)
|
12 |
+
except KeyError:return'Stock code not found.Please enter a valid stock code'
|
13 |
+
K=E.loc[E[C].isin(J),'Description'].drop_duplicates().to_frame().reset_index(drop=True);G=K.to_string(header=F,index=F).split('\n');G.insert(1,'-'*50);return '\n'.join(G)
|
14 |
+
stock_code_input=gr.inputs.Textbox(label='Enter Stock Code:')
|
15 |
+
output_table=gr.outputs.Textbox(label='Recommended Items')
|
16 |
+
gr.Interface(fn=find_similar_items,inputs=stock_code_input,outputs=output_table,theme=gr.themes.Default(primary_hue='slate')).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|