productizationlabs commited on
Commit
7cefc3e
1 Parent(s): a29b06a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -1,15 +1,21 @@
1
- import pandas as pd
2
- from sklearn.metrics.pairwise import cosine_similarity
3
- import gradio as gr
4
- def recommend_items(customer_id_1,customer_id_2):
5
- H='Error';G=customer_id_2;F=customer_id_1;D='StockCode';C='CustomerID'
6
- try:I=pd.read_excel('UBCF_Online_Retail.xlsx')
7
- except FileNotFoundError:return'Error: Excel file not found.'
8
- E=I.dropna(subset=[C]);A=E.pivot_table(index=C,columns=D,values='Quantity',aggfunc='sum');A=A.applymap(lambda x:1 if x>0 else 0);B=pd.DataFrame(cosine_similarity(A));B.columns=A.index;B[C]=A.index;B=B.set_index(C)
9
- try:J=set(A.loc[F].iloc[A.loc[F].to_numpy().nonzero()].index)
10
- except KeyError:return pd.DataFrame({H:['Customer ID 1 is invalid. Please enter a valid Customer ID']})
11
- try:K=set(A.loc[G].iloc[A.loc[G].to_numpy().nonzero()].index)
12
- except KeyError:return pd.DataFrame({H:['Customer ID 2 is invalid. Please enter a valid Customer ID']})
13
- L=J-K;return E.loc[E[D].isin(L),[D,'Description']].drop_duplicates().set_index(D)
14
- iface=gr.Interface(fn=recommend_items,inputs=[gr.inputs.Number(label='Customer ID 1'),gr.inputs.Number(label='Customer ID 2')],outputs=gr.outputs.Dataframe(label='Recommended Items for Customer 2',type='pandas'),theme=gr.themes.Default(primary_hue='slate'),allow_flagging=False)
15
- iface.launch()
 
 
 
 
 
 
 
1
+ _A='countries'
2
+ import gradio as gr,numpy as np,pandas as pd
3
+ from nltk.corpus import stopwords
4
+ from nltk.tokenize import word_tokenize
5
+ from nltk.stem.wordnet import WordNetLemmatizer
6
+ import nltk
7
+ nltk.download('punkt')
8
+ df=pd.read_csv('Hotel_Reviews.csv')
9
+ df[_A]=df.Hotel_Address.apply(lambda x:x.split(' ')[-1])
10
+ def Input_your_destination_and_description(location,description):
11
+ M='Average_Score';L='Hotel_Name';K=False;J='similarity';D=True;C='Tags';B=description;df[_A]=df[_A].str.lower();df[C]=df[C].str.lower();B=B.lower();N=word_tokenize(B);E=stopwords.words('english');F=WordNetLemmatizer();O={A for A in N if not A in E};G=set()
12
+ for P in O:G.add(F.lemmatize(P))
13
+ A=df[df[_A]==location.lower()];A=A.set_index(np.arange(A.shape[0]));H=[]
14
+ for Q in range(A.shape[0]):
15
+ R=word_tokenize(A[C][Q]);S={A for A in R if not A in E};I=set()
16
+ for T in S:I.add(F.lemmatize(T))
17
+ U=I.intersection(G);H.append(len(U))
18
+ A[J]=H;A=A.sort_values(by=J,ascending=K);A.drop_duplicates(subset=L,keep='first',inplace=D);A.sort_values(M,ascending=K,inplace=D);A.reset_index(inplace=D);return A[[L,M,'Hotel_Address']].head(10)
19
+ inputs=[gr.inputs.Textbox(label='Location'),gr.inputs.Textbox(label='Purpose of Travel')]
20
+ outputs=gr.outputs.Dataframe(label='Hotel Recommendations',type='pandas')
21
+ gr.Interface(fn=Input_your_destination_and_description,inputs=inputs,outputs=outputs,theme=gr.themes.Default(primary_hue='slate')).launch()