Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# ElevenLabs API Constants
|
| 6 |
+
BASE_URL = "https://api.elevenlabs.io/v1"
|
| 7 |
+
|
| 8 |
+
def get_voices(api_key):
|
| 9 |
+
"""ElevenLabs account se saari voices fetch karega"""
|
| 10 |
+
if not api_key:
|
| 11 |
+
return []
|
| 12 |
+
|
| 13 |
+
headers = {"xi-api-key": api_key}
|
| 14 |
+
try:
|
| 15 |
+
response = requests.get(f"{BASE_URL}/voices", headers=headers)
|
| 16 |
+
if response.status_code == 200:
|
| 17 |
+
voices_data = response.json()["voices"]
|
| 18 |
+
# List banayenge: "VoiceName - VoiceID"
|
| 19 |
+
return [f"{v['name']} - {v['voice_id']}" for v in voices_data]
|
| 20 |
+
else:
|
| 21 |
+
return ["Error: API Key galat hai ya Network issue"]
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return [f"Error: {e}"]
|
| 24 |
+
|
| 25 |
+
def generate_audio(api_key, text, voice_str, model_id):
|
| 26 |
+
"""Text ko Audio mein convert karega"""
|
| 27 |
+
if not api_key or not text or not voice_str:
|
| 28 |
+
return None, "β οΈ Pehle API Key, Voice aur Text daalein."
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
# Voice ID nikalna string se
|
| 32 |
+
voice_id = voice_str.split(" - ")[-1]
|
| 33 |
+
|
| 34 |
+
url = f"{BASE_URL}/text-to-speech/{voice_id}"
|
| 35 |
+
headers = {
|
| 36 |
+
"Accept": "audio/mpeg",
|
| 37 |
+
"Content-Type": "application/json",
|
| 38 |
+
"xi-api-key": api_key
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
data = {
|
| 42 |
+
"text": text,
|
| 43 |
+
"model_id": model_id,
|
| 44 |
+
"voice_settings": {
|
| 45 |
+
"stability": 0.5,
|
| 46 |
+
"similarity_boost": 0.75
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
response = requests.post(url, json=data, headers=headers)
|
| 51 |
+
|
| 52 |
+
if response.status_code == 200:
|
| 53 |
+
output_file = "output_elevenlabs.mp3"
|
| 54 |
+
with open(output_file, "wb") as f:
|
| 55 |
+
f.write(response.content)
|
| 56 |
+
return output_file, "β
Audio Taiyar Hai!"
|
| 57 |
+
else:
|
| 58 |
+
return None, f"β Error: {response.text}"
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return None, f"β System Error: {e}"
|
| 62 |
+
|
| 63 |
+
# --- UI Design ---
|
| 64 |
+
with gr.Blocks(title="Deepak's ElevenLabs Tool") as demo:
|
| 65 |
+
gr.Markdown("# ποΈ Deepak's Official ElevenLabs Generator")
|
| 66 |
+
gr.Markdown("Apni API Key daalein aur high-quality Hindi/English audio banayein.")
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
with gr.Column():
|
| 70 |
+
# Step 1: API Key Input
|
| 71 |
+
api_input = gr.Textbox(
|
| 72 |
+
label="Step 1: ElevenLabs API Key",
|
| 73 |
+
placeholder="Apni API Key yahan paste karein...",
|
| 74 |
+
type="password"
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Button to load voices
|
| 78 |
+
load_btn = gr.Button("π Load My Voices (Pappi etc.)")
|
| 79 |
+
|
| 80 |
+
# Step 2: Settings
|
| 81 |
+
voice_dropdown = gr.Dropdown(label="Step 2: Character Select Karein", choices=[])
|
| 82 |
+
|
| 83 |
+
model_dropdown = gr.Dropdown(
|
| 84 |
+
label="Step 3: Model Select Karein",
|
| 85 |
+
choices=[
|
| 86 |
+
"eleven_multilingual_v2", # Best for Hindi
|
| 87 |
+
"eleven_multilingual_v1", # Old Hindi
|
| 88 |
+
"eleven_turbo_v2", # Fast English
|
| 89 |
+
"eleven_monolingual_v1" # English Only
|
| 90 |
+
],
|
| 91 |
+
value="eleven_multilingual_v2"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Step 3: Text
|
| 95 |
+
text_input = gr.Textbox(
|
| 96 |
+
label="Step 4: Text Likhein",
|
| 97 |
+
placeholder="Namaste, main Pappi hoon. Kaise hain aap?",
|
| 98 |
+
lines=4
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
generate_btn = gr.Button("π Generate Audio", variant="primary")
|
| 102 |
+
|
| 103 |
+
with gr.Column():
|
| 104 |
+
# Output
|
| 105 |
+
audio_output = gr.Audio(label="Generated Audio")
|
| 106 |
+
status_msg = gr.Textbox(label="Status")
|
| 107 |
+
|
| 108 |
+
# Functions linking
|
| 109 |
+
def update_voices(key):
|
| 110 |
+
voices = get_voices(key)
|
| 111 |
+
return gr.Dropdown(choices=voices, value=voices[0] if voices else None)
|
| 112 |
+
|
| 113 |
+
load_btn.click(fn=update_voices, inputs=[api_input], outputs=[voice_dropdown])
|
| 114 |
+
|
| 115 |
+
generate_btn.click(
|
| 116 |
+
fn=generate_audio,
|
| 117 |
+
inputs=[api_input, text_input, voice_dropdown, model_dropdown],
|
| 118 |
+
outputs=[audio_output, status_msg]
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 123 |
+
|