PuristanLabs1 commited on
Commit
6904c9a
·
verified ·
1 Parent(s): ad22c49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -6,6 +6,13 @@ from transformers import AutoTokenizer
6
  import soundfile as sf
7
  import tempfile
8
 
 
 
 
 
 
 
 
9
  # Supported languages and default settings
10
  languages = {
11
  "Urdu": "A female speaker delivers a clear and expressive speech in Urdu.",
@@ -33,10 +40,10 @@ def generate_description(language, gender, emotion, noise, reverb, expressivity,
33
  # Generate audio function with GPU allocation
34
  @spaces.GPU # Allocate GPU for the duration of this function
35
  def generate_audio(text, description):
36
- # Load model and tokenizer
37
- model = ParlerTTSForConditionalGeneration.from_pretrained("ai4bharat/indic-parler-tts").to("cuda")
38
- tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indic-parler-tts")
39
- description_tokenizer = AutoTokenizer.from_pretrained(model.config.text_encoder._name_or_path)
40
 
41
  # Prepare model inputs
42
  input_ids = description_tokenizer(description, return_tensors="pt").input_ids.to("cuda")
@@ -51,6 +58,9 @@ def generate_audio(text, description):
51
  sf.write(f.name, audio_arr, model.config.sampling_rate)
52
  audio_path = f.name
53
 
 
 
 
54
  return audio_path
55
 
56
  # Gradio Interface
 
6
  import soundfile as sf
7
  import tempfile
8
 
9
+ # Load model and tokenizers at startup (on CPU initially)
10
+ print("Loading model and tokenizers...")
11
+ model = ParlerTTSForConditionalGeneration.from_pretrained("ai4bharat/indic-parler-tts").to("cpu")
12
+ tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indic-parler-tts")
13
+ description_tokenizer = AutoTokenizer.from_pretrained(model.config.text_encoder._name_or_path)
14
+ print("Model and tokenizers loaded.")
15
+
16
  # Supported languages and default settings
17
  languages = {
18
  "Urdu": "A female speaker delivers a clear and expressive speech in Urdu.",
 
40
  # Generate audio function with GPU allocation
41
  @spaces.GPU # Allocate GPU for the duration of this function
42
  def generate_audio(text, description):
43
+ global model # Access the preloaded model
44
+
45
+ # Move model to GPU
46
+ model.to("cuda")
47
 
48
  # Prepare model inputs
49
  input_ids = description_tokenizer(description, return_tensors="pt").input_ids.to("cuda")
 
58
  sf.write(f.name, audio_arr, model.config.sampling_rate)
59
  audio_path = f.name
60
 
61
+ # Move model back to CPU to free GPU memory
62
+ model.to("cpu")
63
+
64
  return audio_path
65
 
66
  # Gradio Interface