Futuresony commited on
Commit
d8b7ad8
Β·
verified Β·
1 Parent(s): 184b47e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -26
app.py CHANGED
@@ -1,45 +1,82 @@
1
  import gradio as gr
2
- from asr import transcribe_auto # Import ASR function
3
- from ttsmms import download, TTS
4
- from langdetect import detect
 
 
 
5
 
6
- # Download and load TTS models for Swahili and English
7
- swahili_dir = download("swh", "./data/swahili")
8
- english_dir = download("eng", "./data/english")
9
 
10
- swahili_tts = TTS(swahili_dir)
11
- english_tts = TTS(english_dir)
12
 
13
- # Function to handle ASR β†’ TTS
14
- def asr_to_tts(audio):
15
- # Step 1: Transcribe Speech
16
- transcribed_text = transcribe_auto(audio)
17
 
18
- # Step 2: Detect Language & Generate Speech
19
- lang = detect(transcribed_text)
20
- wav_path = "./output.wav"
21
 
22
- if lang == "sw": # Swahili
23
- swahili_tts.synthesis(transcribed_text, wav_path=wav_path)
24
- else: # Default to English
25
- english_tts.synthesis(transcribed_text, wav_path=wav_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- return transcribed_text, wav_path # Return both text & generated speech
 
 
 
 
 
 
 
 
 
28
 
29
  # Gradio Interface
30
  with gr.Blocks() as demo:
31
- gr.Markdown("<h2 style='text-align: center;'>Multilingual Speech-to-Text & Text-to-Speech</h2>")
32
 
33
  with gr.Row():
34
  audio_input = gr.Audio(source="microphone", type="filepath", label="🎀 Speak Here")
35
- text_output = gr.Textbox(label="πŸ“ Transcription", interactive=False)
36
- audio_output = gr.Audio(label="πŸ”Š Generated Speech")
 
 
 
37
 
38
- submit_button = gr.Button("Transcribe & Speak πŸ”„")
 
 
 
 
39
 
40
- submit_button.click(fn=asr_to_tts, inputs=[audio_input], outputs=[text_output, audio_output])
 
 
 
 
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()