innocent-charles commited on
Commit
d394b95
1 Parent(s): 75adcb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -32
app.py CHANGED
@@ -7,52 +7,54 @@ def load_model(model_name):
7
  return SentenceTransformer(model_name)
8
 
9
  # Function to compute similarity and classify relationship
10
- def predict(original_sentence_input, *sentences_to_compare):
11
  model_name = "sartifyllc/African-Cross-Lingua-Embeddings-Model"
12
  model = load_model(model_name)
13
  result = {
14
  "Model Name": model_name,
15
  "Original Sentence": original_sentence_input,
16
- "Sentences to Compare": sentences_to_compare,
 
 
 
 
17
  "Similarity Scores": {}
18
  }
19
 
20
- if original_sentence_input and sentences_to_compare:
21
- sentences = [original_sentence_input] + list(sentences_to_compare)
22
- embeddings = model.encode(sentences)
23
- original_embedding = embeddings[0]
24
-
25
- for i, sentence in enumerate(sentences_to_compare, start=1):
26
- similarity_score = util.pytorch_cos_sim(original_embedding, embeddings[i]).item()
27
- result["Similarity Scores"][f"Sentence {i}"] = similarity_score
 
 
 
 
 
 
28
 
29
  return result
30
 
31
- # Define inputs and outputs for Gradio interface
32
  model_name_display = gr.Markdown(value="**Model Name**: sartifyllc/African-Cross-Lingua-Embeddings-Model")
33
  original_sentence_input = gr.Textbox(lines=2, placeholder="Enter the original sentence here...", label="Original Sentence")
34
 
35
- # Initial sentence comparison inputs
36
- sentence_to_compare_inputs = [
37
- gr.Textbox(lines=2, placeholder="Enter the sentence you want to compare...", label="Sentence to Compare 1"),
38
- gr.Textbox(lines=2, placeholder="Enter the sentence you want to compare...", label="Sentence to Compare 2"),
39
- gr.Textbox(lines=2, placeholder="Enter the sentence you want to compare...", label="Sentence to Compare 3")
40
- ]
41
 
 
42
  outputs = gr.JSON(label="Detailed Similarity Scores")
43
 
44
- def create_interface():
45
- inputs_dynamic = [model_name_display, original_sentence_input, sentence_to_compare_inputs]
46
- return gr.Interface(
47
- fn=predict,
48
- title="African Cross-Lingua Embeddings Model's Demo",
49
- description="Compute the semantic similarity across various sentences among any African Languages using African-Cross-Lingua-Embeddings-Model.",
50
- inputs=inputs_dynamic,
51
- outputs=outputs,
52
- live=False,
53
- allow_flagging="never"
54
- )
55
-
56
- # Create and launch the initial interface
57
- interface = create_interface()
58
- interface.launch(debug=True, share=True)
 
7
  return SentenceTransformer(model_name)
8
 
9
  # Function to compute similarity and classify relationship
10
+ def predict(model_name_display, original_sentence_input, sentence_1=None, sentence_2=None, sentence_3=None):
11
  model_name = "sartifyllc/African-Cross-Lingua-Embeddings-Model"
12
  model = load_model(model_name)
13
  result = {
14
  "Model Name": model_name,
15
  "Original Sentence": original_sentence_input,
16
+ "Sentences to Compare": {
17
+ "Sentence 1": sentence_1,
18
+ "Sentence 2": sentence_2,
19
+ "Sentence 3": sentence_3
20
+ },
21
  "Similarity Scores": {}
22
  }
23
 
24
+ if not sentence_1 or not sentence_2 or not sentence_3:
25
+ return "Please provide a minimum of three sentences for comparison.", {}
26
+ if not original_sentence_input:
27
+ return "Please provide the original sentence.", {}
28
+
29
+ sentences = [original_sentence_input, sentence_1, sentence_2, sentence_3]
30
+ embeddings = model.encode(sentences)
31
+ similarities = util.cos_sim(embeddings[0], embeddings[1:])
32
+ similarity_scores = {
33
+ "Sentence 1": float(similarities[0, 0]),
34
+ "Sentence 2": float(similarities[0, 1]),
35
+ "Sentence 3": float(similarities[0, 2])
36
+ }
37
+ result["Similarity Scores"] = similarity_scores
38
 
39
  return result
40
 
 
41
  model_name_display = gr.Markdown(value="**Model Name**: sartifyllc/African-Cross-Lingua-Embeddings-Model")
42
  original_sentence_input = gr.Textbox(lines=2, placeholder="Enter the original sentence here...", label="Original Sentence")
43
 
44
+ sentence_1 = gr.Textbox(lines=2, placeholder="Enter the first sentence here...", label="Sentence 1")
45
+ sentence_2 = gr.Textbox(lines=2, placeholder="Enter the second sentence here...", label="Sentence 2")
46
+ sentence_3 = gr.Textbox(lines=2, placeholder="Enter the third sentence here...", label="Sentence 3")
 
 
 
47
 
48
+ inputs = [model_name_display, original_sentence_input, sentence_1, sentence_2, sentence_3]
49
  outputs = gr.JSON(label="Detailed Similarity Scores")
50
 
51
+ # Create Gradio interface
52
+ gr.Interface(
53
+ fn=predict,
54
+ title="African Cross-Lingua Embeddings Model's Demo",
55
+ description="Compute the semantic similarity across various sentences among any African Languages using African-Cross-Lingua-Embeddings-Model.",
56
+ inputs=inputs,
57
+ outputs=outputs,
58
+ cache_examples=False,
59
+ article="Author: Innocent Charles. Model from Hugging Face Hub (sartify.com): [sartifyllc/African-Cross-Lingua-Embeddings-Model](https://huggingface.co/sartifyllc/African-Cross-Lingua-Embeddings-Model)",
60
+ ).launch(debug=True, share=True)