devdefalut20 commited on
Commit
fd7ab08
·
verified ·
1 Parent(s): aadbfaa

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModel
3
+ from torch.nn.functional import cosine_similarity
4
+ import gradio as gr
5
+
6
+ model_name = 'bert-base-multilingual-cased'
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModel.from_pretrained(model_name)
9
+
10
+ # Function to compute embeddings
11
+ def compute_embedding(text):
12
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
13
+ with torch.no_grad():
14
+ outputs = model(**inputs)
15
+ embedding = outputs.last_hidden_state.mean(dim=1)
16
+ return embedding
17
+
18
+ # Function to compute similarity between two sentences
19
+ def compare_sentences(text1, text2):
20
+ embedding1 = compute_embedding(text1)
21
+ embedding2 = compute_embedding(text2)
22
+ similarity_score = cosine_similarity(embedding1, embedding2).item()
23
+ return f"Similarity Score: {similarity_score:.4f}"
24
+
25
+ # Gradio interface for input
26
+ iface = gr.Interface(fn=compare_sentences,
27
+ inputs=["text", "text"],
28
+ outputs="text",
29
+ title="Sentence Similarity",
30
+ description="Enter two sentences to compute their similarity.")
31
+
32
+ iface.launch()