Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,82 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
from
|
4 |
-
from
|
|
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
english_dir = download("eng", "./data/english")
|
9 |
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
transcribed_text = transcribe_auto(audio)
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
# Gradio Interface
|
30 |
with gr.Blocks() as demo:
|
31 |
-
gr.Markdown("<h2 style='text-align: center;'>
|
32 |
|
33 |
with gr.Row():
|
34 |
audio_input = gr.Audio(source="microphone", type="filepath", label="π€ Speak Here")
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
submit_button.click(
|
|
|
|
|
|
|
|
|
41 |
|
42 |
# Run the App
|
43 |
if __name__ == "__main__":
|
44 |
demo.launch()
|
45 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
from transformers import pipeline
|
4 |
+
from datasets import load_dataset
|
5 |
+
import soundfile as sf
|
6 |
+
import torch
|
7 |
+
from asr import transcribe_auto # ASR function
|
8 |
|
9 |
+
# Initialize Chat Model
|
10 |
+
chat_client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
|
|
|
11 |
|
12 |
+
# Initialize Facebook TTS Model
|
13 |
+
tts_synthesizer = pipeline("text-to-speech", model="Futuresony/Output")
|
14 |
|
15 |
+
# Load Speaker Embeddings for TTS
|
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 speech_to_chat(audio, history, system_message, max_tokens, temperature, top_p):
|
20 |
+
# Step 1: Transcribe Speech to Text
|
21 |
+
transcribed_text = transcribe_auto(audio)
|
22 |
|
23 |
+
# Step 2: Generate Chat Response
|
24 |
+
messages = [{"role": "system", "content": system_message}]
|
25 |
+
|
26 |
+
for val in history:
|
27 |
+
if val[0]:
|
28 |
+
messages.append({"role": "user", "content": val[0]})
|
29 |
+
if val[1]:
|
30 |
+
messages.append({"role": "assistant", "content": val[1]})
|
31 |
+
|
32 |
+
messages.append({"role": "user", "content": transcribed_text})
|
33 |
+
|
34 |
+
response = ""
|
35 |
+
for msg in chat_client.chat_completion(
|
36 |
+
messages,
|
37 |
+
max_tokens=max_tokens,
|
38 |
+
stream=True,
|
39 |
+
temperature=temperature,
|
40 |
+
top_p=top_p,
|
41 |
+
):
|
42 |
+
token = msg.choices[0].delta.content
|
43 |
+
response += token
|
44 |
|
45 |
+
# Step 3: Convert Chat Response to Speech
|
46 |
+
speech = tts_synthesizer(response, forward_params={"speaker_embeddings": speaker_embedding})
|
47 |
+
output_file = "generated_speech.wav"
|
48 |
+
sf.write(output_file, speech["audio"], samplerate=speech["sampling_rate"])
|
49 |
+
|
50 |
+
# Update Chat History
|
51 |
+
history.append((transcribed_text, response))
|
52 |
+
|
53 |
+
# Return transcribed text, chatbot response, generated speech, and updated history
|
54 |
+
return transcribed_text, response, output_file, history
|
55 |
|
56 |
# Gradio Interface
|
57 |
with gr.Blocks() as demo:
|
58 |
+
gr.Markdown("<h2 style='text-align: center;'>Real-time ASR β Chat β TTS</h2>")
|
59 |
|
60 |
with gr.Row():
|
61 |
audio_input = gr.Audio(source="microphone", type="filepath", label="π€ Speak Here")
|
62 |
+
transcribed_text_output = gr.Textbox(label="π Transcribed Text", interactive=False)
|
63 |
+
chat_response_output = gr.Textbox(label="π€ AI Response", interactive=False)
|
64 |
+
audio_output = gr.Audio(label="π AI Speech Output")
|
65 |
+
|
66 |
+
submit_button = gr.Button("ποΈ Speak & Generate Response")
|
67 |
|
68 |
+
system_msg = gr.Textbox(value="You are a friendly chatbot.", label="System Message")
|
69 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens")
|
70 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
71 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
72 |
+
chat_history = gr.State([]) # Store conversation history
|
73 |
|
74 |
+
submit_button.click(
|
75 |
+
fn=speech_to_chat,
|
76 |
+
inputs=[audio_input, chat_history, system_msg, max_tokens, temperature, top_p],
|
77 |
+
outputs=[transcribed_text_output, chat_response_output, audio_output, chat_history],
|
78 |
+
)
|
79 |
|
80 |
# Run the App
|
81 |
if __name__ == "__main__":
|
82 |
demo.launch()
|
|