PBusienei commited on
Commit
28be794
1 Parent(s): 7827f05

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import time
3
+ import pandas as pd
4
+ import numpy as np
5
+ import plotly.express as px
6
+ from plotly.subplots import make_subplots
7
+ import plotly.graph_objects as go
8
+ import matplotlib.pyplot as plt
9
+
10
+ !pip install -U sentence-transformers
11
+
12
+ from sentence_transformers import SentenceTransformer, util
13
+ import numpy as np
14
+ import pandas as pd
15
+
16
+ # Load document embeddings
17
+ doc_emb = np.loadtxt("abstract-embed.txt", dtype=float)
18
+ doc_emb
19
+
20
+ # Load data
21
+ df = pd.read_csv("sessions.csv", usecols=['Unique ID', 'Name', 'Description', 'Activity Code', 'Start Time', 'End Time', 'Location Name'])
22
+ df.head()
23
+
24
+ # Get attributes from dataframe
25
+ docs = list(df["Description"])
26
+ titles = list(df["Name"])
27
+ start_times = list(df["Start Time"])
28
+ end_times = list(df["End Time"])
29
+ locations = list(df["Location Name"])
30
+
31
+
32
+ # Query
33
+ query = input("Enter your query: ")
34
+
35
+ #Encode query and documents
36
+ query_emb = model.encode(query).astype(float)
37
+
38
+ #Compute dot score between query and all document embeddings
39
+ scores = util.dot_score(query_emb, doc_emb.astype(float))[0].cpu().tolist()
40
+
41
+ #Combine docs & scores with other attributes
42
+ doc_score_pairs = list(zip(docs, scores, titles, start_times, end_times, locations))
43
+
44
+ # top_k results to return
45
+ top_k=3
46
+ print(" Your top", top_k, "most similar sessions in the Summit:")
47
+
48
+ #Sort by decreasing score
49
+ doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)
50
+
51
+
52
+ #Output presentation recommendations
53
+ for doc, score, title, start_time, end_time, location in doc_score_pairs[:top_k]:
54
+
55
+ print("Score: %f" %score)
56
+ print("Title: %s" %title)
57
+ print("Abstract: %s" %doc)
58
+ print("Location: %s" %location)
59
+ f"From {start_time} to {end_time}"
60
+ print('\n')
61
+
62
+