Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import openai
|
4 |
+
from gtts import gTTS
|
5 |
+
|
6 |
+
|
7 |
+
openai.api_key = os.environ["OPEN_AI_KEY"]
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
#messages = gr.State([
|
12 |
+
# {"role": "system", "content": "You are a therapist. Respond in less than 5 sentences."}
|
13 |
+
#])
|
14 |
+
messages = [{"role": "system", "content": "You are a therapist. Respond in less than 5 sentences."}]
|
15 |
+
|
16 |
+
|
17 |
+
def transcribe(audio):
|
18 |
+
|
19 |
+
audio_file = open(audio, "rb")
|
20 |
+
# Call the transcribe method with the file-like object
|
21 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
22 |
+
|
23 |
+
|
24 |
+
#msg_contents.append({"role": "user", "content": transcript["text"]})
|
25 |
+
|
26 |
+
#chat_transcript = ""
|
27 |
+
#for message in msg_contents:
|
28 |
+
# if (message["role"] != "system"):
|
29 |
+
# chat_transcript += message["role"] + ": " + message["content"] + "\n\n"
|
30 |
+
|
31 |
+
return transcript
|
32 |
+
|
33 |
+
def botResponse(chat_log, msg_contents):
|
34 |
+
response = openai.ChatCompletion.create(
|
35 |
+
model="gpt-3.5-turbo",
|
36 |
+
messages=msg_contents)
|
37 |
+
|
38 |
+
system_message = response["choices"][0]["message"]["content"]
|
39 |
+
msg_contents.append({"role": "assistant", "content": system_message})
|
40 |
+
|
41 |
+
chat_transcript = chat_log
|
42 |
+
for message in msg_contents:
|
43 |
+
if (message["role"] != "system"):
|
44 |
+
chat_transcript += message["role"] + ": " + message["content"] + "\n\n"
|
45 |
+
|
46 |
+
return system_message
|
47 |
+
|
48 |
+
|
49 |
+
def giveVoice(bot_message):
|
50 |
+
myobj = gTTS(text=bot_message)
|
51 |
+
myobj.save("temp.mp3")
|
52 |
+
|
53 |
+
dir = os.getcwd()
|
54 |
+
new_path = os.path.join(dir, "temp.mp3")
|
55 |
+
|
56 |
+
return new_path
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
with gr.Blocks() as demo:
|
64 |
+
with gr.Row():
|
65 |
+
with gr.Column():
|
66 |
+
user_audio = gr.Audio(source="microphone", type="filepath", label="Input Phrase")
|
67 |
+
submit_btn = gr.Button(value="Transcribe")
|
68 |
+
submit2_btn = gr.Button(value="Bot Response")
|
69 |
+
submit3_btn = gr.Button(value="Give Voice")
|
70 |
+
with gr.Column():
|
71 |
+
#gpt_response = gr.Audio(label="Voice Response")
|
72 |
+
gpt_transcript = gr.Text(label="Generate Transcript")
|
73 |
+
gpt_transcript2 = gr.Text(label="Bot Response")
|
74 |
+
gpt_response = gr.Audio(label="Voice Response")
|
75 |
+
submit_btn.click(transcribe, inputs=user_audio, outputs=gpt_transcript)
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
demo.launch(share=True)
|