File size: 2,271 Bytes
b3b0738
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96f6f8c
 
 
b3b0738
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96f6f8c
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import gradio as gr
import openai
from gtts import gTTS


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



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


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


    #msg_contents.append({"role": "user", "content": transcript["text"]})

    #chat_transcript = ""
    #for message in msg_contents:
    #    if (message["role"] != "system"):
    #        chat_transcript += message["role"] + ": " + message["content"] + "\n\n"

    return transcript

def botResponse(chat_log, msg_contents):    
    response = openai.ChatCompletion.create(
      model="gpt-3.5-turbo",
      messages=msg_contents)
  
    system_message = response["choices"][0]["message"]["content"]
    msg_contents.append({"role": "assistant", "content": system_message})

    chat_transcript = chat_log
    for message in msg_contents:
        if (message["role"] != "system"):
            chat_transcript += message["role"] + ": " + message["content"] + "\n\n"

    return system_message


def giveVoice(bot_message):
    myobj = gTTS(text=bot_message)
    myobj.save("temp.mp3")

    dir = os.getcwd()
    new_path = os.path.join(dir, "temp.mp3")

    return new_path






with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            user_audio = gr.Audio(source="microphone", type="filepath", label="Input Phrase")
            submit_btn = gr.Button(value="Transcribe") 
            submit2_btn = gr.Button(value="Bot Response")
            submit3_btn = gr.Button(value="Give Voice")
        with gr.Column():
            #gpt_response = gr.Audio(label="Voice Response")
            gpt_transcript = gr.Text(label="Generate Transcript")
            gpt_transcript2 = gr.Text(label="Bot Response")
            gpt_response = gr.Audio(label="Voice Response")
    submit_btn.click(transcribe, inputs=[user_audio, "test"] outputs=gpt_transcript)
    
    

demo.launch(share=False)