Omartificial-Intelligence-Space commited on
Commit
ff131c7
1 Parent(s): cce52ef

update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -5,12 +5,21 @@ from sentence_transformers import SentenceTransformer, util
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(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.", {}
@@ -26,11 +35,13 @@ def predict(mode, sentence1, sentence2=None, sentence3=None, sentence4=None, dim
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")
@@ -41,7 +52,7 @@ sentence3_input = gr.Textbox(lines=2, placeholder="Enter the third sentence here
41
  sentence4_input = gr.Textbox(lines=2, placeholder="Enter the fourth sentence here...", label="Sentence 4")
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"],
@@ -57,5 +68,5 @@ gr.Interface(
57
  examples=examples,
58
  outputs=outputs,
59
  cache_examples=False,
60
- article="Author: Your Name. Model from Hugging Face Hub: Omartificial-Intelligence-Space/Arabic-Nli-Matryoshka",
61
  ).launch(debug=True, share=True)
 
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,
14
+ "Input Sentences": {
15
+ "Sentence 1": sentence1,
16
+ "Sentence 2": sentence2,
17
+ "Sentence 3": sentence3,
18
+ "Sentence 4": sentence4
19
+ },
20
+ "Similarity Scores": {}
21
+ }
22
+
23
  if mode == "Compare one to three":
24
  if sentence2 is None or sentence3 is None or sentence4 is None:
25
  return "Please provide three sentences for comparison.", {}
 
35
  if mode == "Compare one to three":
36
  similarities = util.cos_sim(embeddings[0], embeddings[1:])
37
  similarity_scores = {f"Sentence {i+2}": float(similarities[0, i]) for i in range(3)}
38
+ result["Similarity Scores"] = similarity_scores
39
  else:
40
  similarity_score = util.cos_sim(embeddings[0], embeddings[1])
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")
 
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"],
 
68
  examples=examples,
69
  outputs=outputs,
70
  cache_examples=False,
71
+ article="Author: OMER NACAR. Model from Hugging Face Hub: [Omartificial-Intelligence-Space/Arabic-Nli-Matryoshka](https://huggingface.co/Omartificial-Intelligence-Space/Arabic-all-nli-triplet-Matryoshka)",
72
  ).launch(debug=True, share=True)