disha81 commited on
Commit
75596a7
·
verified ·
1 Parent(s): 6c88eca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
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
- print("MPS is available. Using M1 GPU!")
 
 
 
 
10
  else:
11
- print("MPS not available. Check your PyTorch installation.")
 
 
12
 
13
  model_name = 'all-MiniLM-L6-v2'
14
- model = SentenceTransformer(model_name, device='mps') # Use 'mps' to utilize the M1 chip
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)