Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
from transformers import pipeline
|
5 |
+
from datasets import load_dataset
|
6 |
+
import soundfile as sf
|
7 |
+
import torch
|
8 |
+
|
9 |
+
# Initialize the chat model
|
10 |
+
chat_client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
11 |
+
|
12 |
+
# Initialize the TTS pipeline
|
13 |
+
tts_synthesizer = pipeline("text-to-speech", model="Futuresony/output")
|
14 |
+
|
15 |
+
# Load the speaker embeddings dataset
|
16 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
17 |
+
speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
18 |
+
|
19 |
+
def chat_with_tts(message, history, system_message, max_tokens, temperature, top_p):
|
20 |
+
# Step 1: Generate response using the chat model
|
21 |
+
messages = [{"role": "system", "content": system_message}]
|
22 |
+
|
23 |
+
for val in history:
|
24 |
+
if val[0]:
|
25 |
+
messages.append({"role": "user", "content": val[0]})
|
26 |
+
if val[1]:
|
27 |
+
messages.append({"role": "assistant", "content": val[1]})
|
28 |
+
|
29 |
+
messages.append({"role": "user", "content": message})
|
30 |
+
|
31 |
+
response = ""
|
32 |
+
for msg in chat_client.chat_completion(
|
33 |
+
messages,
|
34 |
+
max_tokens=max_tokens,
|
35 |
+
stream=True,
|
36 |
+
temperature=temperature,
|
37 |
+
top_p=top_p,
|
38 |
+
):
|
39 |
+
token = msg.choices[0].delta.content
|
40 |
+
response += token
|
41 |
+
|
42 |
+
# Step 2: Generate speech using TTS
|
43 |
+
speech = tts_synthesizer(response, forward_params={"speaker_embeddings": speaker_embedding})
|
44 |
+
output_file = "generated_speech.wav"
|
45 |
+
sf.write(output_file, speech["audio"], samplerate=speech["sampling_rate"])
|
46 |
+
|
47 |
+
# Update the chat history
|
48 |
+
history.append((message, response))
|
49 |
+
|
50 |
+
# Return both text response, audio file, and updated history
|
51 |
+
return response, output_file, history
|
52 |
+
|
53 |
+
# Create the Gradio interface
|
54 |
+
demo = gr.Interface(
|
55 |
+
fn=chat_with_tts,
|
56 |
+
inputs=[
|
57 |
+
gr.Textbox(label="User Input", placeholder="Type your message..."),
|
58 |
+
gr.State([]), # Initialize history as an empty list
|
59 |
+
gr.Textbox(value="You are a friendly chatbot.", label="System Message"),
|
60 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
|
61 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
62 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
63 |
+
],
|
64 |
+
outputs=[
|
65 |
+
gr.Textbox(label="Generated Response"),
|
66 |
+
gr.Audio(label="Generated Speech"),
|
67 |
+
gr.State(), # Add State as an output to update the history
|
68 |
+
],
|
69 |
+
title="Chat with TTS",
|
70 |
+
description="Enter text to chat with an AI chatbot. The chatbot will generate a response, which will also be converted to speech using TTS."
|
71 |
+
)
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
demo.launch()
|