Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModel
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
|
| 6 |
+
# Load model + tokenizer
|
| 7 |
+
model_name = "nikhil061307/contrastive-learning-bert-added-token-v5"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModel.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Function for inference: encode text -> embedding
|
| 12 |
+
def encode(text):
|
| 13 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 14 |
+
with torch.no_grad():
|
| 15 |
+
outputs = model(**inputs)
|
| 16 |
+
# mean pooling over sequence length
|
| 17 |
+
embedding = outputs.last_hidden_state.mean(dim=1)
|
| 18 |
+
return embedding
|
| 19 |
+
|
| 20 |
+
# Compare two sentences
|
| 21 |
+
def compute_similarity(sentence1, sentence2):
|
| 22 |
+
emb1 = encode(sentence1)
|
| 23 |
+
emb2 = encode(sentence2)
|
| 24 |
+
similarity = F.cosine_similarity(emb1, emb2).item()
|
| 25 |
+
return {"similarity": round(similarity, 4)}
|
| 26 |
+
|
| 27 |
+
# Gradio UI
|
| 28 |
+
demo = gr.Interface(
|
| 29 |
+
fn=compute_similarity,
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.Textbox(lines=2, placeholder="Enter first sentence..."),
|
| 32 |
+
gr.Textbox(lines=2, placeholder="Enter second sentence...")
|
| 33 |
+
],
|
| 34 |
+
outputs="json",
|
| 35 |
+
title="Contrastive Learning BERT Similarity",
|
| 36 |
+
description="Enter two sentences to compute their semantic similarity using the fine-tuned BERT model."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
demo.launch()
|