File size: 1,437 Bytes
c33ad65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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()