fix directory issue
Browse files
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 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
print(text_input)
|
| 75 |
-
if not text_input:
|
| 76 |
-
return jsonify({"error": "No text or text_url provided"}), 400
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
output_filename = f"output_cloned_{uuid.uuid4().hex}.mp3"
|
| 93 |
-
output_file_path = os.path.join(output_dir, output_filename)
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
)
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 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)
|