voice-assistant / main.py
heliosbrahma's picture
first commit
c33ad65
raw
history blame
1.44 kB
import whisper
import gradio as gr
import openai
from TTS.api import TTS
import subprocess
model_name = TTS.list_models()[9]
tts = TTS(model_name)
model = whisper.load_model('medium')
def run_ffmpeg_command():
command = ['ffmpeg', '-f', 'lavfi', '-i', 'anullsrc=r=44100:cl=mono', '-t', '1', '-q:a', '9', '-acodec', 'libmp3lame', 'output.wav']
result = subprocess.run(command, capture_output=True, text=True)
print(result.stdout)
def voice_chat(api_key, user_voice):
openai.api_key = str(api_key)
messages = [
{"role": "system", "content": "You are a kind helpful assistant."},
]
user_message = model.transcribe(user_voice)["text"]
messages.append(
{"role": "user", "content": user_message},
)
chat = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages
)
reply = chat.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
tts.tts_to_file(text=reply, file_path="output.wav")
return(reply, "output.wav")
# run_ffmpeg_command()
text_reply = gr.Textbox(label="Summarized Answer")
voice_reply = gr.Audio(type="filepath")
gr.Interface(
title = 'Smart Voice Assistant',
fn=voice_chat,
inputs=[
gr.Textbox(label="OpenAI API Key"),
gr.Audio(source="microphone", type="filepath")
],
outputs=[
text_reply, voice_reply
]).launch()