hwberry2 commited on
Commit
f241696
1 Parent(s): a206d4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -5
app.py CHANGED
@@ -1,11 +1,14 @@
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
  def transcribe(audio):
10
  audio_file = open(audio, "rb")
11
  # Call the transcribe method with the file-like object
@@ -15,20 +18,33 @@ def transcribe(audio):
15
 
16
 
17
 
18
-
19
  with gr.Blocks() as demo:
 
 
 
20
  messages = gr.State(value=[{"role": "system", "content": "You are a therapist. Respond in less than 5 sentences."}])
21
 
 
 
 
22
  def botResponse(user_input, messages):
 
 
23
  messages.append({"role": "user", "content": user_input})
24
  response = openai.ChatCompletion.create(
25
  model="gpt-3.5-turbo-0301",
26
  messages=messages
27
  )
28
-
 
 
29
  system_message = response["choices"][0]["message"]["content"]
30
  messages.append({"role": "assistant", "content": system_message})
31
-
 
 
 
32
  chat_transcript = ""
33
  for message in messages:
34
  if (message["role"] != "system"):
@@ -36,6 +52,11 @@ with gr.Blocks() as demo:
36
 
37
  return chat_transcript
38
 
 
 
 
 
 
39
  def giveVoice(messages):
40
  bot_message=messages[-1]
41
 
@@ -47,6 +68,10 @@ with gr.Blocks() as demo:
47
 
48
  return new_path
49
 
 
 
 
 
50
  with gr.Row():
51
  with gr.Column(scale=1):
52
  user_audio = gr.Audio(source="microphone", type="filepath", label="Input Phrase")
 
1
  import os
2
  import gradio as gr
3
  import openai
4
+ from gtts import gTTS # Google Text To Speech
 
5
 
6
+ # load the api key
7
  openai.api_key = os.environ["OPEN_AI_KEY"]
8
 
9
+ # takes an audio file from the microphone
10
+ # submits the raw audio to OpenAI for
11
+ # Speech to Text Translation
12
  def transcribe(audio):
13
  audio_file = open(audio, "rb")
14
  # Call the transcribe method with the file-like object
 
18
 
19
 
20
 
21
+ # Create a Gradio App using Blocks
22
  with gr.Blocks() as demo:
23
+ # First message as instructions to OpenAI
24
+ # Establishes a State object to create a
25
+ # unique state for each user and on reload
26
  messages = gr.State(value=[{"role": "system", "content": "You are a therapist. Respond in less than 5 sentences."}])
27
 
28
+ # Takes the users transcribed audio as a string
29
+ # Takes the messages list as a reference
30
+ # Sends the ongoing chat log to OpenAI
31
  def botResponse(user_input, messages):
32
+ # adds the user input to the ongoing chat log
33
+ # and submits the log to OpenAI
34
  messages.append({"role": "user", "content": user_input})
35
  response = openai.ChatCompletion.create(
36
  model="gpt-3.5-turbo-0301",
37
  messages=messages
38
  )
39
+
40
+ # Parse the response from OpenAI and store
41
+ # it in the chat log
42
  system_message = response["choices"][0]["message"]["content"]
43
  messages.append({"role": "assistant", "content": system_message})
44
+
45
+ # Process the messages list to get the
46
+ # chat log into a string. Exclude the
47
+ # System responses from the string
48
  chat_transcript = ""
49
  for message in messages:
50
  if (message["role"] != "system"):
 
52
 
53
  return chat_transcript
54
 
55
+ # Gets the last message in the
56
+ # chat log and uses GTTS to
57
+ # convert the last response into
58
+ # an audio file. Returns a path to
59
+ # the converted text as an mp3 file
60
  def giveVoice(messages):
61
  bot_message=messages[-1]
62
 
 
68
 
69
  return new_path
70
 
71
+ # Creates the Gradio interface objects
72
+ # The submit button triggers a cascade of
73
+ # events that each engage a different
74
+ # component as input/output
75
  with gr.Row():
76
  with gr.Column(scale=1):
77
  user_audio = gr.Audio(source="microphone", type="filepath", label="Input Phrase")