Bils commited on
Commit
019c404
·
verified ·
1 Parent(s): f16af7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -10
app.py CHANGED
@@ -13,6 +13,7 @@ from pydub import AudioSegment
13
  from dotenv import load_dotenv
14
  import tempfile
15
  import spaces
 
16
 
17
  # Load environment variables
18
  load_dotenv()
@@ -55,18 +56,15 @@ def generate_script(user_prompt: str, model_id: str, token: str, duration: int):
55
  # Voice-Over Generation Function
56
  # ---------------------------------------------------------------------
57
  @spaces.GPU(duration=300)
58
- def generate_voice(script: str, speaker: str):
59
  try:
60
- # Replace with your chosen TTS model
61
- tts_model = "coqui/XTTS-v2"
62
- processor = AutoProcessor.from_pretrained(tts_model)
63
- model = AutoModelForCausalLM.from_pretrained(tts_model)
64
-
65
- inputs = processor(script, return_tensors="pt")
66
- speech = model.generate(**inputs)
67
-
68
  output_path = f"{tempfile.gettempdir()}/generated_voice.wav"
69
- write(output_path, 22050, speech.cpu().numpy())
 
70
  return output_path
71
  except Exception as e:
72
  return f"Error generating voice-over: {e}"
 
13
  from dotenv import load_dotenv
14
  import tempfile
15
  import spaces
16
+ from TTS.api import TTS
17
 
18
  # Load environment variables
19
  load_dotenv()
 
56
  # Voice-Over Generation Function
57
  # ---------------------------------------------------------------------
58
  @spaces.GPU(duration=300)
59
+ def generate_voice(script: str, speaker: str = "default"):
60
  try:
61
+ # Load the TTS model
62
+ tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", gpu=torch.cuda.is_available())
63
+
64
+ # Generate the speech audio file
 
 
 
 
65
  output_path = f"{tempfile.gettempdir()}/generated_voice.wav"
66
+ tts.tts_to_file(text=script, file_path=output_path, speaker=speaker)
67
+
68
  return output_path
69
  except Exception as e:
70
  return f"Error generating voice-over: {e}"