Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModel, AutoTokenizer
|
| 3 |
-
import
|
| 4 |
|
| 5 |
# Load a small CPU model for text to vector processing
|
| 6 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
|
@@ -8,25 +8,50 @@ model = AutoModel.from_pretrained(model_name)
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
|
| 10 |
def text_to_vector(texts):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
return
|
| 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
|
| 29 |
description="This demo converts an array of sentences to vectors and returns objects with sentence and vector."
|
| 30 |
)
|
| 31 |
|
| 32 |
demo.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModel, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
# Load a small CPU model for text to vector processing
|
| 6 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
|
|
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
|
| 10 |
def text_to_vector(texts):
|
| 11 |
+
results = []
|
| 12 |
+
|
| 13 |
+
# Process each sentence individually to catch errors
|
| 14 |
+
for sentence in texts:
|
| 15 |
+
try:
|
| 16 |
+
# Tokenize the sentence
|
| 17 |
+
inputs = tokenizer(sentence, return_tensors="pt", padding=True, truncation=True)
|
| 18 |
+
|
| 19 |
+
# Check if tokenization results in valid tokens
|
| 20 |
+
if inputs['input_ids'].shape[1] == 0:
|
| 21 |
+
raise ValueError(f"Tokenization failed for sentence: '{sentence}'")
|
| 22 |
+
|
| 23 |
+
# Pass through the model
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
outputs = model(**inputs)
|
| 26 |
+
|
| 27 |
+
# Get the vector from pooler_output or handle errors
|
| 28 |
+
if outputs.pooler_output is None:
|
| 29 |
+
raise ValueError(f"No vector generated for sentence: '{sentence}'")
|
| 30 |
+
|
| 31 |
+
# Convert the vector to a list of floats
|
| 32 |
+
vector = outputs.pooler_output.squeeze().numpy().tolist()
|
| 33 |
+
|
| 34 |
+
# Append result as sentence and vector pair
|
| 35 |
+
results.append({
|
| 36 |
+
"sentence": sentence,
|
| 37 |
+
"vector": vector
|
| 38 |
+
})
|
| 39 |
+
except Exception as e:
|
| 40 |
+
# Handle any errors for individual sentences
|
| 41 |
+
results.append({
|
| 42 |
+
"sentence": sentence,
|
| 43 |
+
"vector": f"Error: {str(e)}"
|
| 44 |
+
})
|
| 45 |
|
| 46 |
+
return results
|
| 47 |
|
| 48 |
demo = gr.Interface(
|
| 49 |
fn=text_to_vector,
|
| 50 |
inputs=gr.Textbox(label="Enter JSON array", placeholder="Enter an array of sentences as a JSON string"),
|
| 51 |
outputs=gr.JSON(label="Sentence and Vector Pairs"),
|
| 52 |
+
title="Batch Text to Vector",
|
| 53 |
description="This demo converts an array of sentences to vectors and returns objects with sentence and vector."
|
| 54 |
)
|
| 55 |
|
| 56 |
demo.launch()
|
| 57 |
+
|