|
|
import gradio as gr |
|
|
import requests |
|
|
import os |
|
|
|
|
|
|
|
|
BASE_URL = "https://api.elevenlabs.io/v1" |
|
|
|
|
|
def get_voices(api_key): |
|
|
"""ElevenLabs account se saari voices fetch karega""" |
|
|
if not api_key: |
|
|
return [] |
|
|
|
|
|
headers = {"xi-api-key": api_key} |
|
|
try: |
|
|
response = requests.get(f"{BASE_URL}/voices", headers=headers) |
|
|
if response.status_code == 200: |
|
|
voices_data = response.json()["voices"] |
|
|
|
|
|
return [f"{v['name']} - {v['voice_id']}" for v in voices_data] |
|
|
else: |
|
|
return ["Error: API Key galat hai ya Network issue"] |
|
|
except Exception as e: |
|
|
return [f"Error: {e}"] |
|
|
|
|
|
def generate_audio(api_key, text, voice_str, model_id): |
|
|
"""Text ko Audio mein convert karega""" |
|
|
if not api_key or not text or not voice_str: |
|
|
return None, "β οΈ Pehle API Key, Voice aur Text daalein." |
|
|
|
|
|
try: |
|
|
|
|
|
voice_id = voice_str.split(" - ")[-1] |
|
|
|
|
|
url = f"{BASE_URL}/text-to-speech/{voice_id}" |
|
|
headers = { |
|
|
"Accept": "audio/mpeg", |
|
|
"Content-Type": "application/json", |
|
|
"xi-api-key": api_key |
|
|
} |
|
|
|
|
|
data = { |
|
|
"text": text, |
|
|
"model_id": model_id, |
|
|
"voice_settings": { |
|
|
"stability": 0.5, |
|
|
"similarity_boost": 0.75 |
|
|
} |
|
|
} |
|
|
|
|
|
response = requests.post(url, json=data, headers=headers) |
|
|
|
|
|
if response.status_code == 200: |
|
|
output_file = "output_elevenlabs.mp3" |
|
|
with open(output_file, "wb") as f: |
|
|
f.write(response.content) |
|
|
return output_file, "β
Audio Taiyar Hai!" |
|
|
else: |
|
|
return None, f"β Error: {response.text}" |
|
|
|
|
|
except Exception as e: |
|
|
return None, f"β System Error: {e}" |
|
|
|
|
|
|
|
|
with gr.Blocks(title="Deepak's ElevenLabs Tool") as demo: |
|
|
gr.Markdown("# ποΈ Deepak's Official ElevenLabs Generator") |
|
|
gr.Markdown("Apni API Key daalein aur high-quality Hindi/English audio banayein.") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
|
|
|
api_input = gr.Textbox( |
|
|
label="Step 1: ElevenLabs API Key", |
|
|
placeholder="Apni API Key yahan paste karein...", |
|
|
type="password" |
|
|
) |
|
|
|
|
|
|
|
|
load_btn = gr.Button("π Load My Voices (Pappi etc.)") |
|
|
|
|
|
|
|
|
voice_dropdown = gr.Dropdown(label="Step 2: Character Select Karein", choices=[]) |
|
|
|
|
|
model_dropdown = gr.Dropdown( |
|
|
label="Step 3: Model Select Karein", |
|
|
choices=[ |
|
|
"eleven_multilingual_v2", |
|
|
"eleven_multilingual_v1", |
|
|
"eleven_turbo_v2", |
|
|
"eleven_monolingual_v1" |
|
|
], |
|
|
value="eleven_multilingual_v2" |
|
|
) |
|
|
|
|
|
|
|
|
text_input = gr.Textbox( |
|
|
label="Step 4: Text Likhein", |
|
|
placeholder="Namaste, main Pappi hoon. Kaise hain aap?", |
|
|
lines=4 |
|
|
) |
|
|
|
|
|
generate_btn = gr.Button("π Generate Audio", variant="primary") |
|
|
|
|
|
with gr.Column(): |
|
|
|
|
|
audio_output = gr.Audio(label="Generated Audio") |
|
|
status_msg = gr.Textbox(label="Status") |
|
|
|
|
|
|
|
|
def update_voices(key): |
|
|
voices = get_voices(key) |
|
|
return gr.Dropdown(choices=voices, value=voices[0] if voices else None) |
|
|
|
|
|
load_btn.click(fn=update_voices, inputs=[api_input], outputs=[voice_dropdown]) |
|
|
|
|
|
generate_btn.click( |
|
|
fn=generate_audio, |
|
|
inputs=[api_input, text_input, voice_dropdown, model_dropdown], |
|
|
outputs=[audio_output, status_msg] |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(server_name="0.0.0.0", server_port=7860) |
|
|
|