File size: 1,524 Bytes
af9db04
 
 
 
 
 
 
 
 
 
 
 
 
 
35d7008
af9db04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import gradio as gr
from sentence_transformers import SentenceTransformer
import numpy as np

# Download from the 🤗 Hub
model = SentenceTransformer("syubraj/sentence_similarity_nepali_v2")

def calculate_similarity(sentence1, sentence2):
    # Encode the sentences
    embeddings = model.encode([sentence1, sentence2])
    
    # Calculate cosine similarity
    similarity = np.dot(embeddings[0], embeddings[1]) / (np.linalg.norm(embeddings[0]) * np.linalg.norm(embeddings[1]))
    
    return f"समानता स्कोर: {similarity:.4f}"

# Define example inputs
examples = [
    ["रातो, डबल डेकर बस।", "रातो डबल डेकर बस।"],
    ["दुई कालो कुकुर हिउँमा हिंड्दै।", "तीन सेतो बिरालो घाँसमा बसिरहेको।"],
    ["आज मौसम सफा र घाम लागेको छ।", "आकाश निलो र घाम चम्किलो छ।"],
]

# Create Gradio interface
iface = gr.Interface(
    fn=calculate_similarity,
    inputs=[
        gr.Textbox(label="Enter the first sentence:"),
        gr.Textbox(label="Enter the sentence to compare:")
    ],
    outputs=gr.Textbox(label="Result"),
    title="Nepali Sentence Similarity Calculator",
    description="Compare the similarity between two Nepali sentences using the syubraj/sentence_similarity_nepali_v2 model.",
    examples=examples
)

# Launch the interface
iface.launch()