PuristanLabs1 commited on
Commit
ad22c49
1 Parent(s): 69d2a81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -12
app.py CHANGED
@@ -1,16 +1,10 @@
 
1
  import gradio as gr
2
  import torch
3
  from parler_tts import ParlerTTSForConditionalGeneration
4
  from transformers import AutoTokenizer
5
  import soundfile as sf
6
  import tempfile
7
- import spaces # Import the spaces module for ZeroGPU compatibility
8
-
9
- # Load the model and tokenizers
10
- device = "cuda" if torch.cuda.is_available() else "cpu"
11
- model = ParlerTTSForConditionalGeneration.from_pretrained("ai4bharat/indic-parler-tts")
12
- tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indic-parler-tts")
13
- description_tokenizer = AutoTokenizer.from_pretrained(model.config.text_encoder._name_or_path)
14
 
15
  # Supported languages and default settings
16
  languages = {
@@ -39,8 +33,10 @@ def generate_description(language, gender, emotion, noise, reverb, expressivity,
39
  # Generate audio function with GPU allocation
40
  @spaces.GPU # Allocate GPU for the duration of this function
41
  def generate_audio(text, description):
42
- # Move model to GPU
43
- model.to("cuda")
 
 
44
 
45
  # Prepare model inputs
46
  input_ids = description_tokenizer(description, return_tensors="pt").input_ids.to("cuda")
@@ -55,9 +51,6 @@ def generate_audio(text, description):
55
  sf.write(f.name, audio_arr, model.config.sampling_rate)
56
  audio_path = f.name
57
 
58
- # Move model back to CPU
59
- model.to("cpu")
60
-
61
  return audio_path
62
 
63
  # Gradio Interface
 
1
+ import spaces # Import spaces first to avoid CUDA initialization issues
2
  import gradio as gr
3
  import torch
4
  from parler_tts import ParlerTTSForConditionalGeneration
5
  from transformers import AutoTokenizer
6
  import soundfile as sf
7
  import tempfile
 
 
 
 
 
 
 
8
 
9
  # Supported languages and default settings
10
  languages = {
 
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
  sf.write(f.name, audio_arr, model.config.sampling_rate)
52
  audio_path = f.name
53
 
 
 
 
54
  return audio_path
55
 
56
  # Gradio Interface