Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,15 +7,14 @@ import requests
|
|
| 7 |
import gradio as gr
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
load_dotenv()
|
| 12 |
|
|
|
|
| 13 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 14 |
GROQ_TTS_ENDPOINT = "https://api.groq.com/openai/v1/audio/speech"
|
| 15 |
-
|
| 16 |
DEFAULT_MODEL = "playai-tts"
|
| 17 |
|
| 18 |
-
# Simple list of example voices — you can replace or extend these with names from your Groq account.
|
| 19 |
EXAMPLE_VOICES = [
|
| 20 |
"Fritz-PlayAI",
|
| 21 |
"Emma-PlayAI",
|
|
@@ -24,9 +23,9 @@ EXAMPLE_VOICES = [
|
|
| 24 |
|
| 25 |
|
| 26 |
def synthesize_text(text: str, voice: str = None, model: str = DEFAULT_MODEL, response_format: str = "wav"):
|
| 27 |
-
"""Call Groq TTS and
|
| 28 |
if not GROQ_API_KEY:
|
| 29 |
-
return None, "Missing GROQ_API_KEY environment variable.
|
| 30 |
|
| 31 |
if not text or text.strip() == "":
|
| 32 |
return None, "Please provide some text to synthesize."
|
|
@@ -38,3 +37,55 @@ def synthesize_text(text: str, voice: str = None, model: str = DEFAULT_MODEL, re
|
|
| 38 |
}
|
| 39 |
if voice:
|
| 40 |
payload["voice"] = voice
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
| 10 |
+
# Load .env for local use (Hugging Face Spaces ignores if not present)
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
+
# --- Configuration ---
|
| 14 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 15 |
GROQ_TTS_ENDPOINT = "https://api.groq.com/openai/v1/audio/speech"
|
|
|
|
| 16 |
DEFAULT_MODEL = "playai-tts"
|
| 17 |
|
|
|
|
| 18 |
EXAMPLE_VOICES = [
|
| 19 |
"Fritz-PlayAI",
|
| 20 |
"Emma-PlayAI",
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
def synthesize_text(text: str, voice: str = None, model: str = DEFAULT_MODEL, response_format: str = "wav"):
|
| 26 |
+
"""Call Groq TTS and return path to temporary audio file."""
|
| 27 |
if not GROQ_API_KEY:
|
| 28 |
+
return None, "Missing GROQ_API_KEY environment variable. Add it in your Hugging Face Space Secrets."
|
| 29 |
|
| 30 |
if not text or text.strip() == "":
|
| 31 |
return None, "Please provide some text to synthesize."
|
|
|
|
| 37 |
}
|
| 38 |
if voice:
|
| 39 |
payload["voice"] = voice
|
| 40 |
+
|
| 41 |
+
headers = {
|
| 42 |
+
"Content-Type": "application/json",
|
| 43 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
resp = requests.post(GROQ_TTS_ENDPOINT, json=payload, headers=headers, stream=True, timeout=60)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return None, f"Request failed: {e}"
|
| 50 |
+
|
| 51 |
+
if resp.status_code != 200:
|
| 52 |
+
try:
|
| 53 |
+
detail = resp.json()
|
| 54 |
+
except Exception:
|
| 55 |
+
detail = resp.text
|
| 56 |
+
return None, f"Groq API error ({resp.status_code}): {detail}"
|
| 57 |
+
|
| 58 |
+
suffix = ".wav" if response_format == "wav" else ".mp3"
|
| 59 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
|
| 60 |
+
for chunk in resp.iter_content(chunk_size=8192):
|
| 61 |
+
if chunk:
|
| 62 |
+
tmp.write(chunk)
|
| 63 |
+
tmp.flush()
|
| 64 |
+
tmp.close()
|
| 65 |
+
|
| 66 |
+
return tmp.name, "Synthesis successful!"
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
with gr.Blocks(title="GroqCloud TTS — Hugging Face Space") as demo:
|
| 70 |
+
gr.Markdown("# 🎙️ GroqCloud Text-to-Speech\nConvert your text to natural speech using Groq TTS API.")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
with gr.Column(scale=3):
|
| 74 |
+
txt = gr.Textbox(lines=6, label="Text", placeholder="Enter text here...")
|
| 75 |
+
voice = gr.Dropdown(EXAMPLE_VOICES, label="Voice", value=EXAMPLE_VOICES[0])
|
| 76 |
+
resp_format = gr.Radio(["wav", "mp3"], value="wav", label="Output format")
|
| 77 |
+
synth_btn = gr.Button("🔊 Synthesize")
|
| 78 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 79 |
+
|
| 80 |
+
with gr.Column(scale=2):
|
| 81 |
+
audio_out = gr.Audio(label="Generated Audio", type="filepath")
|
| 82 |
+
|
| 83 |
+
def on_synthesize(text, voice, response_format):
|
| 84 |
+
path, msg = synthesize_text(text, voice, DEFAULT_MODEL, response_format)
|
| 85 |
+
return path, msg
|
| 86 |
+
|
| 87 |
+
synth_btn.click(on_synthesize, inputs=[txt, voice, resp_format], outputs=[audio_out, status])
|
| 88 |
+
|
| 89 |
+
# ✅ Important: launch the app!
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
demo.launch()
|