Commit
954807c
1 Parent(s): c8be4d8

update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -30
app.py CHANGED
@@ -8,50 +8,51 @@ model = SentenceTransformer("Omartificial-Intelligence-Space/Arabic-Nli-Matryosh
8
  # Define the labels for NLI
9
  labels = ["contradiction", "entailment", "neutral"]
10
 
11
- # Define the Matryoshka dimension
12
- matryoshka_dim = 64
13
-
14
  # Function to compute similarity and classify relationship
15
- def predict(sentence1, sentence2):
16
- sentences = [sentence1, sentence2]
17
- embeddings = model.encode(sentences)
18
-
19
- # Shrink the embedding dimensions
20
- embeddings = embeddings[..., :matryoshka_dim]
21
-
22
- # Compute cosine similarity between the two sentences
23
- similarity_score = util.cos_sim(embeddings[0], embeddings[1])
 
24
 
25
- # Placeholder logic for NLI (needs to be replaced with actual model inference)
26
- # This is just an example; in reality, you need a classifier trained for NLI
27
- scores = np.random.rand(3) # Replace this with actual model prediction logic
28
- scores = scores / scores.sum() # Normalize to sum to 1
29
 
30
- label_probs = {labels[i]: float(scores[i]) for i in range(len(labels))}
 
 
 
 
 
31
 
32
- return similarity_score.item(), label_probs
33
 
34
  # Define inputs and outputs for Gradio interface
35
- inputs = [
36
- gr.Textbox(lines=2, placeholder="Enter the first sentence here...", label="Sentence 1"),
37
- gr.Textbox(lines=2, placeholder="Enter the second sentence here...", label="Sentence 2")
38
- ]
 
 
39
 
40
- outputs = [
41
- gr.Textbox(label="Similarity Score"),
42
- gr.Label(num_top_classes=3, label="Label Probabilities")
43
- ]
44
 
45
  examples = [
46
- ["يجلس شاب ذو شعر أشقر على الحائط يقرأ جريدة بينما تمر امرأة وفتاة شابة.", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه"],
47
- ["الشاب نائم بينما الأم تقود ابنتها إلى الحديقة", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه"]
48
  ]
49
 
50
  # Create Gradio interface
51
  gr.Interface(
52
  fn=predict,
53
- title="Arabic Sentence Similarity and NLI Classification",
54
- description="Compute the semantic similarity and classify the relationship between two Arabic sentences using a SentenceTransformer model.",
55
  inputs=inputs,
56
  examples=examples,
57
  outputs=outputs,
 
8
  # Define the labels for NLI
9
  labels = ["contradiction", "entailment", "neutral"]
10
 
 
 
 
11
  # Function to compute similarity and classify relationship
12
+ def predict(mode, sentence1, sentence2=None, sentence3=None, sentence4=None, dimension="64"):
13
+ dimension = int(dimension)
14
+ if mode == "Compare one to three":
15
+ if sentence2 is None or sentence3 is None or sentence4 is None:
16
+ return "Please provide three sentences for comparison.", {}
17
+ sentences = [sentence1, sentence2, sentence3, sentence4]
18
+ else:
19
+ if sentence2 is None:
20
+ return "Please provide the second sentence for comparison.", {}
21
+ sentences = [sentence1, sentence2]
22
 
23
+ embeddings = model.encode(sentences)
24
+ embeddings = embeddings[..., :dimension]
 
 
25
 
26
+ if mode == "Compare one to three":
27
+ similarities = util.cos_sim(embeddings[0], embeddings[1:])
28
+ similarity_scores = {f"Sentence {i+2}": float(similarities[0, i]) for i in range(3)}
29
+ else:
30
+ similarity_score = util.cos_sim(embeddings[0], embeddings[1])
31
+ similarity_scores = {"Similarity Score": float(similarity_score)}
32
 
33
+ return similarity_scores
34
 
35
  # Define inputs and outputs for Gradio interface
36
+ mode_dropdown = gr.Dropdown(choices=["Compare two sentences", "Compare one to three"], label="Mode", default="Compare two sentences")
37
+ dimension_dropdown = gr.inputs.Dropdown(choices=["768", "512", "256", "128", "64"], label="Embedding Dimension", default="64")
38
+ sentence1_input = gr.Textbox(lines=2, placeholder="Enter the first sentence here...", label="Sentence 1")
39
+ sentence2_input = gr.Textbox(lines=2, placeholder="Enter the second sentence here...", label="Sentence 2 (or first of three for mode)", optional=True)
40
+ sentence3_input = gr.Textbox(lines=2, placeholder="Enter the third sentence here...", label="Sentence 3", optional=True)
41
+ sentence4_input = gr.Textbox(lines=2, placeholder="Enter the fourth sentence here...", label="Sentence 4", optional=True)
42
 
43
+ inputs = [mode_dropdown, sentence1_input, sentence2_input, sentence3_input, sentence4_input, dimension_dropdown]
44
+ outputs = gr.JSON(label="Similarity Scores")
 
 
45
 
46
  examples = [
47
+ ["Compare one to three", "يجلس شاب ذو شعر أشقر على الحائط يقرأ جريدة بينما تمر امرأة وفتاة شابة.", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه", "الشاب نائم بينما الأم تقود ابنتها إلى الحديقة", "رجل يقرأ الجريدة في الحديقة", "64"],
48
+ ["Compare two sentences", "يجلس شاب ذو شعر أشقر على الحائط يقرأ جريدة بينما تمر امرأة وفتاة شابة.", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه", None, None, "64"]
49
  ]
50
 
51
  # Create Gradio interface
52
  gr.Interface(
53
  fn=predict,
54
+ title="Arabic Sentence Similarity with Matryoshka Model",
55
+ description="Compute the semantic similarity between Arabic sentences using the Matryoshka SentenceTransformer model.",
56
  inputs=inputs,
57
  examples=examples,
58
  outputs=outputs,