Spaces:
Sleeping
Sleeping
Update app.py
#1
by
Hamzasha - opened
app.py
CHANGED
|
@@ -9,6 +9,7 @@ print("Gradio version:", gr.__version__)
|
|
| 9 |
|
| 10 |
# βββ 1. Read & validate API key from environment ββββββββββββββββββββββββββββββββ
|
| 11 |
api_key = os.getenv("GOOGLE_API_KEY")
|
|
|
|
| 12 |
if not api_key:
|
| 13 |
raise ValueError("Environment variable 'GOOGLE_API_KEY' not found.")
|
| 14 |
|
|
@@ -37,6 +38,7 @@ def generate_audio(
|
|
| 37 |
instructions: str,
|
| 38 |
text: str,
|
| 39 |
voice_name: str,
|
|
|
|
| 40 |
custom_additions: str,
|
| 41 |
) -> str:
|
| 42 |
full_prompt = f"""
|
|
@@ -68,7 +70,38 @@ def generate_audio(
|
|
| 68 |
|
| 69 |
pcm_data = response.candidates[0].content.parts[0].inline_data.data
|
| 70 |
wav_path = wave_file(pcm_data)
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# βββ 5. Gradio UI components ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 74 |
model_choices = ["gemini-2.5-flash-preview-tts", "gemini-2.5-pro-preview-tts"]
|
|
@@ -115,6 +148,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 115 |
clarity = gr.Dropdown(clarity_choices, value="Very clear", label="Clarity")
|
| 116 |
style = gr.Dropdown(style_choices, value="Motivational and casual", label="Style")
|
| 117 |
voice_name = gr.Dropdown(voice_names, value="Charon", label="Voice Name")
|
|
|
|
| 118 |
instructions = gr.Textbox(
|
| 119 |
value="Imagine you're speaking to someone close, giving them an energy boost to start their day. Use expressive intonation and add emotional warmth.",
|
| 120 |
lines=3,
|
|
|
|
| 9 |
|
| 10 |
# βββ 1. Read & validate API key from environment ββββββββββββββββββββββββββββββββ
|
| 11 |
api_key = os.getenv("GOOGLE_API_KEY")
|
| 12 |
+
eleven_api_key = os.getenv("ELEVEN_API_KEY")
|
| 13 |
if not api_key:
|
| 14 |
raise ValueError("Environment variable 'GOOGLE_API_KEY' not found.")
|
| 15 |
|
|
|
|
| 38 |
instructions: str,
|
| 39 |
text: str,
|
| 40 |
voice_name: str,
|
| 41 |
+
premium_voice_id: str,
|
| 42 |
custom_additions: str,
|
| 43 |
) -> str:
|
| 44 |
full_prompt = f"""
|
|
|
|
| 70 |
|
| 71 |
pcm_data = response.candidates[0].content.parts[0].inline_data.data
|
| 72 |
wav_path = wave_file(pcm_data)
|
| 73 |
+
if premium_voice_id is None:
|
| 74 |
+
return wav_path
|
| 75 |
+
else :
|
| 76 |
+
url = f"https://api.elevenlabs.io/v1/speech-to-speech/{premium_voice_id}"
|
| 77 |
+
|
| 78 |
+
headers = {
|
| 79 |
+
"xi-api-key": eleven_api_key,
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
# Read the audio file
|
| 83 |
+
with open(wav_path, "rb") as audio_file:
|
| 84 |
+
files = {
|
| 85 |
+
"audio": audio_file,
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
data = {
|
| 89 |
+
"model_id": "eleven_multilingual_sts_v2", # or eleven_english_sts_v2
|
| 90 |
+
"output_format": "mp3_44100_128",
|
| 91 |
+
# Optional: "voice_settings": json.dumps({...}),
|
| 92 |
+
# Optional: "seed": 12345,
|
| 93 |
+
# Optional: "remove_background_noise": "true",
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
response = requests.post(url, headers=headers, files=files, data=data)
|
| 97 |
+
if response.ok:
|
| 98 |
+
with open("output.mp3", "wb") as f:
|
| 99 |
+
f.write(response.content)
|
| 100 |
+
print("β
Voice converted and saved as output.mp3")
|
| 101 |
+
else:
|
| 102 |
+
print("β Error:", response.status_code, response.text)
|
| 103 |
+
|
| 104 |
+
return "output.mp3" # Gradio will serve this as a URL
|
| 105 |
|
| 106 |
# βββ 5. Gradio UI components ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 107 |
model_choices = ["gemini-2.5-flash-preview-tts", "gemini-2.5-pro-preview-tts"]
|
|
|
|
| 148 |
clarity = gr.Dropdown(clarity_choices, value="Very clear", label="Clarity")
|
| 149 |
style = gr.Dropdown(style_choices, value="Motivational and casual", label="Style")
|
| 150 |
voice_name = gr.Dropdown(voice_names, value="Charon", label="Voice Name")
|
| 151 |
+
premium_voice_id = gr.Textbox(label="premium Voice ID (optional)")
|
| 152 |
instructions = gr.Textbox(
|
| 153 |
value="Imagine you're speaking to someone close, giving them an energy boost to start their day. Use expressive intonation and add emotional warmth.",
|
| 154 |
lines=3,
|