Commit
c2b54ad
1 Parent(s): ff131c7

update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -7
app.py CHANGED
@@ -2,12 +2,22 @@ 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
 
9
  # Function to compute similarity and classify relationship
10
- def predict(mode, sentence1, sentence2=None, sentence3=None, sentence4=None, dimension="64"):
 
11
  dimension = int(dimension)
12
  result = {
13
  "Selected Dimension": dimension,
@@ -41,9 +51,24 @@ def predict(mode, sentence1, sentence2=None, sentence3=None, sentence4=None, dim
41
  similarity_scores = {"Similarity Score": float(similarity_score)}
42
  result["Similarity Scores"] = similarity_scores
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  return result
45
 
46
  # Define inputs and outputs for Gradio interface
 
47
  mode_dropdown = gr.Dropdown(choices=["Compare two sentences", "Compare one to three"], label="Mode")
48
  dimension_dropdown = gr.Dropdown(choices=["768", "512", "256", "128", "64"], label="Embedding Dimension")
49
  sentence1_input = gr.Textbox(lines=2, placeholder="Enter the first sentence here...", label="Sentence 1")
@@ -51,19 +76,19 @@ sentence2_input = gr.Textbox(lines=2, placeholder="Enter the second sentence her
51
  sentence3_input = gr.Textbox(lines=2, placeholder="Enter the third sentence here...", label="Sentence 3")
52
  sentence4_input = gr.Textbox(lines=2, placeholder="Enter the fourth sentence here...", label="Sentence 4")
53
 
54
- inputs = [mode_dropdown, sentence1_input, sentence2_input, sentence3_input, sentence4_input, dimension_dropdown]
55
  outputs = gr.JSON(label="Detailed Similarity Scores")
56
 
57
  examples = [
58
- ["Compare one to three", "يجلس شاب ذو شعر أشقر على الحائط يقرأ جريدة بينما تمر امرأة وفتاة شابة.", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه", "الشاب نائم بينما الأم تقود ابنتها إلى الحديقة", "رجل يقرأ الجريدة في الحديقة", "64"],
59
- ["Compare two sentences", "يجلس شاب ذو شعر أشقر على الحائط يقرأ جريدة بينما تمر امرأة وفتاة شابة.", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه", None, None, "64"]
60
  ]
61
 
62
  # Create Gradio interface
63
  gr.Interface(
64
  fn=predict,
65
  title="Arabic Sentence Similarity with Matryoshka Model",
66
- description="Compute the semantic similarity between Arabic sentences using the Matryoshka SentenceTransformer model.",
67
  inputs=inputs,
68
  examples=examples,
69
  outputs=outputs,
 
2
  import gradio as gr
3
  from sentence_transformers import SentenceTransformer, util
4
 
5
+ # Available models
6
+ model_dict = {
7
+ "Arabic-mpnet-base-all-nli-triplet": "Omartificial-Intelligence-Space/Arabic-mpnet-base-all-nli-triplet",
8
+ "Arabic-all-nli-triplet-Matryoshka": "Omartificial-Intelligence-Space/Arabic-all-nli-triplet-Matryoshka",
9
+ "Arabert-all-nli-triplet-Matryoshka": "Omartificial-Intelligence-Space/Arabert-all-nli-triplet-Matryoshka",
10
+ "Arabic-labse-Matryoshka": "Omartificial-Intelligence-Space/Arabic-labse-Matryoshka",
11
+ "Marbert-all-nli-triplet-Matryoshka": "Omartificial-Intelligence-Space/Marbert-all-nli-triplet-Matryoshka"
12
+ }
13
 
14
+ # Function to load the selected model
15
+ def load_model(model_name):
16
+ return SentenceTransformer(model_dict[model_name])
17
 
18
  # Function to compute similarity and classify relationship
19
+ def predict(model_name, mode, sentence1, sentence2=None, sentence3=None, sentence4=None, dimension="64"):
20
+ model = load_model(model_name)
21
  dimension = int(dimension)
22
  result = {
23
  "Selected Dimension": dimension,
 
51
  similarity_scores = {"Similarity Score": float(similarity_score)}
52
  result["Similarity Scores"] = similarity_scores
53
 
54
+ # Word-level similarity
55
+ if mode == "Compare two sentences" and sentence2 is not None:
56
+ words1 = sentence1.split()
57
+ words2 = sentence2.split()
58
+ word_pairs = [(w1, w2) for w1 in words1 for w2 in words2]
59
+ word_embeddings1 = model.encode(words1)[..., :dimension]
60
+ word_embeddings2 = model.encode(words2)[..., :dimension]
61
+ word_similarities = {
62
+ f"{w1} - {w2}": float(util.cos_sim(we1, we2))
63
+ for (w1, we1) in zip(words1, word_embeddings1)
64
+ for (w2, we2) in zip(words2, word_embeddings2)
65
+ }
66
+ result["Word-level Similarities"] = word_similarities
67
+
68
  return result
69
 
70
  # Define inputs and outputs for Gradio interface
71
+ model_dropdown = gr.Dropdown(choices=list(model_dict.keys()), label="Model")
72
  mode_dropdown = gr.Dropdown(choices=["Compare two sentences", "Compare one to three"], label="Mode")
73
  dimension_dropdown = gr.Dropdown(choices=["768", "512", "256", "128", "64"], label="Embedding Dimension")
74
  sentence1_input = gr.Textbox(lines=2, placeholder="Enter the first sentence here...", label="Sentence 1")
 
76
  sentence3_input = gr.Textbox(lines=2, placeholder="Enter the third sentence here...", label="Sentence 3")
77
  sentence4_input = gr.Textbox(lines=2, placeholder="Enter the fourth sentence here...", label="Sentence 4")
78
 
79
+ inputs = [model_dropdown, mode_dropdown, sentence1_input, sentence2_input, sentence3_input, sentence4_input, dimension_dropdown]
80
  outputs = gr.JSON(label="Detailed Similarity Scores")
81
 
82
  examples = [
83
+ ["Arabic-all-nli-triplet-Matryoshka", "Compare one to three", "يجلس شاب ذو شعر أشقر على الحائط يقرأ جريدة بينما تمر امرأة وفتاة شابة.", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه", "الشاب نائم بينما الأم تقود ابنتها إلى الحديقة", "رجل يقرأ الجريدة في الحديقة", "64"],
84
+ ["Arabic-all-nli-triplet-Matryoshka", "Compare two sentences", "يجلس شاب ذو شعر أشقر على الحائط يقرأ جريدة بينما تمر امرأة وفتاة شابة.", "ذكر شاب ينظر إلى جريدة بينما تمر إمرأتان بجانبه", None, None, "64"]
85
  ]
86
 
87
  # Create Gradio interface
88
  gr.Interface(
89
  fn=predict,
90
  title="Arabic Sentence Similarity with Matryoshka Model",
91
+ description="Compute the semantic similarity between Arabic sentences using various SentenceTransformer models.",
92
  inputs=inputs,
93
  examples=examples,
94
  outputs=outputs,