shaikrabbani-dev commited on
Commit
4bc2bed
Β·
verified Β·
1 Parent(s): a6c6ac5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -33
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  import requests
4
  import uuid
5
  import os
 
6
  import soundfile as sf
7
  from f5_tts.api import F5TTS
8
 
@@ -16,10 +17,10 @@ whisper_model = whisper.load_model("base")
16
 
17
  print("Loading F5-TTS Engine...")
18
  f5tts = F5TTS()
19
- print("System Operational!")
20
 
21
  # ==========================================
22
- # 2. Dynamic Text Response Generation
23
  # ==========================================
24
  def get_ai_answer(question):
25
  response = requests.post(
@@ -35,61 +36,76 @@ def get_ai_answer(question):
35
  },
36
  timeout=10
37
  )
38
- # Extracts text purely from the live API response array
39
  data = response.json()
40
- return data["choices"][0]["message"]["content"].strip()
41
 
42
  # ==========================================
43
- # 3. Dynamic Voice Synthesis Matrix
44
  # ==========================================
45
  def clone_and_speak(text, dynamic_ref_audio):
46
  try:
47
  if not dynamic_ref_audio or not os.path.exists(dynamic_ref_audio):
 
48
  return None
49
 
50
  # Automatically extract text from reference clip
51
- print(f"πŸ”Š Transcribing profile: {dynamic_ref_audio}")
52
  ref_transcription = whisper_model.transcribe(dynamic_ref_audio)
53
  extracted_ref_text = ref_transcription["text"].strip()
54
 
55
  if not extracted_ref_text:
56
  extracted_ref_text = "Hello"
57
 
58
- # Unique filename generation
59
  unique_filename = f"voice_clone_{uuid.uuid4().hex[:8]}.wav"
60
 
61
- # Execute voice clone generation
62
- outputs = f5tts.infer(
63
- ref_file=dynamic_ref_audio,
64
- ref_text=extracted_ref_text,
65
- gen_text=text,
66
- nfe_step=32,
67
- speed=1.0
68
- )
69
-
70
- # Universal unpacking handler for different package versions
71
- if isinstance(outputs, tuple):
72
- if len(outputs) == 3:
73
- wav_data, sr, _ = outputs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  else:
75
- wav_data, sr = outputs
76
- else:
77
- wav_data, sr = outputs, 24000
78
 
79
- # Cast torch tensor to NumPy structure if present
80
- if hasattr(wav_data, "cpu"):
81
- wav_data = wav_data.cpu().numpy()
82
 
83
- # Write data to physical disk space for browser playback mapping
84
- sf.write(unique_filename, wav_data, sr, format='WAV', subtype='PCM_16')
85
-
 
86
  if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
87
- print(f"πŸŽ‰ Generated track successfully: {unique_filename}")
88
  return unique_filename
 
 
89
  return None
90
 
91
  except Exception as e:
92
- print(f"❌ Core Exception: {e}")
93
  return None
94
 
95
  # ==========================================
@@ -107,14 +123,14 @@ def chatbot_engine(text_input, audio_input, target_voice_profile):
107
  if target_voice_profile is None:
108
  return None
109
 
110
- # Processes text and directly converts it into output audio path
111
  ai_text_response = get_ai_answer(question)
112
  generated_audio_track = clone_and_speak(ai_text_response, target_voice_profile)
113
 
114
  return generated_audio_track
115
 
116
  # ==========================================
117
- # 5. Gradio Audio-Only UI Canvas
118
  # ==========================================
119
  with gr.Blocks(title="Dynamic Voice Simulation Studio") as interface:
120
  gr.Markdown("# πŸŽ™οΈ Single-Page Voice Cloning Studio")
 
3
  import requests
4
  import uuid
5
  import os
6
+ import torch
7
  import soundfile as sf
8
  from f5_tts.api import F5TTS
9
 
 
17
 
18
  print("Loading F5-TTS 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
  response = requests.post(
 
36
  },
37
  timeout=10
38
  )
 
39
  data = response.json()
40
+ return data["choices"]["message"]["content"].strip()
41
 
42
  # ==========================================
43
+ # 3. Resilient Voice Synthesis Engine
44
  # ==========================================
45
  def clone_and_speak(text, dynamic_ref_audio):
46
  try:
47
  if not dynamic_ref_audio or not os.path.exists(dynamic_ref_audio):
48
+ print("❌ Reference enrollment track missing.")
49
  return None
50
 
51
  # Automatically extract text from reference clip
52
+ print(f"πŸ”Š Transcribing profile properties: {dynamic_ref_audio}")
53
  ref_transcription = whisper_model.transcribe(dynamic_ref_audio)
54
  extracted_ref_text = ref_transcription["text"].strip()
55
 
56
  if not extracted_ref_text:
57
  extracted_ref_text = "Hello"
58
 
59
+ # Unique filename generation to bypass browser cache freezes
60
  unique_filename = f"voice_clone_{uuid.uuid4().hex[:8]}.wav"
61
 
62
+ # EXPLICIT BRIDGE: Try both common signatures of the F5TTS inference layer
63
+ print("πŸ”Š Processing voice clone generation...")
64
+ try:
65
+ # Format A: Directly write to file using file_wave parameter
66
+ f5tts.infer(
67
+ ref_file=dynamic_ref_audio,
68
+ ref_text=extracted_ref_text,
69
+ gen_text=text,
70
+ file_wave=unique_filename,
71
+ nfe_step=32,
72
+ speed=1.0
73
+ )
74
+ except TypeError:
75
+ # Format B: Object returns raw arrays instead of writing files directly
76
+ outputs = f5tts.infer(
77
+ ref_file=dynamic_ref_audio,
78
+ ref_text=extracted_ref_text,
79
+ gen_text=text,
80
+ nfe_step=32,
81
+ speed=1.0
82
+ )
83
+ # Safe tuple array parsing extraction boundary
84
+ if isinstance(outputs, tuple):
85
+ if len(outputs) == 3:
86
+ wav_data, sr, _ = outputs
87
+ else:
88
+ wav_data, sr = outputs
89
  else:
90
+ wav_data, sr = outputs, 24000
 
 
91
 
92
+ # Convert underlying torch tensor blocks to NumPy structure if present
93
+ if hasattr(wav_data, "cpu"):
94
+ wav_data = wav_data.cpu().numpy()
95
 
96
+ # Save arrays onto your server disk workspace
97
+ sf.write(unique_filename, wav_data, sr, format='WAV', subtype='PCM_16')
98
+
99
+ # Double check data properties on disk space
100
  if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
101
+ print(f"πŸŽ‰ Generated track successfully saved to: {unique_filename}")
102
  return unique_filename
103
+
104
+ print("❌ Generated audio file was empty or missing from disk.")
105
  return None
106
 
107
  except Exception as e:
108
+ print(f"❌ Core TTS System Exception triggered: {e}")
109
  return None
110
 
111
  # ==========================================
 
123
  if target_voice_profile is None:
124
  return None
125
 
126
+ # Pipeline generation flow execution
127
  ai_text_response = get_ai_answer(question)
128
  generated_audio_track = clone_and_speak(ai_text_response, target_voice_profile)
129
 
130
  return generated_audio_track
131
 
132
  # ==========================================
133
+ # 5. Gradio Audio-Only UI Canvas (Single Page)
134
  # ==========================================
135
  with gr.Blocks(title="Dynamic Voice Simulation Studio") as interface:
136
  gr.Markdown("# πŸŽ™οΈ Single-Page Voice Cloning Studio")