|
import numpy as np |
|
import gradio as gr |
|
from sentence_transformers import SentenceTransformer, util |
|
|
|
|
|
model = SentenceTransformer("Omartificial-Intelligence-Space/Arabic-Nli-Matryoshka") |
|
|
|
|
|
labels = ["contradiction", "entailment", "neutral"] |
|
|
|
|
|
def predict(sentence1, sentence2): |
|
sentences = [sentence1, sentence2] |
|
embeddings = model.encode(sentences) |
|
|
|
|
|
similarity_score = util.pytorch_cos_sim(embeddings[0], embeddings[1]) |
|
|
|
|
|
|
|
scores = np.random.rand(3) |
|
scores = scores / scores.sum() |
|
|
|
label_probs = {labels[i]: float(scores[i]) for i in range(len(labels))} |
|
|
|
return similarity_score.item(), label_probs |
|
|
|
|
|
inputs = [ |
|
gr.Textbox(lines=2, placeholder="Enter the first sentence here...", label="Sentence 1"), |
|
gr.Textbox(lines=2, placeholder="Enter the second sentence here...", label="Sentence 2") |
|
] |
|
|
|
outputs = [ |
|
gr.Textbox(label="Similarity Score"), |
|
gr.Label(num_top_classes=3, label="Label Probabilities") |
|
] |
|
|
|
examples = [ |
|
["يجلس شاب ذو شعر أشقر على الحائط يقرأ جريدة بينما تمر امرأة وفتاة شابة.", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه"], |
|
["الشاب نائم بينما الأم تقود ابنتها إلى الحديقة", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه"] |
|
] |
|
|
|
|
|
gr.Interface( |
|
fn=predict, |
|
title="Arabic Sentence Similarity and NLI Classification", |
|
description="Compute the semantic similarity and classify the relationship between two Arabic sentences using an Arabic Matryoshka Embedding model.", |
|
inputs=inputs, |
|
examples=examples, |
|
outputs=outputs, |
|
cache_examples=False, |
|
article="Author: OMER NACAR. Model from Hugging Face Hub: Omartificial-Intelligence-Space/Arabic-Nli-Matryoshka", |
|
).launch(debug=True, share=True) |
|
|