Update app.py
Browse files
app.py
CHANGED
|
@@ -5,13 +5,13 @@ import os
|
|
| 5 |
import soundfile as sf
|
| 6 |
import numpy as np
|
| 7 |
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 |
-
#
|
| 15 |
GROQ_API_KEY = "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"
|
| 16 |
voice_file = "my_voice_fixed.wav"
|
| 17 |
|
|
@@ -23,7 +23,6 @@ if not os.path.exists(voice_file):
|
|
| 23 |
wav.write(voice_file, sample_rate, audio_data)
|
| 24 |
|
| 25 |
print("Initializing Official Groq Client Subsystem...")
|
| 26 |
-
# Initializes the official SDK parser securely
|
| 27 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 28 |
|
| 29 |
print("Loading Lightweight Whisper Engine...")
|
|
@@ -50,7 +49,6 @@ print("All Speech Systems Fully Active!")
|
|
| 50 |
# ==========================================
|
| 51 |
def get_ai_answer(question):
|
| 52 |
try:
|
| 53 |
-
# The SDK automatically handles the correct endpoint URL and payload layout under the hood
|
| 54 |
chat_completion = groq_client.chat.completions.create(
|
| 55 |
messages=[
|
| 56 |
{"role": "system", "content": "You are a professional voice simulation assistant. Keep answers short and under 30 words."},
|
|
@@ -60,10 +58,7 @@ def get_ai_answer(question):
|
|
| 60 |
max_tokens=80,
|
| 61 |
timeout=10
|
| 62 |
)
|
| 63 |
-
|
| 64 |
-
# Safe object extraction provided directly by the library
|
| 65 |
return chat_completion.choices[0].message.content.strip()
|
| 66 |
-
|
| 67 |
except Exception as e:
|
| 68 |
print(f"Groq SDK Exception Details: {e}")
|
| 69 |
return f"Inference processing error occurred: {str(e)}"
|
|
@@ -76,8 +71,9 @@ def clone_and_speak(text):
|
|
| 76 |
unique_filename = f"voice_output_{uuid.uuid4().hex[:8]}.wav"
|
| 77 |
print(f"π Rendering voice cloning track for text: '{text}'")
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
ref_text=auto_reference_text,
|
| 82 |
gen_text=text
|
| 83 |
)
|
|
@@ -90,7 +86,6 @@ def clone_and_speak(text):
|
|
| 90 |
if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
|
| 91 |
print(f"π Generated track successfully saved to: {unique_filename}")
|
| 92 |
return unique_filename
|
| 93 |
-
|
| 94 |
return None
|
| 95 |
except Exception as e:
|
| 96 |
print(f"β Core TTS System Exception triggered: {e}")
|
|
@@ -107,7 +102,7 @@ def chatbot_engine(text_input, audio_input):
|
|
| 107 |
question = text_input
|
| 108 |
else:
|
| 109 |
return "System Notification: Please enter a text question or record your microphone.", None
|
| 110 |
-
|
| 111 |
ai_text_response = get_ai_answer(question)
|
| 112 |
generated_audio_track = clone_and_speak(ai_text_response)
|
| 113 |
|
|
@@ -125,7 +120,6 @@ with gr.Blocks(title="Rabbani A I voice clone") as interface:
|
|
| 125 |
gr.Markdown("### π¬ Input Interaction Canvas")
|
| 126 |
text_prompt_box = gr.Textbox(placeholder="Type your custom question query here...", label="Your Written Question")
|
| 127 |
audio_prompt_box = gr.Audio(label="Or Record/Speak Your Question Directly", type="filepath")
|
| 128 |
-
|
| 129 |
submit_btn = gr.Button("Send Question to AI Engine", variant="primary")
|
| 130 |
clear_btn = gr.ClearButton(value="Clear Inputs")
|
| 131 |
|
|
@@ -133,11 +127,11 @@ with gr.Blocks(title="Rabbani A I voice clone") as interface:
|
|
| 133 |
gr.Markdown("### π Cloned Output Responses")
|
| 134 |
text_output_box = gr.Textbox(label="AI Answer Text Output", interactive=False)
|
| 135 |
audio_output_player = gr.Audio(
|
| 136 |
-
label="Rendered Cloned Response Audio Player",
|
| 137 |
-
type="filepath",
|
| 138 |
interactive=False
|
| 139 |
)
|
| 140 |
-
|
| 141 |
submit_btn.click(
|
| 142 |
fn=chatbot_engine,
|
| 143 |
inputs=[text_prompt_box, audio_prompt_box],
|
|
|
|
| 5 |
import soundfile as sf
|
| 6 |
import numpy as np
|
| 7 |
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 |
+
# β οΈ REMINDER: Update this key if your Groq organization remains restricted!
|
| 15 |
GROQ_API_KEY = "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"
|
| 16 |
voice_file = "my_voice_fixed.wav"
|
| 17 |
|
|
|
|
| 23 |
wav.write(voice_file, sample_rate, audio_data)
|
| 24 |
|
| 25 |
print("Initializing Official Groq Client Subsystem...")
|
|
|
|
| 26 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 27 |
|
| 28 |
print("Loading Lightweight Whisper Engine...")
|
|
|
|
| 49 |
# ==========================================
|
| 50 |
def get_ai_answer(question):
|
| 51 |
try:
|
|
|
|
| 52 |
chat_completion = groq_client.chat.completions.create(
|
| 53 |
messages=[
|
| 54 |
{"role": "system", "content": "You are a professional voice simulation assistant. Keep answers short and under 30 words."},
|
|
|
|
| 58 |
max_tokens=80,
|
| 59 |
timeout=10
|
| 60 |
)
|
|
|
|
|
|
|
| 61 |
return chat_completion.choices[0].message.content.strip()
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
print(f"Groq SDK Exception Details: {e}")
|
| 64 |
return f"Inference processing error occurred: {str(e)}"
|
|
|
|
| 71 |
unique_filename = f"voice_output_{uuid.uuid4().hex[:8]}.wav"
|
| 72 |
print(f"π Rendering voice cloning track for text: '{text}'")
|
| 73 |
|
| 74 |
+
# FIXED: Changed ref_audio= to ref_file= to match the F5TTS API layout
|
| 75 |
+
audio_data, sample_rate, spectogram = f5tts.infer(
|
| 76 |
+
ref_file=voice_file,
|
| 77 |
ref_text=auto_reference_text,
|
| 78 |
gen_text=text
|
| 79 |
)
|
|
|
|
| 86 |
if os.path.exists(unique_filename) and os.path.getsize(unique_filename) > 0:
|
| 87 |
print(f"π Generated track successfully saved to: {unique_filename}")
|
| 88 |
return unique_filename
|
|
|
|
| 89 |
return None
|
| 90 |
except Exception as e:
|
| 91 |
print(f"β Core TTS System Exception triggered: {e}")
|
|
|
|
| 102 |
question = text_input
|
| 103 |
else:
|
| 104 |
return "System Notification: Please enter a text question or record your microphone.", None
|
| 105 |
+
|
| 106 |
ai_text_response = get_ai_answer(question)
|
| 107 |
generated_audio_track = clone_and_speak(ai_text_response)
|
| 108 |
|
|
|
|
| 120 |
gr.Markdown("### π¬ Input Interaction Canvas")
|
| 121 |
text_prompt_box = gr.Textbox(placeholder="Type your custom question query here...", label="Your Written Question")
|
| 122 |
audio_prompt_box = gr.Audio(label="Or Record/Speak Your Question Directly", type="filepath")
|
|
|
|
| 123 |
submit_btn = gr.Button("Send Question to AI Engine", variant="primary")
|
| 124 |
clear_btn = gr.ClearButton(value="Clear Inputs")
|
| 125 |
|
|
|
|
| 127 |
gr.Markdown("### π Cloned Output Responses")
|
| 128 |
text_output_box = gr.Textbox(label="AI Answer Text Output", interactive=False)
|
| 129 |
audio_output_player = gr.Audio(
|
| 130 |
+
label="Rendered Cloned Response Audio Player",
|
| 131 |
+
type="filepath",
|
| 132 |
interactive=False
|
| 133 |
)
|
| 134 |
+
|
| 135 |
submit_btn.click(
|
| 136 |
fn=chatbot_engine,
|
| 137 |
inputs=[text_prompt_box, audio_prompt_box],
|