File size: 1,031 Bytes
0c07570
 
8d7a379
0c07570
 
226cae8
0c07570
 
 
56518e1
 
 
0c07570
56518e1
 
 
 
 
0c07570
 
 
56518e1
 
 
 
0c07570
 
 
56518e1
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
import gradio as gr
from transformers import AutoModel, AutoTokenizer
import numpy as np

# Load a small CPU model for text to vector processing
model_name = "sentence-transformers/all-mpnet-base-v2"
model = AutoModel.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

def text_to_vector(texts):
    # Tokenize and process a batch of inputs
    inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
    outputs = model(**inputs)
    vectors = outputs.pooler_output.detach().numpy()
    
    # Convert each vector to a string representation
    vector_strings = [", ".join(map(str, vector)) for vector in vectors]
    return vector_strings

demo = gr.Interface(
    fn=text_to_vector,
    inputs=gr.Textbox(label="Enter text", lines=4, placeholder="Enter one or more texts (separate with new lines)"),
    outputs=gr.Textbox(label="Text Vectors", lines=10),
    title="Batch Text to Vector",
    description="This demo converts a batch of texts to vectors."
)

demo.launch()