Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,48 @@
|
|
1 |
-
|
2 |
import torch
|
3 |
import torch.nn.functional as F
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
10 |
-
|
11 |
-
|
12 |
-
# Sentences we want sentence embeddings for
|
13 |
-
sentences = ['This is an example sentence', 'Each sentence is converted']
|
14 |
-
|
15 |
-
# Load model from HuggingFace Hub
|
16 |
-
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
|
17 |
-
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
|
18 |
-
|
19 |
-
# Tokenize sentences
|
20 |
-
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
21 |
-
|
22 |
-
# Compute token embeddings
|
23 |
-
with torch.no_grad():
|
24 |
-
model_output = model(**encoded_input)
|
25 |
-
|
26 |
-
# Perform pooling
|
27 |
-
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
28 |
-
|
29 |
-
# Normalize embeddings
|
30 |
-
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
|
31 |
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
import torch
|
3 |
import torch.nn.functional as F
|
4 |
+
import gradio as gr
|
5 |
+
from transformers import AutoTokenizer, AutoModel
|
6 |
|
7 |
+
# βββ Model loading βββ
|
8 |
+
MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
10 |
+
model = AutoModel.from_pretrained(MODEL_NAME)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
def mean_pooling(model_output, attention_mask):
|
13 |
+
token_embeddings = model_output[0] # first element: token embeddings
|
14 |
+
mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
15 |
+
return torch.sum(token_embeddings * mask_expanded, 1) / torch.clamp(mask_expanded.sum(1), min=1e-9)
|
16 |
+
|
17 |
+
# βββ Inference function βββ
|
18 |
+
def compute_similarity(sent1: str, sent2: str) -> float:
|
19 |
+
# Tokenize
|
20 |
+
encoded = tokenizer([sent1, sent2], padding=True, truncation=True, return_tensors="pt")
|
21 |
+
# Forward pass
|
22 |
+
with torch.no_grad():
|
23 |
+
output = model(**encoded)
|
24 |
+
# Pool & normalize
|
25 |
+
embeddings = mean_pooling(output, encoded["attention_mask"])
|
26 |
+
embeddings = F.normalize(embeddings, p=2, dim=1)
|
27 |
+
# Cosine similarity
|
28 |
+
sim = float((embeddings[0] @ embeddings[1]).item())
|
29 |
+
return round(sim, 4)
|
30 |
+
|
31 |
+
# βββ Gradio interface βββ
|
32 |
+
demo = gr.Interface(
|
33 |
+
fn=compute_similarity,
|
34 |
+
inputs=[
|
35 |
+
gr.Textbox(label="Sentence 1", lines=2, placeholder="Enter first sentence..."),
|
36 |
+
gr.Textbox(label="Sentence 2", lines=2, placeholder="Enter second sentence...")
|
37 |
+
],
|
38 |
+
outputs=gr.Number(label="Cosine Similarity"),
|
39 |
+
title="Sentence Embedding Similarity",
|
40 |
+
description="Compute the cosine similarity between the embeddings of two sentences using MiniLM."
|
41 |
+
)
|
42 |
+
|
43 |
+
if __name__ == "__main__":
|
44 |
+
demo.launch(
|
45 |
+
server_name="0.0.0.0",
|
46 |
+
server_port=int(os.environ.get("PORT", 7860)),
|
47 |
+
share=False
|
48 |
+
)
|