Upload 2 files
Browse files- Recommendation_paper.ipynb +0 -0
- recommendation_paper.py +100 -0
Recommendation_paper.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
recommendation_paper.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Recommendation_paper.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/16l0DYZhK4q7tjYvRqpa1IMyIatp1Gtd1
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
!pip install -q -U sentence-transformers
|
| 11 |
+
|
| 12 |
+
import torch
|
| 13 |
+
import pandas as pd
|
| 14 |
+
|
| 15 |
+
from sentence_transformers import SentenceTransformer,util
|
| 16 |
+
|
| 17 |
+
data=pd.read_csv("/content/arxiv_data.csv")
|
| 18 |
+
|
| 19 |
+
data.head(10)
|
| 20 |
+
|
| 21 |
+
titles=data["titles"]
|
| 22 |
+
|
| 23 |
+
titles.head(5)
|
| 24 |
+
|
| 25 |
+
model=SentenceTransformer("all-MiniLM-L6-v2")
|
| 26 |
+
|
| 27 |
+
Encoded_titles=model.encode(titles,device="cuda")
|
| 28 |
+
|
| 29 |
+
keep_counter=0
|
| 30 |
+
for title,encode in zip(titles,Encoded_titles):
|
| 31 |
+
print("Titles",title)
|
| 32 |
+
print("Encoded Data",encode)
|
| 33 |
+
print("length of Encoded Data",len(encode))
|
| 34 |
+
if(keep_counter==5):
|
| 35 |
+
break;
|
| 36 |
+
keep_counter+=1
|
| 37 |
+
|
| 38 |
+
type(titles),type(Encoded_titles)
|
| 39 |
+
|
| 40 |
+
#checking the shape of embeding
|
| 41 |
+
Encoded_titles.shape
|
| 42 |
+
|
| 43 |
+
!pip install huggingface-cli
|
| 44 |
+
|
| 45 |
+
!pip install huggingface_hub
|
| 46 |
+
|
| 47 |
+
from huggingface_hub import notebook_login
|
| 48 |
+
notebook_login()
|
| 49 |
+
|
| 50 |
+
from google.colab import userdata
|
| 51 |
+
userdata.get('secretName')
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
!pip install datasets
|
| 56 |
+
|
| 57 |
+
model.push_to_hub("1998Shubham007/ModelRecomm")
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
#Now save your embedding,Title,model
|
| 62 |
+
import pickle
|
| 63 |
+
|
| 64 |
+
#saving embedding
|
| 65 |
+
with open("embedding.pkl","wb") as f:
|
| 66 |
+
pickle.dump(Encoded_titles,f)
|
| 67 |
+
|
| 68 |
+
with open("ModelRec.pkl","wb") as f:
|
| 69 |
+
pickle.dump(model,f)
|
| 70 |
+
|
| 71 |
+
with open("Titles.pkl","wb") as f:
|
| 72 |
+
pickle.dump(titles,f)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
#Loading the saved Embedding
|
| 77 |
+
with open("/content/embedding.pkl","rb") as f:
|
| 78 |
+
Lencode=pickle.load(f)
|
| 79 |
+
|
| 80 |
+
#Loading the saved Model
|
| 81 |
+
with open("/content/ModelRec.pkl","rb") as f:
|
| 82 |
+
lModelRec=pickle.load(f)
|
| 83 |
+
|
| 84 |
+
#Prediction
|
| 85 |
+
|
| 86 |
+
def recomm(inputPaper):
|
| 87 |
+
encodePaper=lModelRec.encode(inputPaper)
|
| 88 |
+
cosine_score=util.cos_sim(Lencode,encodePaper)
|
| 89 |
+
Top_score=torch.topk(cosine_score,dim=0,k=4)
|
| 90 |
+
paperList=[]
|
| 91 |
+
for i in Top_score.indices:
|
| 92 |
+
paperList.append(titles[i.item()])
|
| 93 |
+
return paperList
|
| 94 |
+
|
| 95 |
+
value=input("enter the paper name")
|
| 96 |
+
|
| 97 |
+
papers=recomm(value)
|
| 98 |
+
|
| 99 |
+
print(papers)
|
| 100 |
+
|