Tuananh20015 commited on
Commit
e1a4864
·
verified ·
1 Parent(s): 08d3032

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -1,7 +1,6 @@
1
  #!/usr/bin/env python3
2
  """
3
- HuggingFace Space entry point for OmniVoice demo.
4
-
5
  """
6
 
7
  import logging
@@ -15,31 +14,40 @@ logging.basicConfig(
15
  logging.getLogger("omnivoice").setLevel(logging.DEBUG)
16
 
17
  import numpy as np
18
- import spaces
19
  import torch
20
  from omnivoice import OmniVoice, OmniVoiceGenerationConfig
21
  from omnivoice.cli.demo import build_demo
22
 
 
 
 
 
 
 
23
  # ---------------------------------------------------------------------------
24
  # Model loading
25
  # ---------------------------------------------------------------------------
26
  CHECKPOINT = os.environ.get("OMNIVOICE_MODEL", "k2-fsa/OmniVoice")
27
 
28
  print(f"Loading model from {CHECKPOINT} to cpu ...")
 
 
 
 
 
29
  model = OmniVoice.from_pretrained(
30
  CHECKPOINT,
31
  device_map="cpu",
32
- dtype=torch.float16,
33
  load_asr=True,
34
  )
35
  sampling_rate = model.sampling_rate
36
- print("Model loaded successfully!")
37
 
38
  # ---------------------------------------------------------------------------
39
  # Generation logic
40
  # ---------------------------------------------------------------------------
41
 
42
-
43
  def _gen_core(
44
  text,
45
  language,
@@ -58,8 +66,11 @@ def _gen_core(
58
  if not text or not text.strip():
59
  return None, "Please enter the text to synthesize."
60
 
 
 
 
61
  gen_config = OmniVoiceGenerationConfig(
62
- num_step=int(num_step or 32),
63
  guidance_scale=float(guidance_scale) if guidance_scale is not None else 2.0,
64
  denoise=bool(denoise) if denoise is not None else True,
65
  preprocess_prompt=bool(preprocess_prompt),
@@ -89,7 +100,9 @@ def _gen_core(
89
  kw["instruct"] = instruct.strip()
90
 
91
  try:
92
- audio = model.generate(**kw)
 
 
93
  except Exception as e:
94
  return None, f"Error: {type(e).__name__}: {e}"
95
 
@@ -98,11 +111,8 @@ def _gen_core(
98
 
99
 
100
  # ---------------------------------------------------------------------------
101
- # ZeroGPU wrapper
102
  # ---------------------------------------------------------------------------
103
-
104
-
105
- @spaces.GPU(duration=60)
106
  def generate_fn(*args, **kwargs):
107
  return _gen_core(*args, **kwargs)
108
 
 
1
  #!/usr/bin/env python3
2
  """
3
+ HuggingFace Space entry point for OmniVoice demo - OPTIMIZED FOR CPU.
 
4
  """
5
 
6
  import logging
 
14
  logging.getLogger("omnivoice").setLevel(logging.DEBUG)
15
 
16
  import numpy as np
 
17
  import torch
18
  from omnivoice import OmniVoice, OmniVoiceGenerationConfig
19
  from omnivoice.cli.demo import build_demo
20
 
21
+ # --- [TỐI ƯU CPU] Cấu hình Threading cho PyTorch ---
22
+ # Thay số 4 bằng số nhân thực (Physical Cores) của CPU bạn để đạt hiệu năng tốt nhất
23
+ num_cores = os.cpu_count() or 4
24
+ torch.set_num_threads(max(1, num_cores // 2))
25
+ torch.set_num_interop_threads(1)
26
+
27
  # ---------------------------------------------------------------------------
28
  # Model loading
29
  # ---------------------------------------------------------------------------
30
  CHECKPOINT = os.environ.get("OMNIVOICE_MODEL", "k2-fsa/OmniVoice")
31
 
32
  print(f"Loading model from {CHECKPOINT} to cpu ...")
33
+
34
+ # --- [TỐI ƯU CPU] Chuyển đổi dtype sang bfloat16 (hoặc float32 nếu CPU quá cũ) ---
35
+ # bfloat16 chạy rất nhanh trên các CPU hiện đại hỗ trợ AVX-512/AMX
36
+ chosen_dtype = torch.bfloat16 if torch.cuda.is_available() or hasattr(torch, 'bfloat16') else torch.float32
37
+
38
  model = OmniVoice.from_pretrained(
39
  CHECKPOINT,
40
  device_map="cpu",
41
+ dtype=chosen_dtype,
42
  load_asr=True,
43
  )
44
  sampling_rate = model.sampling_rate
45
+ print(f"Model loaded successfully with {chosen_dtype}!")
46
 
47
  # ---------------------------------------------------------------------------
48
  # Generation logic
49
  # ---------------------------------------------------------------------------
50
 
 
51
  def _gen_core(
52
  text,
53
  language,
 
66
  if not text or not text.strip():
67
  return None, "Please enter the text to synthesize."
68
 
69
+ # --- [TỐI ƯU CPU] Mặc định giảm num_step xuống 16 hoặc 20 để chạy nhanh hơn ---
70
+ steps = int(num_step) if num_step else 16
71
+
72
  gen_config = OmniVoiceGenerationConfig(
73
+ num_step=steps,
74
  guidance_scale=float(guidance_scale) if guidance_scale is not None else 2.0,
75
  denoise=bool(denoise) if denoise is not None else True,
76
  preprocess_prompt=bool(preprocess_prompt),
 
100
  kw["instruct"] = instruct.strip()
101
 
102
  try:
103
+ # Tăng tốc tính toán bằng cách tắt autograd
104
+ with torch.no_grad():
105
+ audio = model.generate(**kw)
106
  except Exception as e:
107
  return None, f"Error: {type(e).__name__}: {e}"
108
 
 
111
 
112
 
113
  # ---------------------------------------------------------------------------
114
+ # Wrapper (Bỏ ZeroGPU decorator vì chạy trên CPU local)
115
  # ---------------------------------------------------------------------------
 
 
 
116
  def generate_fn(*args, **kwargs):
117
  return _gen_core(*args, **kwargs)
118