matijap commited on
Commit
41782f4
1 Parent(s): 84f3fa1

Added knowledge bases and simple query with top 5 results as a table.

Browse files
app.py CHANGED
@@ -1,4 +1,51 @@
 
 
 
 
1
  import streamlit as st
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import faiss
2
+ import joblib
3
+ import numpy as np
4
+ import pandas as pd
5
  import streamlit as st
6
+ from sentence_transformers import SentenceTransformer
7
 
8
+ # st.set_page_config(layout="wide")
9
+
10
+
11
+ @st.cache_resource
12
+ def load_model():
13
+ return SentenceTransformer("TamedWicked/MathBERT_hr")
14
+
15
+
16
+ @st.cache_resource
17
+ def load_knowledge_base_df():
18
+ return pd.read_parquet("data/knowledge_base.parquet")
19
+
20
+
21
+ @st.cache_resource
22
+ def load_knowledge_base_index():
23
+ embeddings = joblib.load("data/knowledge_base_embeddings.pkl")
24
+ index = faiss.IndexFlatL2(embeddings.shape[1])
25
+ index.add(embeddings)
26
+ return index
27
+
28
+
29
+ def vector_search(query: list[str], model: SentenceTransformer, index, num_results=10):
30
+ vector = model.encode(list(query), show_progress_bar=False, convert_to_numpy=True)
31
+ D, I = index.search(np.array(vector).astype("float32"), k=num_results)
32
+ return D, I
33
+
34
+
35
+ def show_df_as_html(df: pd.DataFrame):
36
+ return df.to_html()
37
+
38
+
39
+ def show_df_as_markdown(df: pd.DataFrame):
40
+ return df.to_markdown()
41
+
42
+
43
+ model: SentenceTransformer = load_model()
44
+ df: pd.DataFrame = load_knowledge_base_df()
45
+ knowledge_index: np.array = load_knowledge_base_index()
46
+
47
+ query = st.text_input("Your math query:", value="Jesu li strukture koje su elementarno ekvivalentne izomorfne?")
48
+ if query:
49
+ D, I = vector_search([query], model, knowledge_index, num_results=5)
50
+ result = df[["Speech", "start_link"]].iloc[I[0]]
51
+ st.write(show_df_as_markdown(result), unsafe_allow_html=True)
data/knowledge_base.parquet ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1b60bdb181b8841e6d0d2e56f645c7070bf1017d056be4950f0facca9936be2a
3
+ size 999177
data/knowledge_base_embeddings.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6bc7c3a5a91b52295c08bd22289a11d609f9a273e760c381041859bebe0c0223
3
+ size 58875105