0xalfroz commited on
Commit
56518e1
·
verified ·
1 Parent(s): 226cae8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -7,19 +7,23 @@ 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(text):
11
- inputs = tokenizer(text, return_tensors="pt")
 
12
  outputs = model(**inputs)
13
- vector = outputs.pooler_output.detach().numpy()[0]
14
- # Convert to a string representation for display
15
- return ", ".join(map(str, vector))
 
 
16
 
17
  demo = gr.Interface(
18
  fn=text_to_vector,
19
- inputs=gr.Textbox(label="Enter text"),
20
- outputs=gr.Textbox(label="Text Vector"),
21
- title="Text to Vector",
22
- description="This demo uses a small CPU model to convert text to vector."
23
  )
24
 
25
  demo.launch()
 
 
7
  model = AutoModel.from_pretrained(model_name)
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
 
10
+ def text_to_vector(texts):
11
+ # Tokenize and process a batch of inputs
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 text", lines=4, placeholder="Enter one or more texts (separate with new lines)"),
23
+ outputs=gr.Textbox(label="Text Vectors", lines=10),
24
+ title="Batch Text to Vector",
25
+ description="This demo converts a batch of texts to vectors."
26
  )
27
 
28
  demo.launch()
29
+