Mollel commited on
Commit
f571279
1 Parent(s): 13e6d97

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ from sentence_transformers import SentenceTransformer, util
4
+
5
+ # Available models
6
+ model_dict = {
7
+ "MultiLinguSwahili-serengeti-E250-nli-matryoshka": "sartifyllc/MultiLinguSwahili-serengeti-E250-nli-matryoshka",
8
+
9
+ }
10
+
11
+ # Function to load the selected model
12
+ def load_model(model_name):
13
+ return SentenceTransformer(model_dict[model_name])
14
+
15
+ # Function to compute similarity and classify relationship
16
+ def predict(model_name, mode, sentence1, sentence2=None, sentence3=None, sentence4=None, dimension="64"):
17
+ model = load_model(model_name)
18
+ dimension = int(dimension)
19
+ result = {
20
+ "Selected Dimension": dimension,
21
+ "Input Sentences": {
22
+ "Sentence 1": sentence1,
23
+ "Sentence 2": sentence2,
24
+ "Sentence 3": sentence3,
25
+ "Sentence 4": sentence4
26
+ },
27
+ "Similarity Scores": {}
28
+ }
29
+
30
+ if mode == "Compare one to three":
31
+ if sentence2 is None or sentence3 is None or sentence4 is None:
32
+ return "Please provide three sentences for comparison.", {}
33
+ sentences = [sentence1, sentence2, sentence3, sentence4]
34
+ else:
35
+ if sentence2 is None:
36
+ return "Please provide the second sentence for comparison.", {}
37
+ sentences = [sentence1, sentence2]
38
+
39
+ embeddings = model.encode(sentences)
40
+ embeddings = embeddings[..., :dimension]
41
+
42
+ if mode == "Compare one to three":
43
+ similarities = util.cos_sim(embeddings[0], embeddings[1:])
44
+ similarity_scores = {f"Sentence {i+2}": float(similarities[0, i]) for i in range(3)}
45
+ result["Similarity Scores"] = similarity_scores
46
+ else:
47
+ similarity_score = util.cos_sim(embeddings[0], embeddings[1])
48
+ similarity_scores = {"Similarity Score": float(similarity_score)}
49
+ result["Similarity Scores"] = similarity_scores
50
+
51
+ # Word-level similarity
52
+ if mode == "Compare two sentences" and sentence2 is not None:
53
+ words1 = sentence1.split()
54
+ words2 = sentence2.split()
55
+ word_pairs = [(w1, w2) for w1 in words1 for w2 in words2]
56
+ word_embeddings1 = model.encode(words1)[..., :dimension]
57
+ word_embeddings2 = model.encode(words2)[..., :dimension]
58
+ word_similarities = {
59
+ f"{w1} - {w2}": float(util.cos_sim(we1, we2))
60
+ for (w1, we1) in zip(words1, word_embeddings1)
61
+ for (w2, we2) in zip(words2, word_embeddings2)
62
+ }
63
+ result["Word-level Similarities"] = word_similarities
64
+
65
+ return result
66
+
67
+ # Define inputs and outputs for Gradio interface
68
+ model_dropdown = gr.Dropdown(choices=list(model_dict.keys()), label="Model")
69
+ mode_dropdown = gr.Dropdown(choices=["Compare two sentences", "Compare one to three"], label="Mode")
70
+ dimension_dropdown = gr.Dropdown(choices=["768", "512", "256", "128", "64"], label="Embedding Dimension")
71
+ sentence1_input = gr.Textbox(lines=2, placeholder="Enter the first sentence here...", label="Sentence 1")
72
+ sentence2_input = gr.Textbox(lines=2, placeholder="Enter the second sentence here...", label="Sentence 2 (or first of three for mode)")
73
+ sentence3_input = gr.Textbox(lines=2, placeholder="Enter the third sentence here...", label="Sentence 3")
74
+ sentence4_input = gr.Textbox(lines=2, placeholder="Enter the fourth sentence here...", label="Sentence 4")
75
+
76
+ inputs = [model_dropdown, mode_dropdown, sentence1_input, sentence2_input, sentence3_input, sentence4_input, dimension_dropdown]
77
+ outputs = gr.JSON(label="Detailed Similarity Scores")
78
+
79
+ examples = [
80
+ ["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"],
81
+ ["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"]
82
+ ]
83
+
84
+ # Create Gradio interface
85
+ gr.Interface(
86
+ fn=predict,
87
+ title="Swahili Sentence Similarity with Matryoshka Model",
88
+ description="Compute the semantic similarity between Swahili sentences using various SentenceTransformer models.",
89
+ inputs=inputs,
90
+ examples=examples,
91
+ outputs=outputs,
92
+ cache_examples=False,
93
+ article="Author: Michael Mollel. Model from Hugging Face Hub (sartify.com): [Swahili-Nli-Matryoshka](https://huggingface.co/sartifyllc/MultiLinguSwahili-serengeti-E250-nli-matryoshka)",
94
+ ).launch(debug=True, share=True)