File size: 2,053 Bytes
b3b0738
 
 
 
 
 
 
 
24f06d9
a110eb7
 
 
 
 
b3b0738
a35346c
24f06d9
 
 
 
 
 
a110eb7
 
e27db14
a110eb7
 
 
 
 
 
 
 
 
 
 
 
b3b0738
7ee2f4c
 
a35346c
7ee2f4c
a35346c
 
 
 
 
 
 
b3b0738
092aeaf
b3b0738
50f2112
a206d4b
092aeaf
1ef1929
854d54e
a35346c
b7cf674
7ee2f4c
b3b0738
 
 
99c65a9
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
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import gradio as gr
import openai
from gtts import gTTS


openai.api_key = os.environ["OPEN_AI_KEY"]

def transcribe(audio):   
        audio_file = open(audio, "rb")    
        # Call the transcribe method with the file-like object
        transcript = openai.Audio.transcribe("whisper-1", audio_file)
        
        return transcript["text"]



    
with gr.Blocks() as demo:
    messages = gr.State(value=[{"role": "system", "content": "You are a therapist. Respond in less than 5 sentences."}])

    def botResponse(user_input, messages):
        messages.append({"role": "user", "content": user_input})
        response = openai.ChatCompletion.create(
          model="gpt-3.5-turbo-0301",
          messages=messages
        )
      
        system_message = response["choices"][0]["message"]["content"]
        messages.append({"role": "assistant", "content": system_message})
    
        chat_transcript = ""
        for message in messages:
            if (message["role"] != "system"):
                chat_transcript += message["role"] + ": " + message["content"] + "\n\n"
    
        return chat_transcript

    def giveVoice(messages):
        bot_message=messages[-1]
        
        myobj = gTTS(text=bot_message["content"])
        myobj.save("temp.mp3")
    
        dir = os.getcwd()
        new_path = os.path.join(dir, "temp.mp3")
    
        return new_path

    with gr.Row():
        with gr.Column(scale=1):
            user_audio = gr.Audio(source="microphone", type="filepath", label="Input Phrase")
            submit_btn = gr.Button(value="Transcribe")
            gpt_voice = gr.Audio(label="Voice Response")
        with gr.Column(scale=2):
            user_transcript = gr.Text(label="User Transcript")
            gpt_transcript = gr.Text(label="Chat Transcript")
    submit_btn.click(transcribe, user_audio, user_transcript)
    user_transcript.change(botResponse, [user_transcript, messages], gpt_transcript)
    gpt_transcript.change(giveVoice, messages, gpt_voice)
    
    

demo.launch(share=False)