Mynameisju commited on
Commit
637e45f
·
verified ·
1 Parent(s): eb67530

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -7
app.py CHANGED
@@ -6,11 +6,16 @@ import soundfile as sf
6
  import numpy as np
7
  import os
8
 
 
 
 
9
  # Set device (GPU if available, else CPU)
10
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
11
 
12
  # Load Indic Parler-TTS model and tokenizer
13
- model = ParlerTTSForConditionalGeneration.from_pretrained("ai4bharat/indic-parler-tts").to(device)
 
 
14
  tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indic-parler-tts")
15
 
16
  # Supported languages (Indic Parler-TTS officially supports these)
@@ -39,13 +44,17 @@ def generate_speech(text, language, voice_description):
39
 
40
  # Generate audio
41
  try:
42
- generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
43
- audio_arr = generation.cpu().numpy().squeeze()
 
 
 
 
44
 
45
- # Save audio to a temporary file
46
- output_file = "output.wav"
47
- sf.write(output_file, audio_arr, model.config.sampling_rate)
48
- return output_file, None
49
  except Exception as e:
50
  return None, f"Error generating audio: {str(e)}"
51
 
 
6
  import numpy as np
7
  import os
8
 
9
+ # Optimize for CPU usage
10
+ torch.set_num_threads(2) # Optional: adjust this based on your CPU core count
11
+
12
  # Set device (GPU if available, else CPU)
13
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
14
 
15
  # Load Indic Parler-TTS model and tokenizer
16
+ model = ParlerTTSForConditionalGeneration.from_pretrained(
17
+ "ai4bharat/indic-parler-tts", torch_dtype=torch.float32
18
+ ).to(device)
19
  tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indic-parler-tts")
20
 
21
  # Supported languages (Indic Parler-TTS officially supports these)
 
44
 
45
  # Generate audio
46
  try:
47
+ with torch.inference_mode(): # Optimization: avoid gradient tracking
48
+ generation = model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
49
+ audio_arr = generation.cpu().numpy().squeeze()
50
+
51
+ # Normalize audio to avoid clipping
52
+ audio_arr = audio_arr / np.max(np.abs(audio_arr) + 1e-6)
53
 
54
+ # Save audio to a temporary file
55
+ output_file = "output.wav"
56
+ sf.write(output_file, audio_arr, model.config.sampling_rate)
57
+ return output_file, None
58
  except Exception as e:
59
  return None, f"Error generating audio: {str(e)}"
60