renator commited on
Commit
6c85678
·
1 Parent(s): a57b3cc
Files changed (1) hide show
  1. texttovoice/views.py +27 -27
texttovoice/views.py CHANGED
@@ -47,36 +47,36 @@ class TextToSpeechCreateView(CreateAPIView):
47
  language = serializer.validated_data.get("language")
48
  output_filename = f"output_{uuid.uuid4()}.wav"
49
 
50
- try:
51
- print("before creating the speaker file path")
52
- # Save the uploaded speaker file to a temporary location
53
- speaker_file_path = os.path.join("/tmp", speaker_wav.name)
54
- with open(speaker_file_path, "wb") as destination:
55
- for chunk in speaker_wav.chunks():
56
- destination.write(chunk)
57
-
58
- print("after creating the speaker file path",speaker_file_path)
59
 
60
- # Generate speech using tts.tts_to_file
61
- tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
62
- tts.tts_to_file(text=text, file_path=output_filename, speaker_wav=speaker_file_path, language=language)
63
 
64
- # Define a function to delete the output file
65
- def file_iterator(file_name):
66
- with open(file_name, 'rb') as f:
67
- yield from f
68
 
69
- # Delete the file after sending it
70
- try:
71
- os.remove(file_name)
72
- except Exception as e:
73
- # You might want to log this error
74
- pass
75
 
76
- # Use the file_iterator to create a FileResponse
77
- response = FileResponse(file_iterator(output_filename), as_attachment=True, content_type='audio/wav')
78
- return response
79
 
80
- except Exception as e:
81
- return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
82
  return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 
47
  language = serializer.validated_data.get("language")
48
  output_filename = f"output_{uuid.uuid4()}.wav"
49
 
50
+ # try:
51
+ print("before creating the speaker file path")
52
+ # Save the uploaded speaker file to a temporary location
53
+ speaker_file_path = os.path.join("/tmp", speaker_wav.name)
54
+ with open(speaker_file_path, "wb") as destination:
55
+ for chunk in speaker_wav.chunks():
56
+ destination.write(chunk)
57
+
58
+ print("after creating the speaker file path",speaker_file_path)
59
 
60
+ # Generate speech using tts.tts_to_file
61
+ tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
62
+ tts.tts_to_file(text=text, file_path=output_filename, speaker_wav=speaker_file_path, language=language)
63
 
64
+ # Define a function to delete the output file
65
+ def file_iterator(file_name):
66
+ with open(file_name, 'rb') as f:
67
+ yield from f
68
 
69
+ # Delete the file after sending it
70
+ try:
71
+ os.remove(file_name)
72
+ except Exception as e:
73
+ # You might want to log this error
74
+ pass
75
 
76
+ # Use the file_iterator to create a FileResponse
77
+ response = FileResponse(file_iterator(output_filename), as_attachment=True, content_type='audio/wav')
78
+ return response
79
 
80
+ # except Exception as e:
81
+ # return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
82
  return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)