dengkane commited on
Commit
418fa4d
1 Parent(s): ad4e8a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -18
app.py CHANGED
@@ -32,26 +32,19 @@ index.add(embeddings)
32
 
33
  # Search for similar documents
34
  query = "训练脚本."
35
- input_ids = tokenizer.encode(query, return_tensors="pt")
36
- with torch.no_grad():
37
- query_embedding = model(input_ids)[0][0].numpy()
38
- k = 2 # Number of similar documents to retrieve
39
- D, I = index.search(query_embedding.reshape(1, -1), k)
40
 
41
- # Print the results
42
- st.write(f"Query: {query}")
43
- for i in range(k):
44
- st.write(f"Rank {i+1}: {texts[I[0][i]]} (similarity score: {D[0][i]})")
45
 
46
-
47
- # Search index for the most similar content
48
- k = 5 # Number of results to retrieve
49
- D, I = index.search(np.array([question_embedding]), k)
50
-
51
- # Display the results
52
- st.write("Top {} similar content:".format(k))
53
- for i in range(k):
54
- st.write("{}: {} : {}".format(i+1, knowledge[I[0][i]], I[0][i]))
 
55
 
56
  st.title('My first app')
57
 
 
32
 
33
  # Search for similar documents
34
  query = "训练脚本."
 
 
 
 
 
35
 
36
+ from sklearn.metrics.pairwise import cosine_similarity
 
 
 
37
 
38
+ query_embedding = model.encode([query])[0]
39
+
40
+ # Compute the cosine similarity between the query embedding and the document embeddings
41
+ similarities = cosine_similarity([query_embedding], embeddings)[0]
42
+
43
+ # Get the index of the most similar document
44
+ most_similar_index = similarities.argmax()
45
+
46
+ # Print the most similar document
47
+ st.write(documents[most_similar_index])
48
 
49
  st.title('My first app')
50