Chloe commited on
Commit
3797366
·
1 Parent(s): ef9a767

fix directory issue

Browse files
Files changed (1) hide show
  1. app.py +56 -45
app.py CHANGED
@@ -9,6 +9,8 @@ import io
9
  from fish_audio import clone_voice_with_fish
10
  import uuid
11
  from dotenv import load_dotenv
 
 
12
 
13
  # Load environment variables from a .env file if it exists.
14
  # This is particularly useful for local development.
@@ -60,56 +62,65 @@ def voice_transfer():
60
  if file.filename == '':
61
  return jsonify({"error": "No selected file"}), 400
62
 
63
- if file:
64
- # Save the uploaded file temporarily
65
- # In a production environment, you'd want a more robust storage solution
66
- upload_folder = 'uploads'
67
- if not os.path.exists(upload_folder):
68
- os.makedirs(upload_folder)
69
 
70
- voice_file_path = os.path.join(upload_folder, file.filename)
71
- file.save(voice_file_path)
72
-
73
- text_input = request.form.get('text')
74
- print(text_input)
75
- if not text_input:
76
- return jsonify({"error": "No text or text_url provided"}), 400
77
 
78
- text_content = ""
79
- if is_url(text_input):
80
- text_content = get_text_from_url(text_input)
81
- if text_content is None:
82
- return jsonify({"error": "Failed to retrieve or parse content from URL"}), 400
83
- else:
84
- print("Not URL")
85
- text_content = text_input
86
 
87
- # --- Perform Voice Cloning using Fish Audio ---
88
- print("Starting voice cloning process with Fish Audio...")
89
-
90
- output_dir = 'outputs_v2'
91
- os.makedirs(output_dir, exist_ok=True)
92
- output_filename = f"output_cloned_{uuid.uuid4().hex}.mp3"
93
- output_file_path = os.path.join(output_dir, output_filename)
94
 
95
- try:
96
- # Note: For best results, provide an accurate transcript of the reference audio.
97
- # Since we don't get it from the user, we can pass a generic placeholder or an empty string.
98
- clone_voice_with_fish(
99
- text=text_content,
100
- reference_audio_path=voice_file_path,
101
- output_path=output_file_path,
102
- reference_text="This is a reference audio for voice cloning."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  )
104
- except Exception as e:
105
- print(f"Error during voice cloning with Fish Audio: {e}")
106
- return jsonify({"error": "Failed to generate voice file."}), 500
107
-
108
- try:
109
- return send_file(output_file_path, as_attachment=True)
110
- except Exception as e:
111
- app.logger.error(f"Error sending file: {e}")
112
- return jsonify({"error": "Failed to send audio file."}), 500
113
 
114
  # if __name__ == '__main__':
115
  # app.run(debug=True, port=5001)
 
9
  from fish_audio import clone_voice_with_fish
10
  import uuid
11
  from dotenv import load_dotenv
12
+ import tempfile
13
+ import shutil
14
 
15
  # Load environment variables from a .env file if it exists.
16
  # This is particularly useful for local development.
 
62
  if file.filename == '':
63
  return jsonify({"error": "No selected file"}), 400
64
 
65
+ temp_dir = tempfile.mkdtemp()
66
+ try:
67
+ if file:
68
+ # Save the uploaded file in the temporary directory
69
+ voice_file_path = os.path.join(temp_dir, file.filename)
70
+ file.save(voice_file_path)
71
 
72
+ text_input = request.form.get('text')
73
+ print(text_input)
74
+ if not text_input:
75
+ return jsonify({"error": "No text or text_url provided"}), 400
 
 
 
76
 
77
+ text_content = ""
78
+ if is_url(text_input):
79
+ text_content = get_text_from_url(text_input)
80
+ if text_content is None:
81
+ return jsonify({"error": "Failed to retrieve or parse content from URL"}), 400
82
+ else:
83
+ print("Not URL")
84
+ text_content = text_input
85
 
86
+ # --- Perform Voice Cloning using Fish Audio ---
87
+ print("Starting voice cloning process with Fish Audio...")
88
+
89
+ output_filename = f"output_cloned_{uuid.uuid4().hex}.mp3"
90
+ output_file_path = os.path.join(temp_dir, output_filename)
 
 
91
 
92
+ try:
93
+ # Note: For best results, provide an accurate transcript of the reference audio.
94
+ # Since we don't get it from the user, we can pass a generic placeholder or an empty string.
95
+ clone_voice_with_fish(
96
+ text=text_content,
97
+ reference_audio_path=voice_file_path,
98
+ output_path=output_file_path,
99
+ reference_text="This is a reference audio for voice cloning."
100
+ )
101
+ except Exception as e:
102
+ print(f"Error during voice cloning with Fish Audio: {e}")
103
+ return jsonify({"error": "Failed to generate voice file."}), 500
104
+
105
+ # Read the generated file into a memory buffer
106
+ with open(output_file_path, 'rb') as f:
107
+ audio_buffer = io.BytesIO(f.read())
108
+
109
+ # Send the buffer as a file
110
+ return send_file(
111
+ audio_buffer,
112
+ as_attachment=True,
113
+ download_name=output_filename,
114
+ mimetype='audio/mpeg'
115
  )
116
+
117
+ except Exception as e:
118
+ app.logger.error(f"An unexpected error occurred: {e}")
119
+ return jsonify({"error": "An internal server error occurred."}), 500
120
+ finally:
121
+ # Clean up the temporary directory and its contents
122
+ if temp_dir and os.path.exists(temp_dir):
123
+ shutil.rmtree(temp_dir)
 
124
 
125
  # if __name__ == '__main__':
126
  # app.run(debug=True, port=5001)