Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,9 @@ import scipy.io.wavfile as wav
|
|
| 8 |
from groq import Groq
|
| 9 |
from f5_tts.api import F5TTS
|
| 10 |
|
| 11 |
-
# ==========================================
|
| 12 |
-
# 1. System Setup & Configuration
|
| 13 |
-
# ==========================================
|
| 14 |
GROQ_API_KEY = "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"
|
| 15 |
voice_file = "my_voice_fixed.wav"
|
| 16 |
|
|
@@ -43,9 +43,9 @@ except Exception as e:
|
|
| 43 |
|
| 44 |
print("All Speech Systems Fully Active!")
|
| 45 |
|
| 46 |
-
# ==========================================
|
| 47 |
-
# 2. Dynamic Text Response Generation
|
| 48 |
-
# ==========================================
|
| 49 |
def get_ai_answer(question):
|
| 50 |
try:
|
| 51 |
chat_completion = groq_client.chat.completions.create(
|
|
@@ -59,20 +59,12 @@ def get_ai_answer(question):
|
|
| 59 |
)
|
| 60 |
return chat_completion.choices[0].message.content.strip()
|
| 61 |
except Exception as e:
|
| 62 |
-
print(f"⚠️ Groq API issue detected ({e}). Processing
|
| 63 |
-
|
| 64 |
-
if "salesforce" in q_clean or "crm" in q_clean:
|
| 65 |
-
return "Salesforce is a cloud-based CRM platform used to manage customer data."
|
| 66 |
-
elif "java" in q_clean:
|
| 67 |
-
return "Java is a popular object-oriented programming language used worldwide."
|
| 68 |
-
elif "python" in q_clean:
|
| 69 |
-
return "Python is an interpreted programming language famous for clean syntax."
|
| 70 |
-
else:
|
| 71 |
-
return f"Answering your question about {question[:15]} with your voice."
|
| 72 |
|
| 73 |
-
# ==========================================
|
| 74 |
-
# 3. Native Voice Synthesis Processing
|
| 75 |
-
# ==========================================
|
| 76 |
def clone_and_speak(text):
|
| 77 |
try:
|
| 78 |
unique_filename = f"voice_output_{uuid.uuid4().hex[:8]}.wav"
|
|
@@ -95,7 +87,7 @@ def clone_and_speak(text):
|
|
| 95 |
audio_data, sample_rate = result[0], 24000
|
| 96 |
else:
|
| 97 |
audio_data, sample_rate = result, 24000
|
| 98 |
-
|
| 99 |
# Convert Torch tensors safely to standard numpy array structures
|
| 100 |
if hasattr(audio_data, "cpu"):
|
| 101 |
audio_data = audio_data.cpu().numpy()
|
|
@@ -116,9 +108,9 @@ def clone_and_speak(text):
|
|
| 116 |
print(f"❌ Core TTS System Exception triggered: {e}")
|
| 117 |
return None
|
| 118 |
|
| 119 |
-
# ==========================================
|
| 120 |
-
# 4. Request Router Pipeline
|
| 121 |
-
# ==========================================
|
| 122 |
def chatbot_engine(text_input, audio_input):
|
| 123 |
if audio_input is not None:
|
| 124 |
user_transcription = whisper_model.transcribe(audio_input, fp16=False)
|
|
@@ -131,11 +123,12 @@ def chatbot_engine(text_input, audio_input):
|
|
| 131 |
ai_text_response = get_ai_answer(question)
|
| 132 |
generated_audio_track = clone_and_speak(ai_text_response)
|
| 133 |
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
-
# ==========================================
|
| 137 |
-
# 5. Full-Output Single-Page UI Canvas
|
| 138 |
-
# ==========================================
|
| 139 |
with gr.Blocks(title="Rabbani A I voice clone") as interface:
|
| 140 |
gr.Markdown("# 🎙️ Rabbani A I voice clone")
|
| 141 |
gr.Markdown("Type or speak your question below. The system processes the response dynamically and outputs both text and voice simulation results simultaneously.")
|
|
@@ -152,15 +145,16 @@ with gr.Blocks(title="Rabbani A I voice clone") as interface:
|
|
| 152 |
gr.Markdown("### 📊 Cloned Output Responses")
|
| 153 |
text_output_box = gr.Textbox(label="AI Answer Text Output", interactive=False)
|
| 154 |
audio_output_player = gr.Audio(
|
| 155 |
-
label="Rendered Cloned Response Audio Player",
|
| 156 |
-
type="filepath",
|
| 157 |
interactive=False
|
| 158 |
)
|
| 159 |
|
|
|
|
| 160 |
submit_btn.click(
|
| 161 |
fn=chatbot_engine,
|
| 162 |
inputs=[text_prompt_box, audio_prompt_box],
|
| 163 |
-
outputs=[text_output_box, audio_output_player]
|
| 164 |
)
|
| 165 |
|
| 166 |
clear_btn.add([text_prompt_box, audio_prompt_box, text_output_box, audio_output_player])
|
|
|
|
| 8 |
from groq import Groq
|
| 9 |
from f5_tts.api import F5TTS
|
| 10 |
|
| 11 |
+
# ========================================== #
|
| 12 |
+
# 1. System Setup & Configuration #
|
| 13 |
+
# ========================================== #
|
| 14 |
GROQ_API_KEY = "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"
|
| 15 |
voice_file = "my_voice_fixed.wav"
|
| 16 |
|
|
|
|
| 43 |
|
| 44 |
print("All Speech Systems Fully Active!")
|
| 45 |
|
| 46 |
+
# ========================================== #
|
| 47 |
+
# 2. Dynamic Text Response Generation #
|
| 48 |
+
# ========================================== #
|
| 49 |
def get_ai_answer(question):
|
| 50 |
try:
|
| 51 |
chat_completion = groq_client.chat.completions.create(
|
|
|
|
| 59 |
)
|
| 60 |
return chat_completion.choices[0].message.content.strip()
|
| 61 |
except Exception as e:
|
| 62 |
+
print(f"⚠️ Groq API issue detected ({e}). Processing safe fallback...")
|
| 63 |
+
return "The AI engine is currently offline. Please try again later."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# ========================================== #
|
| 66 |
+
# 3. Native Voice Synthesis Processing #
|
| 67 |
+
# ========================================== #
|
| 68 |
def clone_and_speak(text):
|
| 69 |
try:
|
| 70 |
unique_filename = f"voice_output_{uuid.uuid4().hex[:8]}.wav"
|
|
|
|
| 87 |
audio_data, sample_rate = result[0], 24000
|
| 88 |
else:
|
| 89 |
audio_data, sample_rate = result, 24000
|
| 90 |
+
|
| 91 |
# Convert Torch tensors safely to standard numpy array structures
|
| 92 |
if hasattr(audio_data, "cpu"):
|
| 93 |
audio_data = audio_data.cpu().numpy()
|
|
|
|
| 108 |
print(f"❌ Core TTS System Exception triggered: {e}")
|
| 109 |
return None
|
| 110 |
|
| 111 |
+
# ========================================== #
|
| 112 |
+
# 4. Request Router Pipeline #
|
| 113 |
+
# ========================================== #
|
| 114 |
def chatbot_engine(text_input, audio_input):
|
| 115 |
if audio_input is not None:
|
| 116 |
user_transcription = whisper_model.transcribe(audio_input, fp16=False)
|
|
|
|
| 123 |
ai_text_response = get_ai_answer(question)
|
| 124 |
generated_audio_track = clone_and_speak(ai_text_response)
|
| 125 |
|
| 126 |
+
# Return outputs AND clear the input fields simultaneously
|
| 127 |
+
return ai_text_response, generated_audio_track, gr.update(value=""), gr.update(value=None)
|
| 128 |
|
| 129 |
+
# ========================================== #
|
| 130 |
+
# 5. Full-Output Single-Page UI Canvas #
|
| 131 |
+
# ========================================== #
|
| 132 |
with gr.Blocks(title="Rabbani A I voice clone") as interface:
|
| 133 |
gr.Markdown("# 🎙️ Rabbani A I voice clone")
|
| 134 |
gr.Markdown("Type or speak your question below. The system processes the response dynamically and outputs both text and voice simulation results simultaneously.")
|
|
|
|
| 145 |
gr.Markdown("### 📊 Cloned Output Responses")
|
| 146 |
text_output_box = gr.Textbox(label="AI Answer Text Output", interactive=False)
|
| 147 |
audio_output_player = gr.Audio(
|
| 148 |
+
label="Rendered Cloned Response Audio Player",
|
| 149 |
+
type="filepath",
|
| 150 |
interactive=False
|
| 151 |
)
|
| 152 |
|
| 153 |
+
# Connect UI outputs and reset input elements instantly upon submission
|
| 154 |
submit_btn.click(
|
| 155 |
fn=chatbot_engine,
|
| 156 |
inputs=[text_prompt_box, audio_prompt_box],
|
| 157 |
+
outputs=[text_output_box, audio_output_player, text_prompt_box, audio_prompt_box]
|
| 158 |
)
|
| 159 |
|
| 160 |
clear_btn.add([text_prompt_box, audio_prompt_box, text_output_box, audio_output_player])
|