File size: 4,143 Bytes
2b83aa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import gradio as gr
import requests
import os

# ElevenLabs API Constants
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"]
            # List banayenge: "VoiceName - VoiceID"
            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 nikalna string se
        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}"

# --- UI Design ---
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():
            # Step 1: API Key Input
            api_input = gr.Textbox(
                label="Step 1: ElevenLabs API Key",
                placeholder="Apni API Key yahan paste karein...",
                type="password"
            )
            
            # Button to load voices
            load_btn = gr.Button("πŸ”„ Load My Voices (Pappi etc.)")
            
            # Step 2: Settings
            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", # Best for Hindi
                    "eleven_multilingual_v1", # Old Hindi
                    "eleven_turbo_v2",        # Fast English
                    "eleven_monolingual_v1"   # English Only
                ],
                value="eleven_multilingual_v2"
            )

            # Step 3: Text
            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():
            # Output
            audio_output = gr.Audio(label="Generated Audio")
            status_msg = gr.Textbox(label="Status")

    # Functions linking
    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)