File size: 2,971 Bytes
50fdd96 062d5b7 50fdd96 964330b 062d5b7 50fdd96 948505c 50fdd96 964330b 50fdd96 964330b 50fdd96 062d5b7 50fdd96 948505c 964330b 062d5b7 c92b6f6 bdd5c64 c92b6f6 bdd5c64 c92b6f6 bdd5c64 c92b6f6 50fdd96 062d5b7 50fdd96 bdd5c64 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import numpy as np
import gradio as gr
from sentence_transformers import SentenceTransformer, util
# Function to load the selected model
def load_model(model_name):
return SentenceTransformer(model_name)
# Function to compute similarity and classify relationship
def predict(original_sentence_input, *sentences_to_compare):
model_name = "sartifyllc/African-Cross-Lingua-Embeddings-Model"
model = load_model(model_name)
result = {
"Model Name": model_name,
"Original Sentence": original_sentence_input,
"Sentences to Compare": sentences_to_compare,
"Similarity Scores": {}
}
if original_sentence_input and sentences_to_compare:
sentences = [original_sentence_input] + list(sentences_to_compare)
embeddings = model.encode(sentences)
original_embedding = embeddings[0]
for i, sentence in enumerate(sentences_to_compare, start=1):
similarity_score = util.pytorch_cos_sim(original_embedding, embeddings[i]).item()
result["Similarity Scores"][f"Sentence {i}"] = similarity_score
return result
# Define inputs and outputs for Gradio interface
model_name_display = gr.Markdown(value="**Model Name**: sartifyllc/African-Cross-Lingua-Embeddings-Model")
original_sentence_input = gr.Textbox(lines=2, placeholder="Enter the original sentence here...", label="Original Sentence")
# Initial sentence comparison inputs
sentence_to_compare_inputs = [
gr.Textbox(lines=2, placeholder="Enter the sentence you want to compare...", label="Sentence to Compare 1"),
gr.Textbox(lines=2, placeholder="Enter the sentence you want to compare...", label="Sentence to Compare 2"),
gr.Textbox(lines=2, placeholder="Enter the sentence you want to compare...", label="Sentence to Compare 3")
]
# Function to dynamically update the interface
def update_interface(num_sentences):
return [gr.Textbox(lines=2, placeholder="Enter the sentence you want to compare...", label=f"Sentence to Compare {i}") for i in range(1, num_sentences + 1)]
num_sentences_input = gr.Slider(minimum=1, maximum=10, step=1, value=3, label="Number of Sentences to Compare")
inputs = [model_name_display, original_sentence_input, num_sentences_input] + sentence_to_compare_inputs
outputs = gr.JSON(label="Detailed Similarity Scores")
# Create Gradio interface
def dynamic_interface(num_sentences):
inputs_dynamic = [model_name_display, original_sentence_input, num_sentences_input] + update_interface(num_sentences)
return gr.Interface(
fn=predict,
title="African Cross-Lingua Embeddings Model's Demo",
description="Compute the semantic similarity across various sentences among any African Languages using African-Cross-Lingua-Embeddings-Model.",
inputs=inputs_dynamic,
outputs=outputs,
live=True
)
# Launch the interface with dynamic updates
interface = dynamic_interface(3)
interface.launch(debug=True, share=True)
|