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()