File size: 1,135 Bytes
718e3b3
 
 
 
 
 
 
a95d821
 
 
 
 
 
 
 
 
 
 
1bab135
718e3b3
 
 
 
a95d821
 
718e3b3
 
 
 
 
 
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
import gradio as gr
from sentence_transformers import SentenceTransformer

# Load the pre-trained model
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')

def get_embeddings(sentences):
    # Check if the input is a list
    if isinstance(sentences, list):
        sentence_list = sentences
    elif isinstance(sentences, str):
        # If it's a string, split by new lines to create a list of sentences
        sentence_list = sentences.split("\n")
    else:
        raise ValueError("Input should be either a string or a list of strings.")
    
    # Generate embeddings
    embeddings = model.encode(sentence_list, convert_to_tensor=True)
    return str(embeddings.tolist())

# Define the Gradio interface
interface = gr.Interface(
    fn=get_embeddings,  # Function to call
    inputs=gr.Textbox(lines=5, placeholder="Enter sentences here, one per line"),  # Input component
    outputs=gr.Textbox(label="Embeddings"),  # Output component
    title="Sentence Embeddings",  # Interface title
    description="Enter sentences to get their embeddings."  # Description
)

# Launch the interface
interface.launch()