Update app.py
Browse files
app.py
CHANGED
|
@@ -8,26 +8,21 @@ 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 |
-
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.
|
| 28 |
title="Batch Text to Vector",
|
| 29 |
-
description="This demo converts an array of sentences to vectors
|
| 30 |
)
|
| 31 |
|
| 32 |
demo.launch()
|
| 33 |
-
|
|
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
|
| 10 |
def text_to_vector(texts):
|
| 11 |
+
# Expect texts to be an 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
|
| 17 |
+
vector_strings = [", ".join(map(str, vector)) for vector in vectors]
|
| 18 |
+
return vector_strings
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.Textbox(label="Text Vectors", lines=10),
|
| 24 |
title="Batch Text to Vector",
|
| 25 |
+
description="This demo converts an array of sentences to vectors."
|
| 26 |
)
|
| 27 |
|
| 28 |
demo.launch()
|
|
|