innocent-charles commited on
Commit
964330b
1 Parent(s): 8e1de57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -69
app.py CHANGED
@@ -2,95 +2,46 @@ import numpy as np
2
  import gradio as gr
3
  from sentence_transformers import SentenceTransformer, util
4
 
5
- # Available models
6
- model_dict = {
7
- "AviLaBSE" :"sartifyllc/AviLaBSE",
8
- "MultiLinguSwahili-serengeti-E250-nli-matryoshka": "sartifyllc/MultiLinguSwahili-serengeti-E250-nli-matryoshka",
9
- "MultiLinguSwahili-bert-base-sw-cased-nli-matryoshka": "Mollel/MultiLinguSwahili-bert-base-sw-cased-nli-matryoshka",
10
- "swahili-paraphrase-multilingual-mpnet-base-v2-nli-matryoshka": "sartifyllc/swahili-paraphrase-multilingual-mpnet-base-v2-nli-matryoshka",
11
- "bge-base-swahili-matryoshka":"sartifyllc/bge-base-swahili-matryoshka",
12
- "MultiLinguSwahili-bge-small-en-v1.5-nli-matryoshka": "sartifyllc/MultiLinguSwahili-bge-small-en-v1.5-nli-matryoshka",
13
- }
14
-
15
  # Function to load the selected model
16
  def load_model(model_name):
17
- return SentenceTransformer(model_dict[model_name])
18
 
19
  # Function to compute similarity and classify relationship
20
- def predict(model_name, mode, sentence1, sentence2=None, sentence3=None, sentence4=None, dimension="64"):
21
  model = load_model(model_name)
22
- dimension = int(dimension)
23
  result = {
24
- "Selected Dimension": dimension,
25
- "Input Sentences": {
26
- "Sentence 1": sentence1,
27
- "Sentence 2": sentence2,
28
- "Sentence 3": sentence3,
29
- "Sentence 4": sentence4
30
- },
31
  "Similarity Scores": {}
32
  }
33
 
34
- if mode == "Compare one to three":
35
- if sentence2 is None or sentence3 is None or sentence4 is None:
36
- return "Please provide three sentences for comparison.", {}
37
- sentences = [sentence1, sentence2, sentence3, sentence4]
38
- else:
39
- if sentence2 is None:
40
- return "Please provide the second sentence for comparison.", {}
41
- sentences = [sentence1, sentence2]
42
-
43
- embeddings = model.encode(sentences)
44
- embeddings = embeddings[..., :dimension]
45
-
46
- if mode == "Compare one to three":
47
- similarities = util.cos_sim(embeddings[0], embeddings[1:])
48
- similarity_scores = {f"Sentence {i+2}": float(similarities[0, i]) for i in range(3)}
49
- result["Similarity Scores"] = similarity_scores
50
- else:
51
- similarity_score = util.cos_sim(embeddings[0], embeddings[1])
52
- similarity_scores = {"Similarity Score": float(similarity_score)}
53
- result["Similarity Scores"] = similarity_scores
54
-
55
- # Word-level similarity
56
- if mode == "Compare two sentences" and sentence2 is not None:
57
- words1 = sentence1.split()
58
- words2 = sentence2.split()
59
- word_pairs = [(w1, w2) for w1 in words1 for w2 in words2]
60
- word_embeddings1 = model.encode(words1)[..., :dimension]
61
- word_embeddings2 = model.encode(words2)[..., :dimension]
62
- word_similarities = {
63
- f"{w1} - {w2}": float(util.cos_sim(we1, we2))
64
- for (w1, we1) in zip(words1, word_embeddings1)
65
- for (w2, we2) in zip(words2, word_embeddings2)
66
- }
67
- result["Word-level Similarities"] = word_similarities
68
 
69
  return result
70
 
71
  # Define inputs and outputs for Gradio interface
72
- model_name = "AviLABASE", label="Model")
73
- sentence1_input = gr.Textbox(lines=2, placeholder="Enter the first sentence here...", label="Sentence 1")
74
- sentence2_input = gr.Textbox(lines=2, placeholder="Enter the second sentence here...", label="Sentence 2 (or first of three for mode)")
75
- sentence3_input = gr.Textbox(lines=2, placeholder="Enter the third sentence here...", label="Sentence 3")
76
- sentence4_input = gr.Textbox(lines=2, placeholder="Enter the fourth sentence here...", label="Sentence 4")
77
 
