Spaces:
Sleeping
Sleeping
Commit ·
eb4547e
1
Parent(s): 7c12ebc
Optimize CUDA device handling - auto-detect GPU and remove unnecessary device transfers
Browse files
api.py
CHANGED
|
@@ -25,7 +25,11 @@ except ImportError:
|
|
| 25 |
# --- Model Loading ---
|
| 26 |
|
| 27 |
MODEL_ID = "ai4bharat/indic-parler-tts"
|
| 28 |
-
DEVICE = "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
print("Loading Indic Parler-TTS model...")
|
| 31 |
model = ParlerTTSForConditionalGeneration.from_pretrained(MODEL_ID).to(DEVICE)
|
|
@@ -101,7 +105,6 @@ def generate_speech_internal(text, speaker, pitch, rate, temperature, do_sample)
|
|
| 101 |
speaker_name = SPEAKERS.get(speaker, "")
|
| 102 |
gender = "female" if "Female" in speaker or speaker in ["Divya", "Rani"] else "male"
|
| 103 |
description = build_description(speaker_name, gender, pitch, rate)
|
| 104 |
-
model.to("cuda")
|
| 105 |
|
| 106 |
sentences = split_sentences(text)
|
| 107 |
if not sentences:
|
|
@@ -120,10 +123,10 @@ def generate_speech_internal(text, speaker, pitch, rate, temperature, do_sample)
|
|
| 120 |
|
| 121 |
with torch.no_grad():
|
| 122 |
generation = model.generate(
|
| 123 |
-
input_ids=desc_tokens.input_ids.to(
|
| 124 |
-
attention_mask=desc_tokens.attention_mask.to(
|
| 125 |
-
prompt_input_ids=prompt_tokens.input_ids.to(
|
| 126 |
-
prompt_attention_mask=prompt_tokens.attention_mask.to(
|
| 127 |
do_sample=do_sample,
|
| 128 |
temperature=temperature if do_sample else 1.0,
|
| 129 |
min_new_tokens=10,
|
|
@@ -136,8 +139,6 @@ def generate_speech_internal(text, speaker, pitch, rate, temperature, do_sample)
|
|
| 136 |
silence = np.zeros(int(SAMPLE_RATE * 0.3), dtype=np.int16)
|
| 137 |
all_audio.append(silence)
|
| 138 |
|
| 139 |
-
model.to("cpu")
|
| 140 |
-
|
| 141 |
if not all_audio:
|
| 142 |
return None
|
| 143 |
|
|
@@ -146,7 +147,6 @@ def generate_speech_internal(text, speaker, pitch, rate, temperature, do_sample)
|
|
| 146 |
|
| 147 |
except Exception as e:
|
| 148 |
print(f"Error generating speech: {e}")
|
| 149 |
-
model.to("cpu")
|
| 150 |
return None
|
| 151 |
|
| 152 |
|
|
|
|
| 25 |
# --- Model Loading ---
|
| 26 |
|
| 27 |
MODEL_ID = "ai4bharat/indic-parler-tts"
|
| 28 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 29 |
+
|
| 30 |
+
print(f"Using device: {DEVICE}")
|
| 31 |
+
if DEVICE == "cuda":
|
| 32 |
+
print(f"GPU: {torch.cuda.get_device_name(0)}")
|
| 33 |
|
| 34 |
print("Loading Indic Parler-TTS model...")
|
| 35 |
model = ParlerTTSForConditionalGeneration.from_pretrained(MODEL_ID).to(DEVICE)
|
|
|
|
| 105 |
speaker_name = SPEAKERS.get(speaker, "")
|
| 106 |
gender = "female" if "Female" in speaker or speaker in ["Divya", "Rani"] else "male"
|
| 107 |
description = build_description(speaker_name, gender, pitch, rate)
|
|
|
|
| 108 |
|
| 109 |
sentences = split_sentences(text)
|
| 110 |
if not sentences:
|
|
|
|
| 123 |
|
| 124 |
with torch.no_grad():
|
| 125 |
generation = model.generate(
|
| 126 |
+
input_ids=desc_tokens.input_ids.to(DEVICE),
|
| 127 |
+
attention_mask=desc_tokens.attention_mask.to(DEVICE),
|
| 128 |
+
prompt_input_ids=prompt_tokens.input_ids.to(DEVICE),
|
| 129 |
+
prompt_attention_mask=prompt_tokens.attention_mask.to(DEVICE),
|
| 130 |
do_sample=do_sample,
|
| 131 |
temperature=temperature if do_sample else 1.0,
|
| 132 |
min_new_tokens=10,
|
|
|
|
| 139 |
silence = np.zeros(int(SAMPLE_RATE * 0.3), dtype=np.int16)
|
| 140 |
all_audio.append(silence)
|
| 141 |
|
|
|
|
|
|
|
| 142 |
if not all_audio:
|
| 143 |
return None
|
| 144 |
|
|
|
|
| 147 |
|
| 148 |
except Exception as e:
|
| 149 |
print(f"Error generating speech: {e}")
|
|
|
|
| 150 |
return None
|
| 151 |
|
| 152 |
|