Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Upload app (1).py
Browse files- app (1).py +60 -0
    	
        app (1).py
    ADDED
    
    | @@ -0,0 +1,60 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import spaces
         | 
| 2 | 
            +
            import os
         | 
| 3 | 
            +
            import sys
         | 
| 4 | 
            +
            sys.path.append("neutts-air")
         | 
| 5 | 
            +
            from neuttsair.neutts import NeuTTSAir
         | 
| 6 | 
            +
            import numpy as np
         | 
| 7 | 
            +
            import gradio as gr
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            SAMPLES_PATH = os.path.join(os.getcwd(), "neutts-air", "samples")
         | 
| 10 | 
            +
            DEFAULT_REF_TEXT = "So I'm live on radio. And I say, well, my dear friend James here clearly, and the whole room just froze. Turns out I'd completely misspoken and mentioned our other friend."
         | 
| 11 | 
            +
            DEFAULT_REF_PATH = os.path.join(SAMPLES_PATH, "dave.wav")
         | 
| 12 | 
            +
            DEFAULT_GEN_TEXT = "My name is Dave, and um, I'm from London."
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            # --- Force CPU usage ---
         | 
| 15 | 
            +
            tts = NeuTTSAir(
         | 
| 16 | 
            +
                backbone_repo="neuphonic/neutts-air",
         | 
| 17 | 
            +
                backbone_device="cpu",
         | 
| 18 | 
            +
                codec_repo="neuphonic/neucodec",
         | 
| 19 | 
            +
                codec_device="cpu"
         | 
| 20 | 
            +
            )
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            def infer(
         | 
| 23 | 
            +
                ref_text: str,
         | 
| 24 | 
            +
                ref_audio_path: str,
         | 
| 25 | 
            +
                gen_text: str,
         | 
| 26 | 
            +
            ) -> tuple[int, np.ndarray]:
         | 
| 27 | 
            +
                """
         | 
| 28 | 
            +
                Generates speech using NeuTTS-Air given a reference audio and text, and new text to synthesize.
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                Args:
         | 
| 31 | 
            +
                    ref_text (str): The text corresponding to the reference audio.
         | 
| 32 | 
            +
                    ref_audio_path (str): The file path to the reference audio.
         | 
| 33 | 
            +
                    gen_text (str): The new text to synthesize.
         | 
| 34 | 
            +
                Returns:
         | 
| 35 | 
            +
                    tuple [int, np.ndarray]: A tuple containing the sample rate (24000) and the generated audio waveform as a numpy array.
         | 
| 36 | 
            +
                """
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                gr.Info("Starting inference request (CPU mode)!")
         | 
| 39 | 
            +
                gr.Info("Encoding reference...")
         | 
| 40 | 
            +
                ref_codes = tts.encode_reference(ref_audio_path)
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                gr.Info(f"Generating audio for input text: {gen_text}")
         | 
| 43 | 
            +
                wav = tts.infer(gen_text, ref_codes, ref_text)
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                return (24_000, wav)
         | 
| 46 | 
            +
             | 
| 47 | 
            +
            demo = gr.Interface(
         | 
| 48 | 
            +
                fn=infer,
         | 
| 49 | 
            +
                inputs=[
         | 
| 50 | 
            +
                    gr.Textbox(label="Reference Text", value=DEFAULT_REF_TEXT),
         | 
| 51 | 
            +
                    gr.Audio(type="filepath", label="Reference Audio", value=DEFAULT_REF_PATH),
         | 
| 52 | 
            +
                    gr.Textbox(label="Text to Generate", value=DEFAULT_GEN_TEXT),
         | 
| 53 | 
            +
                ],
         | 
| 54 | 
            +
                outputs=gr.Audio(type="numpy", label="Generated Speech"),
         | 
| 55 | 
            +
                title="NeuTTS-Air☁️ (CPU Mode)",
         | 
| 56 | 
            +
                description="Upload a reference audio sample, provide the reference text, and enter new text to synthesize (running on CPU)."
         | 
| 57 | 
            +
            )
         | 
| 58 | 
            +
             | 
| 59 | 
            +
            if __name__ == "__main__":
         | 
| 60 | 
            +
                demo.launch(allowed_paths=[SAMPLES_PATH], mcp_server=True, inbrowser=True)
         | 
 
			
