-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
89 lines (70 loc) · 3.48 KB
/
Copy pathapp.py
File metadata and controls
89 lines (70 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# pip install tensorflow==2.15.0
# pip install torch==2.0.1
# pip install sentence_transformers==2.2.2
# pip install streamlit
# import libraries===================================
import streamlit as st
import torch
from sentence_transformers import util
import pickle
from tensorflow.keras.layers import TextVectorization
import numpy as np
from tensorflow import keras
# load save recommendation models===================================
embeddings = pickle.load(open('models/embeddings.pkl','rb'))
sentences = pickle.load(open('models/sentences.pkl','rb'))
rec_model = pickle.load(open('models/rec_model.pkl','rb'))
# load save prediction models============================
# Load the model
loaded_model = keras.models.load_model("models/model.h5")
# Load the configuration of the text vectorizer
with open("models/text_vectorizer_config.pkl", "rb") as f:
saved_text_vectorizer_config = pickle.load(f)
# Create a new TextVectorization layer with the saved configuration
loaded_text_vectorizer = TextVectorization.from_config(saved_text_vectorizer_config)
# Load the saved weights into the new TextVectorization layer
with open("models/text_vectorizer_weights.pkl", "rb") as f:
weights = pickle.load(f)
loaded_text_vectorizer.set_weights(weights)
# Load the vocabulary
with open("models/vocab.pkl", "rb") as f:
loaded_vocab = pickle.load(f)
# custom functions====================================
def recommendation(input_paper):
# Calculate cosine similarity scores between the embeddings of input_paper and all papers in the dataset.
cosine_scores = util.cos_sim(embeddings, rec_model.encode(input_paper))
# Get the indices of the top-k most similar papers based on cosine similarity.
top_similar_papers = torch.topk(cosine_scores, dim=0, k=5, sorted=True)
# Retrieve the titles of the top similar papers.
papers_list = []
for i in top_similar_papers.indices:
papers_list.append(sentences[i.item()])
return papers_list
#=======subject area prediction funtions=================
def invert_multi_hot(encoded_labels):
"""Reverse a single multi-hot encoded label to a tuple of vocab terms."""
hot_indices = np.argwhere(encoded_labels == 1.0)[..., 0]
return np.take(loaded_vocab, hot_indices)
def predict_category(abstract, model, vectorizer, label_lookup):
# Preprocess the abstract using the loaded text vectorizer
preprocessed_abstract = vectorizer([abstract])
# Make predictions using the loaded model
predictions = model.predict(preprocessed_abstract)
# Convert predictions to human-readable labels
predicted_labels = label_lookup(np.round(predictions).astype(int)[0])
return predicted_labels
# create app=========================================
st.title('Research Papers Recommendation and Subject Area Prediction App')
st.write("LLM and Deep Learning Base App")
input_paper = st.text_input("Enter Paper title.....")
new_abstract = st.text_area("Past paper abstract....")
if st.button("Recommend"):
# recommendation part
recommend_papers = recommendation(input_paper)
st.subheader("Recommended Papers")
st.write(recommend_papers)
#========prediction part
st.write("===================================================================")
predicted_categories = predict_category(new_abstract, loaded_model, loaded_text_vectorizer, invert_multi_hot)
st.subheader("Predicted Subject area")
st.write(predicted_categories)