Chloe commited on
Commit
f7ed60e
·
1 Parent(s): 7c2d826

change to ipfs link

Browse files
Files changed (1) hide show
  1. app.py +71 -58
app.py CHANGED
@@ -11,6 +11,7 @@ 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.
@@ -54,73 +55,85 @@ def is_url(string):
54
 
55
  @app.route('/api/voice-transfer', methods=['POST'])
56
  def voice_transfer():
57
- if 'voice_file' not in request.files:
58
- return jsonify({"error": "No voice file part"}), 400
59
-
60
- file = request.files['voice_file']
61
-
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
- app.logger.info(f"Received text input: {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
- app.logger.info("Input is not a URL, using as raw text.")
84
- text_content = text_input
85
-
86
- # --- Perform Voice Cloning using Fish Audio ---
87
- app.logger.info("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
- app.logger.error(f"Error during voice cloning with Fish Audio: {e}")
103
- return jsonify({"error": "Failed to generate voice file."}), 500
104
-
105
- app.logger.info(f"Successfully generated audio file at: {output_file_path}")
106
-
107
- # Read the generated file into a memory buffer
108
- with open(output_file_path, 'rb') as f:
109
- audio_buffer = io.BytesIO(f.read())
110
-
111
- # Send the buffer as a file
112
- return send_file(
113
- audio_buffer,
114
- as_attachment=True,
115
- download_name=output_filename,
116
- mimetype='audio/mpeg'
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
  except Exception as e:
120
  app.logger.error(f"An unexpected error occurred: {e}")
121
  return jsonify({"error": "An internal server error occurred."}), 500
122
  finally:
123
- # Clean up the temporary directory and its contents
124
  if temp_dir and os.path.exists(temp_dir):
125
  shutil.rmtree(temp_dir)
126
 
 
11
  from dotenv import load_dotenv
12
  import tempfile
13
  import shutil
14
+ from pydub import AudioSegment
15
 
16
  # Load environment variables from a .env file if it exists.
17
  # This is particularly useful for local development.
 
55
 
56
  @app.route('/api/voice-transfer', methods=['POST'])
57
  def voice_transfer():
 
 
 
 
 
 
 
 
58
  temp_dir = tempfile.mkdtemp()
59
  try:
60
+ file = None
61
+ voice_file_path = None
 
 
 
 
 
 
 
62
 
63
+ # # 1. Check for file upload
64
+ # if 'voice_file' in request.files:
65
+ # file = request.files['voice_file']
66
+ # if file.filename == '':
67
+ # return jsonify({"error": "No selected file"}), 400
68
+ # voice_file_path = os.path.join(temp_dir, file.filename)
69
+ # file.save(voice_file_path)
70
+ # 2. Check for file URL
71
+ if 'voice_file_url' in request.form:
72
+ file_url = request.form.get('voice_file_url')
73
+ if not file_url:
74
+ return jsonify({"error": "No voice_file_url provided"}), 400
75
+ response = requests.get(file_url)
76
+ if response.status_code != 200:
77
+ return jsonify({"error": "Failed to download voice file from URL"}), 400
78
+ # Save as webm first
79
+ webm_path = os.path.join(temp_dir, "input.webm")
80
+ with open(webm_path, "wb") as f:
81
+ f.write(response.content)
82
+ # Convert to mp3
83
+ mp3_path = os.path.join(temp_dir, "input.mp3")
84
+ audio = AudioSegment.from_file(webm_path, format="webm")
85
+ audio.export(mp3_path, format="mp3")
86
+ voice_file_path = mp3_path
87
+ else:
88
+ return jsonify({"error": "No voice file or voice_file_url provided"}), 400
89
+
90
+ text_input = request.form.get('text')
91
+ app.logger.info(f"Received text input: {text_input}")
92
+ if not text_input:
93
+ return jsonify({"error": "No text or text_url provided"}), 400
94
+
95
+ text_content = ""
96
+ if is_url(text_input):
97
+ text_content = get_text_from_url(text_input)
98
+ if text_content is None:
99
+ return jsonify({"error": "Failed to retrieve or parse content from URL"}), 400
100
+ else:
101
+ app.logger.info("Input is not a URL, using as raw text.")
102
+ text_content = text_input
103
+
104
+ # --- Perform Voice Cloning using Fish Audio ---
105
+ app.logger.info("Starting voice cloning process with Fish Audio...")
106
+
107
+ output_filename = f"output_cloned_{uuid.uuid4().hex}.mp3"
108
+ output_file_path = os.path.join(temp_dir, output_filename)
109
+
110
+ try:
111
+ clone_voice_with_fish(
112
+ text=text_content,
113
+ reference_audio_path=voice_file_path,
114
+ output_path=output_file_path,
115
+ reference_text="This is a reference audio for voice cloning."
116
  )
117
+ except Exception as e:
118
+ app.logger.error(f"Error during voice cloning with Fish Audio: {e}")
119
+ return jsonify({"error": "Failed to generate voice file."}), 500
120
+
121
+ app.logger.info(f"Successfully generated audio file at: {output_file_path}")
122
+
123
+ with open(output_file_path, 'rb') as f:
124
+ audio_buffer = io.BytesIO(f.read())
125
+
126
+ return send_file(
127
+ audio_buffer,
128
+ as_attachment=True,
129
+ download_name=output_filename,
130
+ mimetype='audio/mpeg'
131
+ )
132
 
133
  except Exception as e:
134
  app.logger.error(f"An unexpected error occurred: {e}")
135
  return jsonify({"error": "An internal server error occurred."}), 500
136
  finally:
 
137
  if temp_dir and os.path.exists(temp_dir):
138
  shutil.rmtree(temp_dir)
139