preetidav commited on
Commit
36862ca
·
verified ·
1 Parent(s): bd797d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -16
app.py CHANGED
@@ -1,22 +1,38 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
- # Load pre-trained model for CoLA (linguistic acceptability)
5
- model_name = "preetidav/salesforce-similarity-model"
6
- classifier = pipeline("text-classification", model=model_name)
7
 
8
- def classify_sentence(sentence):
9
- result = classifier(sentence)[0]
10
- return f"Label: {result['label']} (Confidence: {result['score']:.2f})"
 
 
 
 
 
 
 
 
11
 
12
  # Create Gradio interface
13
- iface = gr.Interface(
14
- fn=classify_sentence,
15
- inputs=gr.Textbox(lines=2, placeholder="Enter a sentence..."),
16
- outputs="text",
17
- title="Sentence Acceptability Classifier",
18
- description="This model classifies whether a sentence is linguistically acceptable (LABEL_1) or not (LABEL_0).",
19
- )
 
 
 
 
 
 
 
 
20
 
21
- # Launch app
22
- iface.launch()
 
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()