shaikrabbani-dev commited on
Commit
4ad1db6
Β·
verified Β·
1 Parent(s): 91d9fc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -47
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  import uuid
5
  import os
6
  import soundfile as sf
 
7
  from f5_tts.api import F5TTS
8
 
9
  # ==========================================
@@ -11,15 +12,15 @@ from f5_tts.api import F5TTS
11
  # ==========================================
12
  GROQ_API_KEY = "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"
13
 
14
- print("Loading Whisper Model...")
15
  whisper_model = whisper.load_model("base")
16
 
17
- print("Loading F5-TTS Engine...")
18
  f5tts = F5TTS()
19
  print("Voice Simulation Matrix Active!")
20
 
21
  # ==========================================
22
- # 2. Dynamic Text Response Generation
23
  # ==========================================
24
  def get_ai_answer(question):
25
  try:
@@ -37,17 +38,16 @@ def get_ai_answer(question):
37
  timeout=10
38
  )
39
  data = response.json()
40
- # CRITICAL FIX: Explicitly target array index [0] to parse live text properly
41
  if "choices" in data and len(data["choices"]) > 0:
42
- return data["choices"][0]["message"]["content"].strip()
43
  except Exception as e:
44
  print(f"Groq API connection error: {e}")
45
 
46
- # Clean programmatic default response if the network times out
47
  return "Salesforce is a cloud-based customer relationship management platform that helps businesses manage sales, marketing, and customer service tracking in one place."
48
 
49
  # ==========================================
50
- # 3. Dynamic Voice Synthesis Engine
51
  # ==========================================
52
  def clone_and_speak(text, dynamic_ref_audio):
53
  try:
@@ -75,44 +75,55 @@ def clone_and_speak(text, dynamic_ref_audio):
75
  extracted_ref_text = "Hello"
76
 
77
  unique_filename = f"voice_clone_{uuid.uuid4().hex[:8]}.wav"
78
- print("πŸ”Š Processing voice clone generation...")
79
 
80
- # PARAMETER PATCH: Use try/except blocks to map both 'ref_audio' and 'ref_file' arguments
81
- try:
82
- # Native F5-TTS Library Parameter Format (uses ref_audio)
83
- outputs = f5tts.infer(
84
- ref_audio=clean_ref_path,
85
- ref_text=extracted_ref_text,
86
- gen_text=text,
87
- nfe_step=16,
88
- speed=1.0
89
- )
90
- except TypeError:
91
- # Alternative Custom Wrapper Format (uses ref_file)
92
- outputs = f5tts.infer(
93
- ref_file=clean_ref_path,
94
- ref_text=extracted_ref_text,
95
- gen_text=text,
96
- nfe_step=16,
97
- speed=1.0
98
- )
99
-
100
- # Unpack returned signal arrays safely
101
- if isinstance(outputs, tuple):
102
- if len(outputs) == 3:
103
- wav_data, sr, _ = outputs
104
- else:
105
- wav_data, sr = outputs
106
- else:
107
- wav_data, sr = outputs, 24000
108
-
109
- if hasattr(wav_data, "cpu"):
110
- wav_data = wav_data.cpu().numpy()
111
-
112
- # Write data to physical disk space for browser player rendering
113
- sf.write(unique_filename, wav_data, sr, format='WAV', subtype='PCM_16')
114
-
115
- # Clean temporary tracking paths
 
 
 
 
 
 
 
 
 
 
 
 
116
  if os.path.exists(clean_ref_path) and clean_ref_path != dynamic_ref_audio:
117
  os.remove(clean_ref_path)
118
 
@@ -120,6 +131,7 @@ def clone_and_speak(text, dynamic_ref_audio):
120
  print(f"πŸŽ‰ Generated track successfully saved to: {unique_filename}")
121
  return unique_filename
122
 
 
123
  return None
124
 
125
  except Exception as e:
@@ -147,15 +159,15 @@ def chatbot_engine(text_input, audio_input, target_voice_profile):
147
  return generated_audio_track
148
 
149
  # ==========================================
150
- # 5. Gradio Single-Page Interface Layout
151
  # ==========================================
152
  with gr.Blocks(title="Dynamic Voice Simulation Studio") as interface:
153
- gr.Markdown("# πŸŽ™ *Single-Page Voice Cloning Studio*")
154
  gr.Markdown("Drop any source audio file to clone voice attributes and play back the generated simulated response.")
155
 
156
  with gr.Row():
157
  with gr.Column(scale=1):
