Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModel, AutoTokenizer
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
|
| 5 |
# Load a small CPU model for text to vector processing
|
| 6 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
| 7 |
model = AutoModel.from_pretrained(model_name)
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
|
| 10 |
-
def text_to_vector(
|
| 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 |
-
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()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModel, AutoTokenizer
|
| 3 |
import numpy as np
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
# Load a small CPU model for text to vector processing
|
| 7 |
model_name = "sentence-transformers/all-mpnet-base-v2"
|
| 8 |
model = AutoModel.from_pretrained(model_name)
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
|
| 11 |
+
def text_to_vector(texts_json):
|
| 12 |
+
try:
|
| 13 |
+
texts = json.loads(texts_json)
|
| 14 |
+
if not isinstance(texts, list):
|
| 15 |
+
raise ValueError("Input must be a JSON array of strings.")
|
| 16 |
+
except json.JSONDecodeError:
|
| 17 |
+
raise ValueError("Invalid JSON format.")
|
| 18 |
+
|
| 19 |
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
|
| 20 |
outputs = model(**inputs)
|
| 21 |
+
vectors = outputs.pooler_output.detach().numpy().tolist() # Convert to list
|
| 22 |
+
return json.dumps(vectors) # Return as JSON string
|
|
|
|
|
|
|
|
|
|
| 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.Textbox(label="Text Vectors (JSON)", lines=10),
|
| 28 |
title="Batch Text to Vector",
|
| 29 |
+
description="This demo converts an array of sentences to vectors and returns them as a JSON array."
|
| 30 |
)
|
| 31 |
|
| 32 |
demo.launch()
|