Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -91,10 +91,8 @@ class VoicesResponse(BaseModel): | |
| 91 | 
             
            async def generate_speech_async(text: str, voice: str, pitch: str, rate: str, output_file: str) -> bool:
         | 
| 92 | 
             
                """Generate speech asynchronously"""
         | 
| 93 | 
             
                try:
         | 
| 94 | 
            -
                    #  | 
| 95 | 
            -
                     | 
| 96 | 
            -
                    
         | 
| 97 | 
            -
                    communicate = edge_tts.Communicate(text = text, voice = voice, rate = rate, pitch = pitch)
         | 
| 98 | 
             
                    await communicate.save(output_file)
         | 
| 99 | 
             
                    return True
         | 
| 100 | 
             
                except Exception as e:
         | 
| @@ -170,12 +168,12 @@ async def synthesize_speech(request: TTSRequest): | |
| 170 | 
             
                    if not os.path.exists(output_file):
         | 
| 171 | 
             
                        raise HTTPException(status_code=500, detail="Audio file was not generated")
         | 
| 172 |  | 
| 173 | 
            -
                    # Return the audio file
         | 
| 174 | 
             
                    return FileResponse(
         | 
| 175 | 
             
                        output_file,
         | 
| 176 | 
             
                        media_type="audio/mpeg",
         | 
| 177 | 
             
                        filename=f"speech_{file_id}.mp3",
         | 
| 178 | 
            -
                        background= | 
| 179 | 
             
                    )
         | 
| 180 |  | 
| 181 | 
             
                except HTTPException:
         | 
| @@ -253,10 +251,15 @@ if __name__ == "__main__": | |
| 253 | 
             
                print("API Documentation will be available at: http://localhost:7860/")
         | 
| 254 | 
             
                print("Health check: http://localhost:7860/health")
         | 
| 255 | 
             
                print("Available voices: http://localhost:7860/voices")
         | 
| 256 | 
            -
                print("\nExample usage:")
         | 
| 257 | 
             
                print("curl -X POST 'http://localhost:7860/synthesize' \\")
         | 
| 258 | 
             
                print("  -H 'Content-Type: application/json' \\")
         | 
| 259 | 
             
                print("  -d '{\"text\":\"Hello from Hugging Face!\",\"voice\":\"en-GB-SoniaNeural\",\"pitch\":\"-10Hz\",\"rate\":\"+15%\"}' \\")
         | 
| 260 | 
             
                print("  --output speech.mp3")
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 261 |  | 
| 262 | 
             
                uvicorn.run(app, host="0.0.0.0", port=7860)
         | 
|  | |
| 91 | 
             
            async def generate_speech_async(text: str, voice: str, pitch: str, rate: str, output_file: str) -> bool:
         | 
| 92 | 
             
                """Generate speech asynchronously"""
         | 
| 93 | 
             
                try:
         | 
| 94 | 
            +
                    # Use edge_tts.Communicate with direct parameters (no SSML needed)
         | 
| 95 | 
            +
                    communicate = edge_tts.Communicate(text=text, voice=voice, rate=rate, pitch=pitch)
         | 
|  | |
|  | |
| 96 | 
             
                    await communicate.save(output_file)
         | 
| 97 | 
             
                    return True
         | 
| 98 | 
             
                except Exception as e:
         | 
|  | |
| 168 | 
             
                    if not os.path.exists(output_file):
         | 
| 169 | 
             
                        raise HTTPException(status_code=500, detail="Audio file was not generated")
         | 
| 170 |  | 
| 171 | 
            +
                    # Return the audio file directly
         | 
| 172 | 
             
                    return FileResponse(
         | 
| 173 | 
             
                        output_file,
         | 
| 174 | 
             
                        media_type="audio/mpeg",
         | 
| 175 | 
             
                        filename=f"speech_{file_id}.mp3",
         | 
| 176 | 
            +
                        background=None  # Don't cleanup immediately, let the response complete first
         | 
| 177 | 
             
                    )
         | 
| 178 |  | 
| 179 | 
             
                except HTTPException:
         | 
|  | |
| 251 | 
             
                print("API Documentation will be available at: http://localhost:7860/")
         | 
| 252 | 
             
                print("Health check: http://localhost:7860/health")
         | 
| 253 | 
             
                print("Available voices: http://localhost:7860/voices")
         | 
| 254 | 
            +
                print("\nExample usage (saves audio file locally):")
         | 
| 255 | 
             
                print("curl -X POST 'http://localhost:7860/synthesize' \\")
         | 
| 256 | 
             
                print("  -H 'Content-Type: application/json' \\")
         | 
| 257 | 
             
                print("  -d '{\"text\":\"Hello from Hugging Face!\",\"voice\":\"en-GB-SoniaNeural\",\"pitch\":\"-10Hz\",\"rate\":\"+15%\"}' \\")
         | 
| 258 | 
             
                print("  --output speech.mp3")
         | 
| 259 | 
            +
                print("\nFor your deployed space:")
         | 
| 260 | 
            +
                print("curl -X POST 'https://nitinbot001-tts-api.hf.space/synthesize' \\")
         | 
| 261 | 
            +
                print("  -H 'Content-Type: application/json' \\")
         | 
| 262 | 
            +
                print("  -d '{\"text\":\"hello my name is nitin\",\"voice\":\"en-US-AriaNeural\",\"pitch\":\"+0Hz\",\"rate\":\"+0%\"}' \\")
         | 
| 263 | 
            +
                print("  --output speech.mp3")
         | 
| 264 |  | 
| 265 | 
             
                uvicorn.run(app, host="0.0.0.0", port=7860)
         |