shaikrabbani-dev commited on
Commit
fda629b
Β·
verified Β·
1 Parent(s): ee43ad4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -23
app.py CHANGED
@@ -3,25 +3,22 @@ import gradio as gr
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
 
10
- # 1. Configuration & Device Auto-Detection
 
 
11
  GROQ_API_KEY = "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"
12
  voice_file = "my_voice_fixed.wav"
13
 
14
- # Automatically detect if space has access to GPU acceleration or fallback to CPU
15
- device = "cuda" if torch.cuda.is_available() else "cpu"
16
- print(f"πŸš€ Initializing simulation engine on device: {device}")
17
-
18
  print("Loading Whisper Model...")
19
- whisper_model = whisper.load_model("base", device=device)
20
 
21
- print("Loading F5-TTS Simulation Model...")
22
- f5tts = F5TTS(device=device)
23
 
24
- # Automatically extract reference text from the file at startup
25
  print(f"Extracting reference text from {voice_file}...")
26
  try:
27
  ref_result = whisper_model.transcribe(voice_file)
@@ -33,7 +30,9 @@ except Exception as e:
33
  print(f"⚠️ Text extraction warning: {e}")
34
  auto_reference_text = "Hello"
35
 
36
- # 2. Resilient API Function with Smart Text Fallback
 
 
37
  def get_ai_answer(question):
38
  try:
39
  response = requests.post(
@@ -50,44 +49,52 @@ def get_ai_answer(question):
50
  timeout=10
51
  )
52
  if response.status_code == 200:
 
53
  return response.json()["choices"][0]["message"]["content"].strip()
54
  except Exception as e:
55
- print(f"Groq API connection down: {e}")
56
 
57
- # πŸ› οΈ SMART FALLBACK: If API fails, supply valid text so voice cloning NEVER breaks
58
  q_lower = question.lower()
59
  if "salesforce" in q_lower:
60
  return "Salesforce is a cloud-based customer relationship management platform that helps businesses manage sales, marketing, and customer service tracking in one place."
61
- return "Hello! I am your voice simulation assistant. Your system is online and ready for audio cloning."
62
 
63
- # 3. Robust Voice Simulation Block
 
 
64
  def clone_and_speak(text):
65
  try:
66
  unique_filename = f"voice_{uuid.uuid4().hex[:8]}.wav"
67
- print(f"πŸ”Š Generating cloned speech for text: '{text}' -> Saving to: {unique_filename}")
68
 
69
- # Execute voice simulation inference
70
  f5tts.infer(
71
  ref_file=voice_file,
72
  ref_text=auto_reference_text,
73
  gen_text=text,
74
- file_wave=unique_filename
 
 
75
  )
76
 
77
- # Format the audio file structure to enforce strict Gradio/Browser playback support
78
  if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
 
79
  data, samplerate = sf.read(unique_filename)
80
  sf.write(unique_filename, data, 24000, format='WAV', subtype='PCM_16')
81
- print(f"πŸŽ‰ Cloned audio track generated successfully: {unique_filename}")
82
  return unique_filename
83
  else:
84
- print("❌ Audio file generation mismatch: File is empty.")
85
  return None
86
 
87
  except Exception as e:
88
- print(f"❌ F5-TTS Voice Inference Error Details: {e}")
89
  return None
90
 
 
 
 
91
  def chatbot(text_input, audio_input):
92
  if audio_input is not None:
93
  result = whisper_model.transcribe(audio_input)
@@ -101,7 +108,9 @@ def chatbot(text_input, audio_input):
101
  audio_path = clone_and_speak(answer)
102
  return answer, audio_path
103
 
104
- # 4. Gradio Interface Construction
 
 
105
  interface = gr.Interface(
106
  fn=chatbot,
107
  inputs=[
 
3
  import requests
4
  import uuid
5
  import os
 
6
  import soundfile as sf
7
  from f5_tts.api import F5TTS
8
 
9
+ # ==========================================
10
+ # 1. Setup Configuration & Model Loading
11
+ # ==========================================
12
  GROQ_API_KEY = "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"
13
  voice_file = "my_voice_fixed.wav"
14
 
 
 
 
 
15
  print("Loading Whisper Model...")
16
+ whisper_model = whisper.load_model("base")
17
 
18
+ print("Loading F5-TTS Core Simulation Model...")
19
+ f5tts = F5TTS()
20
 
21
+ # Automatically extract reference text from your voice file at startup
22
  print(f"Extracting reference text from {voice_file}...")
23
  try:
24
  ref_result = whisper_model.transcribe(voice_file)
 
30
  print(f"⚠️ Text extraction warning: {e}")
31
  auto_reference_text = "Hello"
32
 
33
+ # ==========================================
34
+ # 2. Text Response Function (Groq API)
35
+ # ==========================================
36
  def get_ai_answer(question):
37
  try:
38
  response = requests.post(
 
49
  timeout=10
50
  )
51
  if response.status_code == 200:
52
+ # Fixed list indexing to unpack payload properly
53
  return response.json()["choices"][0]["message"]["content"].strip()
54
  except Exception as e:
55
+ print(f"Groq API connection error: {e}")
56
 
57
+ # Text fallback baseline to prevent downstream TTS pipeline crashes
58
  q_lower = question.lower()
59
  if "salesforce" in q_lower:
60
  return "Salesforce is a cloud-based customer relationship management platform that helps businesses manage sales, marketing, and customer service tracking in one place."
61
+ return "Hello! Your voice simulation system is online and ready for audio cloning."
62
 
63
+ # ==========================================
64
+ # 3. Voice Generation Block (F5-TTS)
65
+ # ==========================================
66
  def clone_and_speak(text):
67
  try:
68
  unique_filename = f"voice_{uuid.uuid4().hex[:8]}.wav"
69
+ print(f"πŸ”Š Initiating F5-TTS generation -> Targeting: {unique_filename}")
70
 
71
+ # Generates and writes the file directly to your workspace using the unique name
72
  f5tts.infer(
73
  ref_file=voice_file,
74
  ref_text=auto_reference_text,
75
  gen_text=text,
76
+ file_wave=unique_filename,
77
+ nfe_step=32, # Balanced steps for clean sound quality output
78
+ speed=1.0
79
  )
80
 
81
+ # Verify the file exists on disk and is formatted correctly for your browser player
82
  if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
83
+ print(f"πŸŽ‰ Voice generation successful! File written to disk: {unique_filename}")
84
  data, samplerate = sf.read(unique_filename)
85
  sf.write(unique_filename, data, 24000, format='WAV', subtype='PCM_16')
 
86
  return unique_filename
87
  else:
88
+ print("❌ Audio generation failed: File was not created on disk.")
89
  return None
90
 
91
  except Exception as e:
92
+ print(f"❌ F5-TTS Pipeline Exception Breakdown: {e}")
93
  return None
94
 
95
+ # ==========================================
96
+ # 4. Main Chatbot Routing & Logic
97
+ # ==========================================
98
  def chatbot(text_input, audio_input):
99
  if audio_input is not None:
100
  result = whisper_model.transcribe(audio_input)
 
108
  audio_path = clone_and_speak(answer)
109
  return answer, audio_path
110
 
111
+ # ==========================================
112
+ # 5. Gradio Interface Construction
113
+ # ==========================================
114
  interface = gr.Interface(
115
  fn=chatbot,
116
  inputs=[