import gradio as gr from huggingface_hub import InferenceClient import torch from TTS.api import TTS import soundfile as sf # Load TTS Model tts_model = TTS("tts_models/en/ljspeech/tacotron2-DDC").to("cuda" if torch.cuda.is_available() else "cpu") # Hugging Face LLM client (DeepSeek R1 7B) client = InferenceClient("deepseek-ai/deepseek-r1-7b") def respond( message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p, ): messages = [{"role": "system", "content": system_message}] for val in history: if val[0]: messages.append({"role": "user", "content": val[0]}) if val[1]: messages.append({"role": "assistant", "content": val[1]}) messages.append({"role": "user", "content": message}) response = "" for message in client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): token = message.choices[0].delta.content response += token yield response, None # Yielding text response first # Generate speech from text response output_audio_path = "response.wav" tts_model.tts_to_file(text=response, file_path=output_audio_path) yield response, output_audio_path # Yielding audio response # Using gr.Blocks() instead of ChatInterface with gr.Blocks() as demo: gr.Markdown("## DeepSeek R1 7B Chatbot with TTS") chatbot = gr.Chatbot() msg = gr.Textbox(label="User Input") system_msg = gr.Textbox(value="You are a friendly Chatbot.", label="System Message") max_tokens = gr.Slider(1, 2048, value=512, step=1, label="Max Tokens") temperature = gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature") top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (Nucleus Sampling)") tts_audio = gr.Audio(type="filepath", label="TTS Output") def chat_fn(message, history): return respond(message, history, system_msg.value, max_tokens.value, temperature.value, top_p.value) msg.submit(chat_fn, inputs=[msg, chatbot], outputs=[chatbot, tts_audio]) demo.launch()