Update app.py
Browse files
app.py
CHANGED
@@ -8,23 +8,25 @@ model = AutoModel.from_pretrained(model_name)
|
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
|
10 |
def text_to_vector(texts):
|
11 |
-
#
|
12 |
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
|
13 |
outputs = model(**inputs)
|
14 |
vectors = outputs.pooler_output.detach().numpy()
|
15 |
|
16 |
-
# Convert each vector to a string representation
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
demo = gr.Interface(
|
21 |
fn=text_to_vector,
|
22 |
inputs=gr.Textbox(label="Enter JSON array", placeholder="Enter an array of sentences as a JSON string"),
|
23 |
-
outputs=gr.
|
24 |
-
title="Batch Text to Vector",
|
25 |
-
description="This demo converts an array of sentences to vectors."
|
26 |
)
|
27 |
|
28 |
demo.launch()
|
29 |
-
|
30 |
-
|
|
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
|
10 |
def text_to_vector(texts):
|
11 |
+
# Tokenize the input array of sentences
|
12 |
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
|
13 |
outputs = model(**inputs)
|
14 |
vectors = outputs.pooler_output.detach().numpy()
|
15 |
|
16 |
+
# Convert each vector to a string representation and create an object
|
17 |
+
result = [
|
18 |
+
{"sentence": sentence, "vector": ", ".join(map(str, vector))}
|
19 |
+
for sentence, vector in zip(texts, vectors)
|
20 |
+
]
|
21 |
+
|
22 |
+
return result
|
23 |
|
24 |
demo = gr.Interface(
|
25 |
fn=text_to_vector,
|
26 |
inputs=gr.Textbox(label="Enter JSON array", placeholder="Enter an array of sentences as a JSON string"),
|
27 |
+
outputs=gr.JSON(label="Sentence and Vector Pairs"),
|
28 |
+
title="Batch Text to Vector 769 dim",
|
29 |
+
description="This demo converts an array of sentences to vectors and returns objects with sentence and vector."
|
30 |
)
|
31 |
|
32 |
demo.launch()
|
|
|
|