Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
| 6 |
-
classifier = pipeline("text-classification", model=model_name)
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Create Gradio interface
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# Launch app
|
| 22 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
import numpy as np
|
| 4 |
|
| 5 |
+
# Load the Sentence Transformer model
|
| 6 |
+
model = SentenceTransformer("preetidav/salesforce-similarity-model")
|
|
|
|
| 7 |
|
| 8 |
+
# Function to generate embeddings
|
| 9 |
+
def get_embedding(sentence):
|
| 10 |
+
embedding = model.encode(sentence)
|
| 11 |
+
return str(embedding) # Convert NumPy array to string for display
|
| 12 |
+
|
| 13 |
+
# Function to calculate similarity between two sentences
|
| 14 |
+
def get_similarity(sentence1, sentence2):
|
| 15 |
+
emb1 = model.encode(sentence1)
|
| 16 |
+
emb2 = model.encode(sentence2)
|
| 17 |
+
similarity = np.dot(emb1, emb2) / (np.linalg.norm(emb1) * np.linalg.norm(emb2))
|
| 18 |
+
return f"Similarity Score: {similarity:.2f}"
|
| 19 |
|
| 20 |
# Create Gradio interface
|
| 21 |
+
with gr.Blocks() as demo:
|
| 22 |
+
gr.Markdown("## Sentence Embedding & Similarity App 🚀")
|
| 23 |
+
|
| 24 |
+
with gr.Tab("Get Sentence Embedding"):
|
| 25 |
+
input_text = gr.Textbox(label="Enter a sentence:")
|
| 26 |
+
output_embedding = gr.Textbox(label="Embedding:")
|
| 27 |
+
btn = gr.Button("Generate Embedding")
|
| 28 |
+
btn.click(get_embedding, inputs=input_text, outputs=output_embedding)
|
| 29 |
+
|
| 30 |
+
with gr.Tab("Compare Sentence Similarity"):
|
| 31 |
+
text1 = gr.Textbox(label="Sentence 1:")
|
| 32 |
+
text2 = gr.Textbox(label="Sentence 2:")
|
| 33 |
+
output_similarity = gr.Textbox(label="Similarity Score:")
|
| 34 |
+
btn2 = gr.Button("Compare")
|
| 35 |
+
btn2.click(get_similarity, inputs=[text1, text2], outputs=output_similarity)
|
| 36 |
|
| 37 |
+
# Launch the Gradio app
|
| 38 |
+
demo.launch()
|