NeuralFalcon commited on
Commit
e78a5ff
·
verified ·
1 Parent(s): 6f2db6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -29
app.py CHANGED
@@ -22,43 +22,21 @@ language_map = {
22
  "Mandarin Chinese": "z"
23
  }
24
 
25
- # Print installation instructions if necessary
26
- install_messages = {
27
- "Japanese": "pip install misaki[ja]",
28
- "Mandarin Chinese": "pip install misaki[zh]"
29
- }
30
-
31
-
32
 
33
  def update_pipeline(Language):
34
  """ Updates the pipeline only if the language has changed. """
35
  global pipeline, last_used_language
36
-
37
- # Print installation instructions if necessary
38
- if Language in install_messages:
39
- # raise gr.Error(f"To Use {Language} Install: {install_messages[Language]}",duration=10)
40
- gr.Warning(f"To Use {Language} Install: {install_messages[Language]}",duration=10)
41
- # gr.Warning("Reverting to default English pipeline...", duration=5)
42
- # print(f"To use {Language}, install: {install_messages[Language]}")
43
- # print("Reverting to default English pipeline...")
44
-
45
-
46
- # Revert to default English and return immediately
47
- pipeline = KPipeline(lang_code="a")
48
- last_used_language = "a"
49
- return
50
-
51
  # Get language code, default to 'a' if not found
52
  new_lang = language_map.get(Language, "a")
53
 
54
  # Only update if the language is different
55
  if new_lang != last_used_language:
 
 
56
  try:
57
  pipeline = KPipeline(lang_code=new_lang)
58
  last_used_language = new_lang # Update last used language
59
- # print(f"Pipeline updated to {Language} ({new_lang})")
60
  except Exception as e:
61
- print(f"Error initializing KPipeline: {e}\nRetrying with default language...")
62
  pipeline = KPipeline(lang_code="a") # Fallback to English
63
  last_used_language = "a"
64
 
@@ -118,15 +96,15 @@ def clean_text(text):
118
 
119
  return text
120
 
121
- def tts_file_name(text):
122
  global temp_folder
123
  # Remove all non-alphabetic characters and convert to lowercase
124
  text = re.sub(r'[^a-zA-Z\s]', '', text) # Retain only alphabets and spaces
125
  text = text.lower().strip() # Convert to lowercase and strip leading/trailing spaces
126
  text = text.replace(" ", "_") # Replace spaces with underscores
127
-
128
  # Truncate or handle empty text
129
- truncated_text = text[:20] if len(text) > 20 else text if len(text) > 0 else "empty"
130
 
131
  # Generate a random string for uniqueness
132
  random_string = uuid.uuid4().hex[:8].upper()
@@ -162,7 +140,7 @@ def generate_and_save_audio(text, Language="American English",voice="af_bella",
162
  text=clean_text(text)
163
  update_pipeline(Language)
164
  generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
165
- save_path=tts_file_name(text)
166
  # Open the WAV file for writing
167
  timestamps={}
168
  with wave.open(save_path, 'wb') as wav_file:
@@ -586,4 +564,4 @@ last_used_language = "a"
586
  pipeline = KPipeline(lang_code=last_used_language)
587
  temp_folder = create_audio_dir()
588
  if __name__ == "__main__":
589
- main()
 
22
  "Mandarin Chinese": "z"
23
  }
24
 
 
 
 
 
 
 
 
25
 
26
  def update_pipeline(Language):
27
  """ Updates the pipeline only if the language has changed. """
28
  global pipeline, last_used_language
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  # Get language code, default to 'a' if not found
30
  new_lang = language_map.get(Language, "a")
31
 
32
  # Only update if the language is different
33
  if new_lang != last_used_language:
34
+ pipeline = KPipeline(lang_code=new_lang)
35
+ last_used_language = new_lang
36
  try:
37
  pipeline = KPipeline(lang_code=new_lang)
38
  last_used_language = new_lang # Update last used language
 
39
  except Exception as e:
 
40
  pipeline = KPipeline(lang_code="a") # Fallback to English
41
  last_used_language = "a"
42
 
 
96
 
97
  return text
98
 
99
+ def tts_file_name(text,language):
100
  global temp_folder
101
  # Remove all non-alphabetic characters and convert to lowercase
102
  text = re.sub(r'[^a-zA-Z\s]', '', text) # Retain only alphabets and spaces
103
  text = text.lower().strip() # Convert to lowercase and strip leading/trailing spaces
104
  text = text.replace(" ", "_") # Replace spaces with underscores
105
+ language=language.replace(" ", "_").strip()
106
  # Truncate or handle empty text
107
+ truncated_text = text[:20] if len(text) > 20 else text if len(text) > 0 else language
108
 
109
  # Generate a random string for uniqueness
110
  random_string = uuid.uuid4().hex[:8].upper()
 
140
  text=clean_text(text)
141
  update_pipeline(Language)
142
  generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
143
+ save_path=tts_file_name(text,Language)
144
  # Open the WAV file for writing
145
  timestamps={}
146
  with wave.open(save_path, 'wb') as wav_file:
 
564
  pipeline = KPipeline(lang_code=last_used_language)
565
  temp_folder = create_audio_dir()
566
  if __name__ == "__main__":
567
+ main()