Futuresony commited on
Commit
b838660
·
verified ·
1 Parent(s): efc1876

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -55
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
- # Initialize the client for the text generation model
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 respond(
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(response, forward_params={"speaker_embeddings": speaker_embedding})
54
 
55
  # Save the generated speech to a file
56
- sf.write("generated_speech.wav", speech["audio"], samplerate=speech["sampling_rate"])
 
57
 
58
- # Return both the text and the audio for playback
59
- return response, "generated_speech.wav"
60
 
61
- # Create the Gradio interface with a textbox for the user to input a message
62
  demo = gr.Interface(
63
- fn=respond,
64
- inputs=[
65
- gr.Textbox(label="Type your message here", placeholder="Enter message...", lines=2),
66
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
67
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
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.launch()
 
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