Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
import soundfile as sf
|
4 |
from transformers import pipeline
|
5 |
-
import torch
|
6 |
from datasets import load_dataset
|
7 |
-
|
8 |
-
|
9 |
-
client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
10 |
|
11 |
# Initialize the TTS pipeline from Huggingface
|
12 |
synthesizer = pipeline("text-to-speech", model="Futuresony/output")
|
@@ -15,61 +11,25 @@ synthesizer = pipeline("text-to-speech", model="Futuresony/output")
|
|
15 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
16 |
speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
17 |
|
18 |
-
def
|
19 |
-
message,
|
20 |
-
history: list[tuple[str, str]],
|
21 |
-
system_message,
|
22 |
-
max_tokens,
|
23 |
-
temperature,
|
24 |
-
top_p,
|
25 |
-
):
|
26 |
-
# Prepare the messages for the chatbot
|
27 |
-
messages = [{"role": "system", "content": system_message}]
|
28 |
-
|
29 |
-
# Add history of previous conversation
|
30 |
-
for val in history:
|
31 |
-
if val[0]:
|
32 |
-
messages.append({"role": "user", "content": val[0]})
|
33 |
-
if val[1]:
|
34 |
-
messages.append({"role": "assistant", "content": val[1]})
|
35 |
-
|
36 |
-
messages.append({"role": "user", "content": message})
|
37 |
-
|
38 |
-
response = ""
|
39 |
-
|
40 |
-
# Generate the response from the model
|
41 |
-
for message in client.chat_completion(
|
42 |
-
messages,
|
43 |
-
max_tokens=max_tokens,
|
44 |
-
stream=True,
|
45 |
-
temperature=temperature,
|
46 |
-
top_p=top_p,
|
47 |
-
):
|
48 |
-
token = message.choices[0].delta.content
|
49 |
-
response += token
|
50 |
-
yield response
|
51 |
-
|
52 |
# Convert the generated text to speech
|
53 |
-
speech = synthesizer(
|
54 |
|
55 |
# Save the generated speech to a file
|
56 |
-
|
|
|
57 |
|
58 |
-
# Return
|
59 |
-
return
|
60 |
|
61 |
-
# Create the Gradio interface
|
62 |
demo = gr.Interface(
|
63 |
-
fn=
|
64 |
-
inputs=
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
69 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
70 |
-
],
|
71 |
-
outputs=[gr.Textbox(label="Generated Response"), gr.Audio(label="Generated Speech")],
|
72 |
)
|
73 |
|
74 |
if __name__ == "__main__":
|
75 |
-
demo
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
from transformers import pipeline
|
|
|
3 |
from datasets import load_dataset
|
4 |
+
import soundfile as sf
|
5 |
+
import torch
|
|
|
6 |
|
7 |
# Initialize the TTS pipeline from Huggingface
|
8 |
synthesizer = pipeline("text-to-speech", model="Futuresony/output")
|
|
|
11 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
12 |
speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
13 |
|
14 |
+
def text_to_speech(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Convert the generated text to speech
|
16 |
+
speech = synthesizer(text, forward_params={"speaker_embeddings": speaker_embedding})
|
17 |
|
18 |
# Save the generated speech to a file
|
19 |
+
output_file = "generated_speech.wav"
|
20 |
+
sf.write(output_file, speech["audio"], samplerate=speech["sampling_rate"])
|
21 |
|
22 |
+
# Return the path to the audio file for playback
|
23 |
+
return output_file
|
24 |
|
25 |
+
# Create the Gradio interface
|
26 |
demo = gr.Interface(
|
27 |
+
fn=text_to_speech,
|
28 |
+
inputs=gr.Textbox(label="Enter Text", placeholder="Type something..."),
|
29 |
+
outputs=gr.Audio(label="Generated Speech"),
|
30 |
+
title="Text-to-Speech Generator",
|
31 |
+
description="Enter text and generate speech using a pre-trained TTS model."
|
|
|
|
|
|
|
|
|
32 |
)
|
33 |
|
34 |
if __name__ == "__main__":
|
35 |
+
demo
|