Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,19 +5,24 @@ from streaming_stt_nemo import Model
|
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
import edge_tts
|
| 7 |
|
|
|
|
| 8 |
default_lang = "en"
|
| 9 |
engines = {default_lang: Model(default_lang)}
|
| 10 |
|
|
|
|
| 11 |
def transcribe(audio):
|
| 12 |
lang = "en"
|
| 13 |
model = engines[lang]
|
| 14 |
text = model.stt_file(audio)[0]
|
| 15 |
return text
|
| 16 |
|
|
|
|
| 17 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 18 |
|
|
|
|
| 19 |
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]"
|
| 20 |
|
|
|
|
| 21 |
@spaces.GPU(duration=120)
|
| 22 |
def model(text):
|
| 23 |
generate_kwargs = dict(
|
|
@@ -30,13 +35,15 @@ def model(text):
|
|
| 30 |
)
|
| 31 |
formatted_prompt = system_instructions + text + "[CrucialCoach]"
|
| 32 |
stream = client.text_generation(
|
| 33 |
-
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False
|
|
|
|
| 34 |
output = ""
|
| 35 |
for response in stream:
|
| 36 |
if not response.token.text == "</s>":
|
| 37 |
output += response.token.text
|
| 38 |
return output
|
| 39 |
|
|
|
|
| 40 |
async def respond(audio):
|
| 41 |
user = transcribe(audio)
|
| 42 |
reply = model(user)
|
|
@@ -46,22 +53,25 @@ async def respond(audio):
|
|
| 46 |
await communicate.save(tmp_path)
|
| 47 |
return tmp_path
|
| 48 |
|
|
|
|
| 49 |
theme = gr.themes.Base()
|
| 50 |
|
| 51 |
-
|
| 52 |
-
with gr.Blocks() as voice:
|
| 53 |
with gr.Row():
|
| 54 |
-
input = gr.Audio(label="Voice Chat",
|
| 55 |
-
output = gr.Audio(label="CrucialCoach", type="filepath",
|
| 56 |
-
interactive=False,
|
| 57 |
-
autoplay=True,
|
| 58 |
-
elem_classes="audio")
|
| 59 |
gr.Interface(
|
| 60 |
-
fn=respond,
|
| 61 |
inputs=[input],
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
|
|
|
| 65 |
gr.TabbedInterface([voice], ['🗣️ Crucial Coach Chat'])
|
|
|
|
|
|
|
| 66 |
demo.queue(max_size=200)
|
| 67 |
-
demo.launch()
|
|
|
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
import edge_tts
|
| 7 |
|
| 8 |
+
# Initialize default language and STT model
|
| 9 |
default_lang = "en"
|
| 10 |
engines = {default_lang: Model(default_lang)}
|
| 11 |
|
| 12 |
+
# Function to transcribe audio to text
|
| 13 |
def transcribe(audio):
|
| 14 |
lang = "en"
|
| 15 |
model = engines[lang]
|
| 16 |
text = model.stt_file(audio)[0]
|
| 17 |
return text
|
| 18 |
|
| 19 |
+
# Initialize Huggingface InferenceClient
|
| 20 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 21 |
|
| 22 |
+
# System instructions for the CrucialCoach
|
| 23 |
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]"
|
| 24 |
|
| 25 |
+
# Decorator for using GPU with a duration of 120 seconds
|
| 26 |
@spaces.GPU(duration=120)
|
| 27 |
def model(text):
|
| 28 |
generate_kwargs = dict(
|
|
|
|
| 35 |
)
|
| 36 |
formatted_prompt = system_instructions + text + "[CrucialCoach]"
|
| 37 |
stream = client.text_generation(
|
| 38 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False
|
| 39 |
+
)
|
| 40 |
output = ""
|
| 41 |
for response in stream:
|
| 42 |
if not response.token.text == "</s>":
|
| 43 |
output += response.token.text
|
| 44 |
return output
|
| 45 |
|
| 46 |
+
# Asynchronous function to handle audio input and provide response
|
| 47 |
async def respond(audio):
|
| 48 |
user = transcribe(audio)
|
| 49 |
reply = model(user)
|
|
|
|
| 53 |
await communicate.save(tmp_path)
|
| 54 |
return tmp_path
|
| 55 |
|
| 56 |
+
# Gradio theme
|
| 57 |
theme = gr.themes.Base()
|
| 58 |
|
| 59 |
+
# Gradio interface for voice chat
|
| 60 |
+
with gr.Blocks() as voice:
|
| 61 |
with gr.Row():
|
| 62 |
+
input = gr.Audio(label="Voice Chat", source="microphone", type="filepath", waveform_options=False)
|
| 63 |
+
output = gr.Audio(label="CrucialCoach", type="filepath", interactive=False, autoplay=True, elem_classes="audio")
|
|
|
|
|
|
|
|
|
|
| 64 |
gr.Interface(
|
| 65 |
+
fn=respond,
|
| 66 |
inputs=[input],
|
| 67 |
+
outputs=[output],
|
| 68 |
+
live=True
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
# Gradio demo setup
|
| 72 |
+
with gr.Blocks(theme=theme, css="footer {visibility: hidden} textbox {resize: none}", title="CrucialCoach DEMO") as demo:
|
| 73 |
gr.TabbedInterface([voice], ['🗣️ Crucial Coach Chat'])
|
| 74 |
+
|
| 75 |
+
# Queue setup and launch
|
| 76 |
demo.queue(max_size=200)
|
| 77 |
+
demo.launch()
|