from huggingface_hub import InferenceClient import gradio as gr import os client = InferenceClient(api_key=os.getenv("HF_TOKEN")) def generate_speech(text): if not text.strip(): return None try: audio_bytes = client.text_to_speech( text, extra_body={ "exaggeration": 0.25, "temperature": 0.7, "audio_url": "https://huggingface.co/spaces/abidlabs/hfstudio/resolve/main/frontend/static/voices/andrew.mp3", } ) return audio_bytes except Exception as e: raise gr.Error(f"Error generating speech: {str(e)}") # Create the Gradio interface with gr.Blocks(title="Text to Speech") as demo: gr.Markdown("# Text to Speech") gr.Markdown("Convert text to speech using the Chatterbox model.") with gr.Row(): with gr.Column(): text_input = gr.Textbox( label="Text to convert to speech", placeholder="In a hole in the ground, there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of...", lines=5, value="In a hole in the ground, there lived a hobbit. Not a nasty, dirty, wet hole, filled with the ends of worms and an oozy smell, nor yet a dry, bare, sandy hole with nothing in it to sit down on or to eat: it was a hobbit-hole, and that means comfort." ) generate_btn = gr.Button("Generate Speech", variant="primary") with gr.Column(): audio_output = gr.Audio(label="Generated Speech") generate_btn.click( fn=generate_speech, inputs=[text_input], outputs=[audio_output] ) if __name__ == "__main__": demo.launch()