Update app.py
Browse files
app.py
CHANGED
@@ -11,14 +11,18 @@ import sys
|
|
11 |
|
12 |
model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
-
model =
|
15 |
|
16 |
@spaces.GPU
|
17 |
def get_embedding(text):
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
with torch.no_grad():
|
20 |
outputs = model(**inputs)
|
21 |
-
return outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
|
22 |
|
23 |
def compress_to_3d(embedding):
|
24 |
pca = PCA(n_components=3)
|
@@ -52,5 +56,4 @@ iface = gr.Interface(
|
|
52 |
description="Compare the embeddings of two strings visualized in 3D space."
|
53 |
)
|
54 |
|
55 |
-
iface.launch()
|
56 |
-
demo.launch()
|
|
|
11 |
|
12 |
model_name = "sentence-transformers/all-MiniLM-L6-v2"
|
13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
14 |
+
model = None # We'll load the model inside the GPU-enabled function
|
15 |
|
16 |
@spaces.GPU
|
17 |
def get_embedding(text):
|
18 |
+
global model
|
19 |
+
if model is None:
|
20 |
+
model = AutoModel.from_pretrained(model_name).cuda()
|
21 |
+
|
22 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512).to('cuda')
|
23 |
with torch.no_grad():
|
24 |
outputs = model(**inputs)
|
25 |
+
return outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy()
|
26 |
|
27 |
def compress_to_3d(embedding):
|
28 |
pca = PCA(n_components=3)
|
|
|
56 |
description="Compare the embeddings of two strings visualized in 3D space."
|
57 |
)
|
58 |
|
59 |
+
iface.launch()
|
|