78
- inputs = [model_name, sentence1_input, sentence2_input, sentence3_input, sentence4_input]
79
  outputs = gr.JSON(label="Detailed Similarity Scores")
80
 
81
- examples = [
82
- ["MultiLinguSwahili-serengeti-E250-nli-matryoshka", "Compare one to three", "Mtoto mdogo anaruka mikononi mwa mwanamke aliyevalia suti nyeusi ya kuogelea akiwa kwenye dimbwi.", "Mtoto akiruka mikononi mwa mwanamke aliyevalia suti ya kuogelea kwenye dimbwi.", "Mama na binti wakinunua viatu.", "Mtu anashindana katika mashindano ya mbio.", "64"],
83
- ["MultiLinguSwahili-serengeti-E250-nli-matryoshka", "Compare two sentences", "Mwanamume na mwanamke wachanga waliovaa mikoba wanaweka au kuondoa kitu kutoka kwenye mti mweupe wa zamani, huku watu wengine wamesimama au wameketi nyuma.", "tai huruka", None, None, "64"]
84
- ]
85
-
86
  # Create Gradio interface
87
  gr.Interface(
88
  fn=predict,
89
- title="Swahili Sentence Similarity with Matryoshka Model",
90
- description="Compute the semantic similarity between Swahili sentences using various SentenceTransformer models.",
91
  inputs=inputs,
92
- examples=examples,
93
  outputs=outputs,
94
  cache_examples=False,
95
- article="Author: Michael Mollel. Model from Hugging Face Hub (sartify.com): [Swahili-Nli-Matryoshka](https://huggingface.co/sartifyllc/MultiLinguSwahili-serengeti-E250-nli-matryoshka)",
96
  ).launch(debug=True, share=True)
 
2
  import gradio as gr
3
  from sentence_transformers import SentenceTransformer, util
4
 
 
 
 
 
 
 
 
 
 
 
5
  # Function to load the selected model
6
  def load_model(model_name):
7
+ return SentenceTransformer(model_name)
8
 
9
  # Function to compute similarity and classify relationship
10
+ def predict(model_name, original_sentence_input, *sentences_to_compare):
11
  model = load_model(model_name)
 
12
  result = {
13
+ "Model Name": model_name,
14
+ "Original Sentence": original_sentence_input,
15
+ "Sentences to Compare": sentences_to_compare,
 
 
 
 
16
  "Similarity Scores": {}
17
  }
18
 
19
+ if original_sentence_input and sentences_to_compare:
20
+ sentences = [original_sentence_input] + list(sentences_to_compare)
21
+ embeddings = model.encode(sentences)
22
+ original_embedding = embeddings[0]
23
+
24
+ for i, sentence in enumerate(sentences_to_compare, start=1):
25
+ similarity_score = util.pytorch_cos_sim(original_embedding, embeddings[i]).item()
26
+ result["Similarity Scores"][f"Sentence {i}"] = similarity_score
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  return result
29
 
30
  # Define inputs and outputs for Gradio interface
31
+ model_name = gr.Textbox(value="sartifyllc/African-Cross-Lingua-Embeddings-Model", label="Model Name")
32
+ original_sentence_input = gr.Textbox(lines=2, placeholder="Enter the original sentence here...", label="Original Sentence")
33
+ sentence_to_compare_inputs = gr.Textbox(lines=2, placeholder="Enter the sentence you want to compare...", label="Sentence to Compare", elem_id="sentence_to_compare")
 
 
34
 
35
+ inputs = [model_name, original_sentence_input, sentence_to_compare_inputs]
36
  outputs = gr.JSON(label="Detailed Similarity Scores")
37
 
 
 
 
 
 
38
  # Create Gradio interface
39
  gr.Interface(
40
  fn=predict,
41
+ title="African Cross-Lingua Embeddings Model's Demo",
42
+ description="Compute the semantic similarity across various sentences among any African Languages using African-Cross-Lingua-Embeddings-Model.",
43
  inputs=inputs,
 
44
  outputs=outputs,
45
  cache_examples=False,
46
+ allow_flagging="never"
47
  ).launch(debug=True, share=True)