NLPV commited on
Commit
a2530ca
Β·
verified Β·
1 Parent(s): 3e166ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -6
app.py CHANGED
@@ -3,14 +3,38 @@ from parler_tts import ParlerTTSForConditionalGeneration
3
  from transformers import AutoTokenizer
4
  import gradio as gr
5
  import numpy as np
 
 
6
 
7
  # Set device to GPU if available, else CPU
8
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
9
 
10
- # Load the TTS model and tokenizers
11
- model = ParlerTTSForConditionalGeneration.from_pretrained("ai4bharat/indic-parler-tts").to(device)
12
- tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indic-parler-tts")
13
- description_tokenizer = AutoTokenizer.from_pretrained(model.config.text_encoder._name_or_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  def generate_audio(text: str):
16
  """
@@ -23,8 +47,10 @@ def generate_audio(text: str):
23
  tuple: A tuple containing the audio numpy array and the sampling rate.
24
  """
25
  # Set a default voice description
26
- default_description = ("Divya's voice is monotone yet slightly fast in delivery, with a very close recording "
27
- "that almost has no background noise.")
 
 
28
 
29
  # Tokenize the default description and the input text
30
  description_tokens = description_tokenizer(default_description, return_tensors="pt").to(device)
 
3
  from transformers import AutoTokenizer
4
  import gradio as gr
5
  import numpy as np
6
+ import time
7
+ import requests
8
 
9
  # Set device to GPU if available, else CPU
10
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
11
 
12
+ # Function to load the model with retry logic and increased timeout
13
+ def load_model_with_retry(model_id, retries=3, timeout=60):
14
+ for attempt in range(1, retries + 1):
15
+ try:
16
+ model = ParlerTTSForConditionalGeneration.from_pretrained(model_id, timeout=timeout).to(device)
17
+ return model
18
+ except requests.exceptions.ReadTimeout:
19
+ print(f"Timeout when loading model. Attempt {attempt} of {retries}. Retrying in 5 seconds...")
20
+ time.sleep(5)
21
+ raise Exception("Failed to load model after multiple retries.")
22
+
23
+ # Function to load a tokenizer with retry logic and increased timeout
24
+ def load_tokenizer_with_retry(tokenizer_id, retries=3, timeout=60):
25
+ for attempt in range(1, retries + 1):
26
+ try:
27
+ tokenizer = AutoTokenizer.from_pretrained(tokenizer_id, timeout=timeout)
28
+ return tokenizer
29
+ except requests.exceptions.ReadTimeout:
30
+ print(f"Timeout when loading tokenizer. Attempt {attempt} of {retries}. Retrying in 5 seconds...")
31
+ time.sleep(5)
32
+ raise Exception("Failed to load tokenizer after multiple retries.")
33
+
34
+ # Load the TTS model and tokenizers using the retry functions
35
+ model = load_model_with_retry("ai4bharat/indic-parler-tts")
36
+ tokenizer = load_tokenizer_with_retry("ai4bharat/indic-parler-tts")
37
+ description_tokenizer = load_tokenizer_with_retry(model.config.text_encoder._name_or_path)
38
 
39
  def generate_audio(text: str):
40
  """
 
47
  tuple: A tuple containing the audio numpy array and the sampling rate.
48
  """
49
  # Set a default voice description
50
+ default_description = (
51
+ "Divya's voice is monotone yet slightly fast in delivery, with a very close recording "
52
+ "that almost has no background noise."
53
+ )
54
 
55
  # Tokenize the default description and the input text
56
  description_tokens = description_tokenizer(default_description, return_tensors="pt").to(device)