Spaces:
Sleeping
Sleeping
import spaces | |
import tempfile | |
import gradio as gr | |
from streaming_stt_nemo import Model | |
from huggingface_hub import InferenceClient | |
import edge_tts | |
default_lang = "en" | |
engines = {default_lang: Model(default_lang)} | |
def transcribe(audio): | |
lang = "en" | |
model = engines[lang] | |
text = model.stt_file(audio)[0] | |
return text | |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1") | |
system_instructions = "[SYSTEM] You are CrucialCoach, an AI-powered conversational coach. Guide the user through challenging workplace situations using the principles from 'Crucial Conversations'. Ask one question at a time and provide step-by-step guidance.\n\n[USER]" | |
def model(text): | |
generate_kwargs = dict( | |
temperature=0.7, | |
max_new_tokens=512, | |
top_p=0.95, | |
repetition_penalty=1, | |
do_sample=True, | |
seed=42, | |
) | |
formatted_prompt = system_instructions + text + "[CrucialCoach]" | |
stream = client.text_generation( | |
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False) | |
output = "" | |
for response in stream: | |
if not response.token.text == "</s>": | |
output += response.token.text | |
return output | |
async def respond(audio): | |
user = transcribe(audio) | |
reply = model(user) | |
communicate = edge_tts.Communicate(reply) | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file: | |
tmp_path = tmp_file.name | |
await communicate.save(tmp_path) | |
return tmp_path | |
theme = gr.themes.Base() | |
with gr.Blocks() as voice: | |
with gr.Row(): | |
input = gr.Audio(label="Voice Chat", sources="microphone", type="filepath", waveform_options=False) | |
output = gr.Audio(label="CrucialCoach", type="filepath", | |
interactive=False, | |
autoplay=True, | |
elem_classes="audio") | |
gr.Interface( | |
fn=respond, | |
inputs=[input], | |
outputs=[output], live=True) | |
with gr.Blocks(theme=theme, css="footer {visibility: hidden}textbox{resize:none}", title="CrucialCoach DEMO") as demo: | |
gr.TabbedInterface([voice], ['🗣️ Crucial Coach Chat']) | |
demo.queue(max_size=200) | |
demo.launch() |