158
- gr.Markdown("### πŸŽ› 1. Reference Audio Profile")
159
  voice_profile_uploader = gr.Audio(
160
  label="Target Voice to Clone",
161
  type="filepath"
 
4
  import uuid
5
  import os
6
  import soundfile as sf
7
+ import inspect
8
  from f5_tts.api import F5TTS
9
 
10
  # ==========================================
 
12
  # ==========================================
13
  GROQ_API_KEY = "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"
14
 
15
+ print("Initializing Whisper Speech Recognition...")
16
  whisper_model = whisper.load_model("base")
17
 
18
+ print("Initializing F5-TTS Simulation Engine...")
19
  f5tts = F5TTS()
20
  print("Voice Simulation Matrix Active!")
21
 
22
  # ==========================================
23
+ # 2. Pure Dynamic Text Response Generation
24
  # ==========================================
25
  def get_ai_answer(question):
26
  try:
 
38
  timeout=10
39
  )
40
  data = response.json()
 
41
  if "choices" in data and len(data["choices"]) > 0:
42
+ return data["choices"]["message"]["content"].strip()
43
  except Exception as e:
44
  print(f"Groq API connection error: {e}")
45
 
46
+ # Static fallback message to protect pipeline execution flow
47
  return "Salesforce is a cloud-based customer relationship management platform that helps businesses manage sales, marketing, and customer service tracking in one place."
48
 
49
  # ==========================================
50
+ # 3. Dynamic Voice Synthesis (Signature Inspection)
51
  # ==========================================
52
  def clone_and_speak(text, dynamic_ref_audio):
53
  try:
 
75
  extracted_ref_text = "Hello"
76
 
77
  unique_filename = f"voice_clone_{uuid.uuid4().hex[:8]}.wav"
 
78
 
79
+ # --- AUTOMATED SIGNATURE INSPECTION MATRIX ---
80
+ # Look inside the installed F5-TTS function parameters to build a safe argument map
81
+ sig = inspect.signature(f5tts.infer)
82
+ supported_args = sig.parameters.keys()
83
+ print(f"πŸ“‹ Detected package argument parameters: {list(supported_args)}")
84
+
85
+ kwargs = {}
86
+
87
+ # Dynamically map reference file parameter keys
88
+ if "ref_audio" in supported_args:
89
+ kwargs["ref_audio"] = clean_ref_path
90
+ elif "ref_file" in supported_args:
91
+ kwargs["ref_file"] = clean_ref_path
92
+
93
+ # Dynamically map tracking inputs
94
+ if "ref_text" in supported_args:
95
+ kwargs["ref_text"] = extracted_ref_text
96
+ if "gen_text" in supported_args:
97
+ kwargs["gen_text"] = text
98
+ if "file_wave" in supported_args:
99
+ kwargs["file_wave"] = unique_filename
100
+
101
+ # Safe addition for secondary speed/step controllers if supported by your version
102
+ if "nfe_step" in supported_args:
103
+ kwargs["nfe_step"] = 16
104
+ if "speed" in supported_args:
105
+ kwargs["speed"] = 1.0
106
+
107
+ print(f"πŸ”Š Processing execution loop with arguments: {list(kwargs.keys())}")
108
+ outputs = f5tts.infer(**kwargs)
109
+
110
+ # Handle file saving fallback if package version didn't write to file_wave automatically
111
+ if not os.path.exists(unique_filename) or os.path.getsize(unique_filename) == 0:
112
+ if outputs is not None:
113
+ if isinstance(outputs, tuple):
114
+ if len(outputs) == 3:
115
+ wav_data, sr, _ = outputs
116
+ else:
117
+ wav_data, sr = outputs
118
+ else:
119
+ wav_data, sr = outputs, 24000
120
+
121
+ if hasattr(wav_data, "cpu"):
122
+ wav_data = wav_data.cpu().numpy()
123
+
124
+ sf.write(unique_filename, wav_data, sr, format='WAV', subtype='PCM_16')
125
+
126
+ # Clean temporary tracking assets
127
  if os.path.exists(clean_ref_path) and clean_ref_path != dynamic_ref_audio:
128
  os.remove(clean_ref_path)
129
 
 
131
  print(f"πŸŽ‰ Generated track successfully saved to: {unique_filename}")
132
  return unique_filename
133
 
134
+ print("❌ System warning: Output file generation sizing error.")
135
  return None
136
 
137
  except Exception as e:
 
159
  return generated_audio_track
160
 
161
  # ==========================================
162
+ # 5. Gradio Audio-Only UI Canvas (Single Page)
163
  # ==========================================
164
  with gr.Blocks(title="Dynamic Voice Simulation Studio") as interface:
165
+ gr.Markdown("# πŸŽ™οΈ Single-Page Voice Cloning Studio")
166
  gr.Markdown("Drop any source audio file to clone voice attributes and play back the generated simulated response.")
167
 
168
  with gr.Row():
169
  with gr.Column(scale=1):
170
+ gr.Markdown("### πŸŽ›οΈ 1. Reference Audio Profile")
171
  voice_profile_uploader = gr.Audio(
172
  label="Target Voice to Clone",
173
  type="filepath"