Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 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 = "Supabase/gte-small"
|
|
@@ -18,20 +17,17 @@ def text_to_vector(texts_json):
|
|
| 18 |
|
| 19 |
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
|
| 20 |
outputs = model(**inputs)
|
| 21 |
-
vectors = outputs.pooler_output.detach().numpy()
|
| 22 |
-
|
| 23 |
-
# Convert to PostgreSQL-friendly array format
|
| 24 |
-
postgres_array = "{" + ",".join(["{" + ",".join(map(str, v)) + "}" for v in vectors]) + "}"
|
| 25 |
|
| 26 |
-
return
|
| 27 |
-
|
| 28 |
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=text_to_vector,
|
| 31 |
inputs=gr.Textbox(label="Enter JSON array", placeholder="Enter an array of sentences as a JSON string"),
|
| 32 |
-
outputs=gr.Textbox(label="Text Vectors (
|
| 33 |
title="Batch Text to Vector",
|
| 34 |
-
description="This demo converts an array of sentences to vectors and returns them as a
|
| 35 |
)
|
| 36 |
|
| 37 |
demo.launch()
|
|
|
|
| 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 = "Supabase/gte-small"
|
|
|
|
| 17 |
|
| 18 |
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True)
|
| 19 |
outputs = model(**inputs)
|
| 20 |
+
vectors = outputs.pooler_output.detach().numpy() # NumPy array
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Flatten the array and return as a 1D array of floats
|
| 23 |
+
return vectors.reshape(-1).tolist()
|
| 24 |
|
| 25 |
demo = gr.Interface(
|
| 26 |
fn=text_to_vector,
|
| 27 |
inputs=gr.Textbox(label="Enter JSON array", placeholder="Enter an array of sentences as a JSON string"),
|
| 28 |
+
outputs=gr.Textbox(label="Text Vectors (flattened float array)", lines=10),
|
| 29 |
title="Batch Text to Vector",
|
| 30 |
+
description="This demo converts an array of sentences to vectors and returns them as a flattened float array."
|
| 31 |
)
|
| 32 |
|
| 33 |
demo.launch()
|