Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,23 @@
|
|
| 1 |
import torch
|
| 2 |
-
from altair import Description
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
from datasets import load_dataset
|
| 5 |
from sentence_transformers.util import cos_sim
|
| 6 |
import gradio as gr
|
| 7 |
|
|
|
|
| 8 |
if torch.backends.mps.is_available():
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
else:
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
model_name = 'all-MiniLM-L6-v2'
|
| 14 |
-
model = SentenceTransformer(model_name, device=
|
| 15 |
print("Loading dataset...")
|
| 16 |
|
| 17 |
ds = load_dataset("pszemraj/goodreads-bookgenres", "default")
|
|
@@ -62,4 +68,4 @@ gr_interface = gr.Interface(
|
|
| 62 |
examples=["A thrilling detective story", "A heartwarming novel about friendship", "Science fiction about space travel"]
|
| 63 |
)
|
| 64 |
|
| 65 |
-
gr_interface.launch(share=True)
|
|
|
|
| 1 |
import torch
|
|
|
|
| 2 |
from sentence_transformers import SentenceTransformer
|
| 3 |
from datasets import load_dataset
|
| 4 |
from sentence_transformers.util import cos_sim
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# --- FIX: Dynamically select the device ---
|
| 8 |
if torch.backends.mps.is_available():
|
| 9 |
+
device = torch.device("mps")
|
| 10 |
+
print("MPS device is available. Using M1/M2 GPU!")
|
| 11 |
+
elif torch.cuda.is_available():
|
| 12 |
+
device = torch.device("cuda")
|
| 13 |
+
print("CUDA device is available. Using NVIDIA GPU!")
|
| 14 |
else:
|
| 15 |
+
device = torch.device("cpu")
|
| 16 |
+
print("No GPU available. Falling back to CPU.")
|
| 17 |
+
|
| 18 |
|
| 19 |
model_name = 'all-MiniLM-L6-v2'
|
| 20 |
+
model = SentenceTransformer(model_name, device=device)
|
| 21 |
print("Loading dataset...")
|
| 22 |
|
| 23 |
ds = load_dataset("pszemraj/goodreads-bookgenres", "default")
|
|
|
|
| 68 |
examples=["A thrilling detective story", "A heartwarming novel about friendship", "Science fiction about space travel"]
|
| 69 |
)
|
| 70 |
|
| 71 |
+
gr_interface.launch(share=True)
|