ziyadbastaili commited on
Commit
81d5bd9
·
1 Parent(s): 348eeb3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sentence_transformers import SentenceTransformer, util
3
+
4
+ model = None
5
+ def semantic(company_1, company_2):
6
+ # Single list of sentences
7
+ sentences = [company_1, company_2]
8
+ if model is None:
9
+ model = SentenceTransformer('all-mpnet-base-v2')
10
+ #Compute embeddings
11
+ embeddings = model.encode(sentences, convert_to_tensor=True)
12
+ #Compute cosine-similarities for each sentence with each other sentence
13
+ cosine_scores = util.cos_sim(embeddings, embeddings)
14
+ #Find the pairs with the highest cosine similarity scores
15
+ pairs = []
16
+ for i in range(len(cosine_scores)-1):
17
+ for j in range(i+1, len(cosine_scores)):
18
+ pairs.append({'index': [i, j], 'score': cosine_scores[i][j]})
19
+ #Sort scores in decreasing order
20
+ pairs = sorted(pairs, key=lambda x: x['score'], reverse=True)
21
+ for pair in pairs:
22
+ return "{:.4f}".format(pair['score'])
23
+
24
+ company_1 = "Growth Capital Acquisition Corp"
25
+ company_2 = "Growth Capital Acquisition Corp III"
26
+
27
+ title = 'sentences_semantic'
28
+ gr.Interface(semantic,inputs=[gr.inputs.Textbox(lines=1, default=company_1, label="Company_1"), gr.inputs.Textbox(lines=1, default=company_2, label="Company_2")],
29
+ outputs=[gr.outputs.Textbox(type="auto",label="Answer")],title = title).launch()