rocco commited on
Commit
d4e77c6
·
1 Parent(s): 08a464b

Support CPU GPU and ZeroGPU

Browse files
Files changed (2) hide show
  1. app.py +47 -20
  2. requirements.txt +1 -0
app.py CHANGED
@@ -6,6 +6,8 @@ import pickle
6
  import re
7
  from pathlib import Path
8
 
 
 
9
  import gradio as gr
10
  from sentence_transformers import SentenceTransformer, util
11
 
@@ -16,6 +18,29 @@ MAX_CHARS = 5000
16
  logging.basicConfig(level=logging.INFO)
17
 
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def load_descriptions(tsv_path, pickle_path):
20
  # Only load the maintainer's trusted pickle; never accept user-uploaded pickles.
21
  with open(pickle_path, 'rb') as f:
@@ -42,33 +67,35 @@ def load_descriptions(tsv_path, pickle_path):
42
  return labels, [descriptions[label] for label in labels]
43
 
44
 
45
- def create_demo():
46
- if not MODEL_ID:
47
- raise ValueError('Set MODEL_ID in Space Settings → Variables to your trained model repository ID.')
48
- labels, descriptions = load_descriptions(
49
- ROOT / 'data/WorldsLangs.tsv', ROOT / 'data/all_stats.pkl'
50
- )
51
- # Load the trained LIBE checkpoint, not the unmodified base LaBSE model.
52
- model = SentenceTransformer(
53
- MODEL_ID, device='cpu', revision=MODEL_REVISION,
54
- token=os.environ.get('HF_TOKEN') or None,
55
- )
56
- corpus_embeddings = model.encode(
57
- descriptions, convert_to_tensor=True, normalize_embeddings=True,
58
- batch_size=16, show_progress_bar=True,
59
- ).to('cpu')
60
- top_k = min(5, len(labels))
61
 
 
 
 
 
 
62
  def identify(text):
63
  text = (text or '').strip()
64
  if not text:
65
  raise gr.Error('Please enter some text.')
66
  if len(text) > MAX_CHARS:
67
  raise gr.Error(f'Please use at most {MAX_CHARS:,} characters.')
68
- query_embeddings = model.encode(
69
- [text], convert_to_tensor=True, normalize_embeddings=True,
70
- show_progress_bar=False,
71
- ).to('cpu')
72
  hits = util.semantic_search(query_embeddings, corpus_embeddings, top_k=top_k)[0]
73
  best = hits[0]
74
  i = best['corpus_id']
 
6
  import re
7
  from pathlib import Path
8
 
9
+ import spaces # Import before torch / sentence_transformers for ZeroGPU.
10
+ import torch
11
  import gradio as gr
12
  from sentence_transformers import SentenceTransformer, util
13
 
 
18
  logging.basicConfig(level=logging.INFO)
19
 
20
 
21
+ def select_device(zero_gpu_flag, cuda_available):
22
+ is_zero = zero_gpu_flag.strip().lower() in {'1', 'true', 'yes', 'on', 't'}
23
+ return is_zero, 'cuda' if is_zero or cuda_available else 'cpu'
24
+
25
+
26
+ IS_ZERO_GPU, DEVICE = select_device(
27
+ os.environ.get('SPACES_ZERO_GPU', ''), torch.cuda.is_available()
28
+ )
29
+ logging.info('LIBE hardware: %s', 'ZeroGPU' if IS_ZERO_GPU else DEVICE)
30
+
31
+
32
+ @spaces.GPU(duration=30)
33
+ def encode_query(text):
34
+ # CUDA computation occurs inside the allocation on ZeroGPU.
35
+ # The decorator is a passthrough on ordinary CPU/GPU hardware.
36
+ with torch.inference_mode():
37
+ return model.encode(
38
+ [text], convert_to_tensor=True, normalize_embeddings=True,
39
+ show_progress_bar=False, device=DEVICE,
40
+ ).cpu()
41
+
42
+
43
+
44
  def load_descriptions(tsv_path, pickle_path):
45
  # Only load the maintainer's trusted pickle; never accept user-uploaded pickles.
46
  with open(pickle_path, 'rb') as f:
 
67
  return labels, [descriptions[label] for label in labels]
68
 
69
 
70
+ if not MODEL_ID:
71
+ raise ValueError('Set MODEL_ID in Space Settings → Variables to your trained model repository ID.')
72
+ labels, descriptions = load_descriptions(
73
+ ROOT / 'data/WorldsLangs.tsv', ROOT / 'data/all_stats.pkl'
74
+ )
75
+ # Load the trained LIBE checkpoint, not the unmodified base LaBSE model.
76
+ model = SentenceTransformer(
77
+ MODEL_ID, device='cpu' if IS_ZERO_GPU else DEVICE, revision=MODEL_REVISION,
78
+ token=os.environ.get('HF_TOKEN') or None,
79
+ )
80
+ corpus_embeddings = model.encode(
81
+ descriptions, convert_to_tensor=True, normalize_embeddings=True,
82
+ batch_size=16, show_progress_bar=True,
83
+ ).to('cpu')
84
+ top_k = min(5, len(labels))
85
+
86
 
87
+ # Place model on CUDA outside the decorated function; ZeroGPU emulates this
88
+ # allocation until a request receives a real GPU. No GPU inference at startup.
89
+ model.to(DEVICE)
90
+
91
+ def create_demo():
92
  def identify(text):
93
  text = (text or '').strip()
94
  if not text:
95
  raise gr.Error('Please enter some text.')
96
  if len(text) > MAX_CHARS:
97
  raise gr.Error(f'Please use at most {MAX_CHARS:,} characters.')
98
+ query_embeddings = encode_query(text)
 
 
 
99
  hits = util.semantic_search(query_embeddings, corpus_embeddings, top_k=top_k)[0]
100
  best = hits[0]
101
  i = best['corpus_id']
requirements.txt CHANGED
@@ -1 +1,2 @@
1
  sentence-transformers>=3.4,<6
 
 
1
  sentence-transformers>=3.4,<6
2
+ spaces