Commit
21a79fb
1 Parent(s): 3c22eea

Upload files

Browse files
Files changed (2) hide show
  1. app.py +57 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ from sentence_transformers import SentenceTransformer, util
4
+
5
+ # Load your SentenceTransformer model fine-tuned for NLI
6
+ model = SentenceTransformer("Omartificial-Intelligence-Space/Arabic-Nli-Matryoshka")
7
+
8
+ # Define the labels for NLI
9
+ labels = ["contradiction", "entailment", "neutral"]
10
+
11
+ # Function to compute similarity and classify relationship
12
+ def predict(sentence1, sentence2):
13
+ sentences = [sentence1, sentence2]
14
+ embeddings = model.encode(sentences)
15
+
16
+ # Compute cosine similarity between the two sentences
17
+ similarity_score = util.pytorch_cos_sim(embeddings[0], embeddings[1])
18
+
19
+ # Placeholder logic for NLI (needs to be replaced with actual model inference)
20
+ # This is just an example; in reality, you need a classifier trained for NLI
21
+ scores = np.random.rand(3) # Replace this with actual model prediction logic
22
+ scores = scores / scores.sum() # Normalize to sum to 1
23
+
24
+ label_probs = {labels[i]: float(scores[i]) for i in range(len(labels))}
25
+
26
+ return {
27
+ "Similarity Score": similarity_score.item(),
28
+ "Label Probabilities": label_probs
29
+ }
30
+
31
+ # Define inputs and outputs for Gradio interface
32
+ inputs = [
33
+ gr.inputs.Textbox(lines=2, placeholder="Enter the first sentence here...", label="Sentence 1"),
34
+ gr.inputs.Textbox(lines=2, placeholder="Enter the second sentence here...", label="Sentence 2")
35
+ ]
36
+
37
+ outputs = [
38
+ gr.outputs.Textbox(label="Similarity Score"),
39
+ gr.outputs.Label(num_top_classes=3, label="Label Probabilities")
40
+ ]
41
+
42
+ examples = [
43
+ ["يجلس شاب ذو شعر أشقر على الحائط يقرأ جريدة بينما تمر امرأة وفتاة شابة.", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه"],
44
+ ["الشاب نائم بينما الأم تقود ابنتها إلى الحديقة", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه"]
45
+ ]
46
+
47
+ # Create Gradio interface
48
+ gr.Interface(
49
+ fn=predict,
50
+ title="Arabic Semantic Similarity and NLI with SentenceTransformers",
51
+ description="Compute the semantic similarity and classify the relationship between two Arabic sentences using a SentenceTransformer model.",
52
+ inputs=inputs,
53
+ examples=examples,
54
+ outputs=outputs,
55
+ cache_examples=False,
56
+ article="Author: Your Name. Model from Hugging Face Hub: Omartificial-Intelligence-Space/Arabic-Nli-Matryoshka",
57
+ ).launch(debug=True, enable_queue=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ sentence-transformers
3
+ numpy
4
+ torch