Sharmitha's picture
Update app.py
d6ad09f verified
raw
history blame
No virus
1.59 kB
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)