Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,33 @@
|
|
1 |
-
|
2 |
-
import gradio as gr
|
3 |
-
|
4 |
-
import torch
|
5 |
-
import numpy as np
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
9 |
|
10 |
-
# Load
|
11 |
-
|
12 |
-
model =
|
13 |
|
14 |
-
|
15 |
-
def predict(input_text):
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
# Set the print options to avoid truncation and use fixed-point notation
|
21 |
-
np.set_printoptions(threshold=np.inf, precision=8, suppress=True, floatmode='fixed')
|
22 |
|
23 |
-
# Convert
|
24 |
-
|
25 |
-
|
26 |
-
return embeddings_str
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
gradio_app.launch()
|
|
|
1 |
+
from sentence_transformers import SentenceTransformer
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
|
|
5 |
|
6 |
+
# Function to convert dense vector to binary vector
|
7 |
+
def dense_to_binary(dense_vector):
|
8 |
+
return np.packbits(np.where(dense_vector >= 0, 1, 0)).tobytes()
|
9 |
|
10 |
+
# Load the model
|
11 |
+
device="cuda" if torch.cuda.is_available() else "cpu"
|
12 |
+
model = SentenceTransformer("mixedbread-ai/mxbai-embed-large-v1", device=device)
|
13 |
|
14 |
+
def embed(text:str):
|
|
|
15 |
|
16 |
+
# Float embedding
|
17 |
+
float_vector = model.encode(text, convert_to_numpy=True)
|
|
|
|
|
|
|
18 |
|
19 |
+
# Convert to binary vector
|
20 |
+
binary_vector = dense_to_binary(float_vector)
|
|
|
|
|
21 |
|
22 |
+
# Return both vectors
|
23 |
+
return float_vector, binary_vector
|
24 |
+
|
25 |
+
|
26 |
+
# Gradio interface
|
27 |
+
interface = gr.Interface(
|
28 |
+
fn=embed,
|
29 |
+
inputs=["text"],
|
30 |
+
outputs=["json", "text"]
|
31 |
+
)
|
32 |
|
33 |
+
interface.launch(server_port=7860)
|
|