A-Roucher commited on
Commit
63db6ac
1 Parent(s): f3c2c3b

Add application file

Browse files
Files changed (2) hide show
  1. README.md +3 -3
  2. app.py +57 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Quotes
3
- emoji: 🏢
4
- colorFrom: purple
5
- colorTo: purple
6
  sdk: streamlit
7
  sdk_version: 1.28.1
8
  app_file: app.py
 
1
  ---
2
  title: Quotes
3
+ emoji: 🪶
4
+ colorFrom: green
5
+ colorTo: blue
6
  sdk: streamlit
7
  sdk_version: 1.28.1
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from sentence_transformers import SentenceTransformer
3
+ import datasets
4
+
5
+ x = st.slider('Select a value')
6
+ st.write(x, 'squared is', x * x)
7
+
8
+ st.sidebar.text_input("Type your quote here")
9
+
10
+ dataset = datasets.load_dataset('A-Roucher/english_historical_quotes')['train']
11
+
12
+ model_name = "sentence-transformers/all-MiniLM-L6-v2" # BAAI/bge-small-en-v1.5" # "Cohere/Cohere-embed-english-light-v3.0" # "sentence-transformers/all-MiniLM-L6-v2"
13
+
14
+ encoder = SentenceTransformer(model_name)
15
+ embeddings = encoder.encode(
16
+ dataset["quote"],
17
+ batch_size=8,
18
+ show_progress_bar=True,
19
+ convert_to_numpy=True,
20
+ normalize_embeddings=True,
21
+ )
22
+
23
+ dataset_embeddings = datasets.Dataset.from_dict({"embeddings": embeddings})
24
+ dataset_embeddings.add_faiss_index(column="embeddings")
25
+
26
+ # dataset_embeddings.save_faiss_index('embeddings', 'output/index_alone.faiss')
27
+
28
+ # import faiss
29
+
30
+ # index = faiss.read_index('index_alone.faiss')
31
+
32
+ sentence = "Knowledge of history is power."
33
+ sentence_embedding = encoder.encode([sentence])
34
+ # scores, samples = dataset_embeddings.search(
35
+ # sentence_embedding, k=10
36
+ # )
37
+
38
+ from sentence_transformers.util import semantic_search
39
+
40
+ # hits = semantic_search(sentence_embedding, dataset_embeddings[:, :], top_k=5)
41
+ author_indexes = range(1000)
42
+ hits = semantic_search(sentence_embedding, dataset_embeddings[author_indexes, :], top_k=5)
43
+
44
+ list_hits = [author_indexes[i['corpus_id']] for i in hits[0]]
45
+ st.write(dataset_embeddings.select([12676, 4967, 2612, 8884, 4797]))
46
+
47
+
48
+
49
+ # sentence_embedding = model.encode([sentence])
50
+ # scores, sample_indexes = QUOTES_INDEX.search(
51
+ # sentence_embedding, k=k
52
+ # )
53
+ # quotes = QUOTES_DATASET.iloc[sample_indexes[0]]
54
+ # author_descriptions_df = get_authors_descriptions(quotes['author'].unique())
55
+ # quotes = quotes.merge(author_descriptions_df, on='author')
56
+ # quotes["scores"] = scores[0]
57
+ # quotes = quotes.sort_values("scores", ascending=True) # lower is better