File size: 1,590 Bytes
7e0b7da
 
 
 
 
d6ad09f
 
 
7e0b7da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
from gtts import gTTS
import pyttsx3
from IPython.display import Audio
import gradio as gr
import os
SECRET_TOKEN = os.getenv("api_key")
openai.api_key = SECRET_TOKEN

def translate_audio(audio_file):
    file1 = open(audio_file, "rb")
    text = openai.Audio.translate(
        model="whisper-1",
        file=file1
    )
    return text["text"]

def text_response(t):
    messages = [{"role": "system", "content": "You are a therapist, you are supposed to answer questions related to stress, mental health. For the rest of the questions, respond you don't know. Respond to all input in 100 words or less."}]
    messages.append({"role": "user", "content": t})
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages
    )
    message = response["choices"][0]["message"]["content"]
    return message

from gtts import gTTS

def audio_response(t):
    tts = gTTS(text=t, lang='en', slow=False)
    tts.save("output.mp3")
    mp3_file_path = "output.mp3"
    return mp3_file_path

from IPython.display import Audio

def transcribe(a):
    t1 = translate_audio(a)
    t2 = text_response(t1)
    t3 = audio_response(t2)
    return (t1, t2, t3)

import gradio as gr

output_1 = gr.Textbox(label="Speech to Text")
output_2 = gr.Textbox(label="ChatGPT Output")
output_3 = gr.Audio(label="ChatGPT output to audio")

gr.Interface(
    title='AI Voice Assistant',
    fn=transcribe,
    inputs=[
        gr.Audio(sources="microphone", type="filepath"),
    ],
    outputs=[
        output_1, output_2, output_3
    ]
).launch(share=True)