Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,3 +5,38 @@ import streamlit as st
|
|
5 |
from sentence_transformers import SentenceTransformer, util
|
6 |
|
7 |
st.title("Semantic-Search-Transformer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from sentence_transformers import SentenceTransformer, util
|
6 |
|
7 |
st.title("Semantic-Search-Transformer")
|
8 |
+
|
9 |
+
# Importing the Data
|
10 |
+
df = pd.read_csv('medium_articles.csv')
|
11 |
+
|
12 |
+
# Downloading the sentence transformer model
|
13 |
+
|
14 |
+
embedder = SentenceTransformer('all-MiniLM-L6-v2')
|
15 |
+
|
16 |
+
#Predictions
|
17 |
+
# User-Test function (prediction_script.py)
|
18 |
+
# load saved model
|
19 |
+
|
20 |
+
all_embeddings = np.load('mediumArticle_embeddings.npy')
|
21 |
+
|
22 |
+
# Function
|
23 |
+
|
24 |
+
def prediction(query,top_k,corpus_embeddings,df):
|
25 |
+
query_embedding = embedder.encode(query, convert_to_tensor=True)
|
26 |
+
hits = util.semantic_search(query_embedding, corpus_embeddings, top_k=top_k)
|
27 |
+
hits = hits[0] # Get the hits for the first query
|
28 |
+
|
29 |
+
print(f"\nTop {top_k} most similar sentences in corpus:")
|
30 |
+
for hit in hits:
|
31 |
+
hit_id = hit['corpus_id']
|
32 |
+
article_data = df.iloc[hit_id]
|
33 |
+
title = article_data["title"]
|
34 |
+
st.write("-", title, "(Score: {:.4f})".format(hit['score']))
|
35 |
+
|
36 |
+
query = 'Artificial Intelligence and Blockchain'
|
37 |
+
# query = input("Enter the Input Query:- ")
|
38 |
+
# top_sent = int(input("Enter the number of similarity sentences you want: "))
|
39 |
+
top_k = 10
|
40 |
+
prediction(query,top_k,all_embeddings,df)
|
41 |
+
|
42 |
+
|