Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| from transformers import pipeline | |
| from datasets import load_dataset | |
| import soundfile as sf | |
| import torch | |
| # Initialize the chat model | |
| chat_client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf") | |
| # Initialize the TTS pipeline | |
| tts_synthesizer = pipeline("text-to-speech", model="Futuresony/Output") | |
| # Load the speaker embeddings dataset | |
| embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation") | |
| speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0) | |
| def chat_with_tts(message, history, system_message, max_tokens, temperature, top_p): | |
| # Step 1: Generate response using the chat model | |
| 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 msg in chat_client.chat_completion( | |
| messages, | |
| max_tokens=max_tokens, | |
| stream=True, | |
| temperature=temperature, | |
| top_p=top_p, | |
| ): | |
| token = msg.choices[0].delta.content | |
| response += token | |
| # Step 2: Generate speech using TTS | |
| speech = tts_synthesizer(response, forward_params={"speaker_embeddings": speaker_embedding}) | |
| output_file = "generated_speech.wav" | |
| sf.write(output_file, speech["audio"], samplerate=speech["sampling_rate"]) | |
| # Update the chat history | |
| history.append((message, response)) | |
| # Return both text response, audio file, and updated history | |
| return response, output_file, history | |
| # Create the Gradio interface | |
| demo = gr.Interface( | |
| fn=chat_with_tts, | |
| inputs=[ | |
| gr.Textbox(label="User Input", placeholder="Type your message..."), | |
| gr.State([]), # Initialize history as an empty list | |
| gr.Textbox(value="You are a friendly chatbot.", label="System Message"), | |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"), | |
| gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"), | |
| ], | |
| outputs=[ | |
| gr.Textbox(label="Generated Response"), | |
| gr.Audio(label="Generated Speech"), | |
| gr.State(), # Add State as an output to update the history | |
| ], | |
| title="Chat with TTS", | |
| description="Enter text to chat with an AI chatbot. The chatbot will generate a response, which will also be converted to speech using TTS." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |