shukdevdatta123 commited on
Commit
9414725
Β·
verified Β·
1 Parent(s): 9f540bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -39
app.py CHANGED
@@ -1,42 +1,56 @@
1
- from Crypto.Cipher import AES
2
- from Crypto.Protocol.KDF import PBKDF2
3
- import os
4
  import tempfile
5
- from dotenv import load_dotenv
6
-
7
- load_dotenv() # Load all environment variables
8
-
9
- def unpad(data):
10
- return data[:-data[-1]]
11
-
12
- def decrypt_and_run():
13
- # Get password from Hugging Face Secrets environment variable
14
- password = os.getenv("PASSWORD")
15
- if not password:
16
- raise ValueError("PASSWORD secret not found in environment variables")
17
-
18
- password = password.encode()
19
-
20
- with open("code.enc", "rb") as f:
21
- encrypted = f.read()
22
-
23
- salt = encrypted[:16]
24
- iv = encrypted[16:32]
25
- ciphertext = encrypted[32:]
26
-
27
- key = PBKDF2(password, salt, dkLen=32, count=1000000)
28
- cipher = AES.new(key, AES.MODE_CBC, iv)
29
-
30
- plaintext = unpad(cipher.decrypt(ciphertext))
31
-
32
- with tempfile.NamedTemporaryFile(suffix=".py", delete=False, mode='wb') as tmp:
33
- tmp.write(plaintext)
34
- tmp.flush()
35
- print(f"[INFO] Running decrypted code from {tmp.name}")
36
- os.system(f"python {tmp.name}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  if __name__ == "__main__":
39
- decrypt_and_run()
40
-
41
- # This script decrypts the encrypted code and runs it.
42
- # Ensure you have the PASSWORD secret set in your Hugging Face Secrets
 
1
+ import gradio as gr
2
+ import torch
 
3
  import tempfile
4
+ import soundfile as sf
5
+ from tortoise.api import TextToSpeech
6
+ from tortoise.utils.audio import load_audio
7
+
8
+ # 1) Initialize the Tortoise TTS engine at startup
9
+ tts = TextToSpeech() # Downloads and caches models automatically
10
+
11
+ # 2) Define a helper to generate speech from a reference clip + text
12
+ def generate_speech(reference_audio_path, text):
13
+ """
14
+ reference_audio_path: filepath to a WAV sampled at 22 050 Hz
15
+ text: the string to synthesize
16
+ returns: path to a 24 kHz WAV file with your cloned voice
17
+ """
18
+ # βœ… FIXED: Provide sampling_rate as a required positional argument
19
+ ref_waveform = load_audio(reference_audio_path, 22050)
20
+
21
+ # Generate speech using 'fast' preset (alternatives: ultra_fast, standard, high_quality)
22
+ output_tensor = tts.tts_with_preset(
23
+ text,
24
+ voice_samples=[ref_waveform],
25
+ preset="fast"
26
+ )
27
+
28
+ # Save to temp WAV (float32, 24 kHz)
29
+ wav_np = output_tensor.squeeze().cpu().numpy()
30
+ tmp = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
31
+ sf.write(tmp.name, wav_np, samplerate=24000)
32
+ return tmp.name
33
+
34
+ # 3) Build the Gradio interface
35
+ with gr.Blocks(title="Tortoise Voice Cloning TTS") as app:
36
+ gr.Markdown("## πŸ—£οΈ Voice Cloning with Tortoise TTS")
37
+ gr.Markdown(
38
+ "Upload a ~10 sec WAV clip (22 050 Hz), enter English text, "
39
+ "and hear it spoken back in **your** voice!"
40
+ )
41
+
42
+ with gr.Row():
43
+ voice_sample = gr.Audio(type="filepath", label="πŸŽ™οΈ Upload Reference Voice (22 050 Hz WAV)")
44
+ text_input = gr.Textbox(label="πŸ’¬ Text to Synthesize", placeholder="e.g., Hello, world!")
45
+
46
+ generate_btn = gr.Button("πŸ”Š Generate Speech")
47
+ output_audio = gr.Audio(label="πŸ“’ Cloned Speech Output (24 kHz)", interactive=False)
48
+
49
+ generate_btn.click(
50
+ fn=generate_speech,
51
+ inputs=[voice_sample, text_input],
52
+ outputs=output_audio
53
+ )
54
 
55
  if __name__ == "__main__":
56
+ app.launch()