Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import tempfile | |
| from gtts import gTTS | |
| def clone_voice(text, audio): | |
| if not text: | |
| return 'Please enter text', None | |
| groq_key = os.getenv('GROQ_API_KEY') | |
| openai_key = os.getenv('OPENAI_API_KEY') | |
| if not groq_key and not openai_key: | |
| tts = gTTS(text) | |
| temp_file = tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) | |
| tts.save(temp_file.name) | |
| return f'Demo TTS for: {text}', temp_file.name | |
| try: | |
| if groq_key: | |
| import groq | |
| client = groq.Groq(api_key=groq_key) | |
| if audio: | |
| with open(audio, 'rb') as f: | |
| transcription = client.audio.transcriptions.create( | |
| model='whisper-large-v3', | |
| file=f | |
| ) | |
| return f'Transcribed: {transcription.text}', None | |
| tts = gTTS(text) | |
| temp_file = tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) | |
| tts.save(temp_file.name) | |
| return f'Generated TTS for: {text}', temp_file.name | |
| except Exception as e: | |
| return f'Error: {str(e)}', None | |
| with gr.Blocks(title='AI Voice Clone Demo') as demo: | |
| gr.Markdown('# 🎙️ AI Voice Clone Demo') | |
| gr.Markdown('Clone any voice with AI using advanced TTS models.') | |
| groq_key = os.getenv('GROQ_API_KEY') | |
| openai_key = os.getenv('OPENAI_API_KEY') | |
| if not groq_key and not openai_key: | |
| gr.Warning('⚠️ API keys not set. Using demo TTS mode.') | |
| else: | |
| gr.Markdown('✅ API key detected. Voice cloning ready!') | |
| with gr.Row(): | |
| text = gr.Textbox(label='Text to speak') | |
| audio = gr.Audio(label='Voice sample (optional)', type='filepath') | |
| btn = gr.Button('Clone Voice') | |
| output_text = gr.Textbox(label='Result') | |
| output_audio = gr.Audio(label='Generated Voice') | |
| btn.click(clone_voice, [text, audio], [output_text, output_audio]) | |
| gr.Markdown('## 📦 Install') | |
| gr.Markdown('```bash\npip install ai-voice-clone\n```') | |
| gr.Markdown('**GitHub:** https://github.com/my-ai-stack/ai-voice-clone') | |
| demo.launch() | |