Suraj-Yadav commited on
Commit
0368061
β€’
1 Parent(s): 38fb16b

Uploading food not food text classifier demo app

Browse files
Files changed (1) hide show
  1. app.py +16 -0
app.py CHANGED
@@ -7,6 +7,22 @@ from transformers import pipeline
7
 
8
  huggingface_model_path = "Suraj-Yadav/learn_hf_food_not_food_text_classifier-distilbert-base-uncased"
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # 2. Define function to use our model on given text
11
  def food_not_food_classifier(
12
  text: Union[str, list],
 
7
 
8
  huggingface_model_path = "Suraj-Yadav/learn_hf_food_not_food_text_classifier-distilbert-base-uncased"
9
 
10
+
11
+ def set_device() -> torch.device:
12
+ """
13
+ Set the device to the best available option: CUDA (if available), MPS (if available on Mac),
14
+ or CPU as a fallback. Provides a robust selection mechanism for production environments.
15
+
16
+ Returns:
17
+ torch.device: The best available device for computation.
18
+ """
19
+ if torch.cuda.is_available():
20
+ return torch.device("cuda")
21
+ elif torch.backends.mps.is_available() and torch.backends.mps.is_built():
22
+ return torch.device("mps")
23
+ else:
24
+ return torch.device("cpu")
25
+
26
  # 2. Define function to use our model on given text
27
  def food_not_food_classifier(
28
  text: Union[str, list],