Roland Ding commited on
Commit
d415e7e
·
1 Parent(s): d456202

0.0.2.2 double check.

Browse files
Files changed (4) hide show
  1. app.py +21 -12
  2. defaults.py +0 -0
  3. requirements.txt +2 -0
  4. supplier.py +45 -3
app.py CHANGED
@@ -3,16 +3,17 @@ from supplier import *
3
 
4
  background_examples=[
5
  "This is a friendly chatbot with humour.", # casual setting
6
- "This is the first date between virtual human and real human.", # romantic setting
7
  "We are at the mideval castle, I am talking to my chatbot donkey.", # fantasy setting
8
  ]
9
 
 
10
  with gr.Blocks() as settings:
11
  with gr.Row():
12
- background = gr.TextArea(sys_msg[0]["content"],lines=5,label="Current background")
13
  with gr.Column():
14
  def update_sys(text):
15
- sys_msg[0].update({"content":text})
16
  return text
17
 
18
  background_edit = gr.Textbox(placeholder="change the background here",label="update background",lines = 5)
@@ -24,31 +25,39 @@ with gr.Blocks() as settings:
24
  gr.Examples(background_examples,background_edit,label="Examples")
25
  clear.click(lambda:None,None,background_edit,queue=False)
26
  submit.click(update_sys,background_edit,background,queue=False)
 
 
 
 
27
 
28
  with gr.Blocks() as chat_window:
29
  with gr.Row():
30
  with gr.Column():
31
- chatbot = gr.Chatbot(initial_msg)
32
- clear = gr.Button("Clear")
33
-
 
 
 
 
 
34
  with gr.Column():
35
  msg = gr.Textbox()
36
  submit = gr.Button("Submit")
37
  gr.Examples(["Hello","How are you?"],msg,label="Examples")
38
-
39
  audio = gr.Audio(source="microphone",type="filepath")
40
  # gr.Interface(translate,inputs=gr.Audio(source="microphone",type="filepath"),outputs = "text")
41
 
42
- audio.change(translate,audio,msg,queue=False)
43
- msg.submit(send_chat,[msg,chatbot],[msg,chatbot])
44
- submit.click(send_chat,[msg,chatbot],[msg,chatbot],queue=False)
 
45
 
46
- clear.click(lambda:None,None,chatbot,queue=False)
47
 
48
  iface = gr.TabbedInterface(
49
  [chat_window,settings],
50
  ["Chat","Settings"],
51
- title="ChatGPT 3.5",
52
  css="footer {visibility: hidden}")
53
 
54
  if __name__ == "__main__":
 
3
 
4
  background_examples=[
5
  "This is a friendly chatbot with humour.", # casual setting
6
+ "This is a friendly chatbot which respond to user in Chinese.", # romantic setting
7
  "We are at the mideval castle, I am talking to my chatbot donkey.", # fantasy setting
8
  ]
9
 
10
+ # create a system setting for llm
11
  with gr.Blocks() as settings:
12
  with gr.Row():
13
+ background = gr.TextArea(App_state["sys_msg"][0]["content"],lines=5,label="Current background")
14
  with gr.Column():
15
  def update_sys(text):
16
+ App_state["sys_msg"][0].update({"content":text})
17
  return text
18
 
19
  background_edit = gr.Textbox(placeholder="change the background here",label="update background",lines = 5)
 
25
  gr.Examples(background_examples,background_edit,label="Examples")
26
  clear.click(lambda:None,None,background_edit,queue=False)
27
  submit.click(update_sys,background_edit,background,queue=False)
28
+ with gr.Row():
29
+ voices_list = gr.Dropdown([v["Name"] for v in get_voices()],label="Voices")
30
+
31
+ voices_list.change(lambda voice:App_state.update({"voice":voice}),voices_list,queue=False)
32
 
33
  with gr.Blocks() as chat_window:
34
  with gr.Row():
35
  with gr.Column():
36
+ chatbot = gr.Chatbot([])
37
+ chatbot_speech = gr.Audio()
38
+ with gr.Column():
39
+ chat_clear = gr.Button("Clear")
40
+ play_speech = gr.Button("Play")
41
+ chat_clear.click(lambda:None,None,chatbot,queue=False)
42
+ play_speech.click(text_to_audio,chatbot,chatbot_speech,queue=False)
43
+
44
  with gr.Column():
45
  msg = gr.Textbox()
46
  submit = gr.Button("Submit")
47
  gr.Examples(["Hello","How are you?"],msg,label="Examples")
 
48
  audio = gr.Audio(source="microphone",type="filepath")
49
  # gr.Interface(translate,inputs=gr.Audio(source="microphone",type="filepath"),outputs = "text")
50
 
51
+ audio.change(translate,audio,msg,queue=False)
52
+ msg.submit(send_chat,[msg,chatbot],[msg,chatbot])
53
+ submit.click(send_chat,[msg,chatbot],[msg,chatbot],queue=False)
54
+
55
 
 
56
 
57
  iface = gr.TabbedInterface(
58
  [chat_window,settings],
59
  ["Chat","Settings"],
60
+ title="MiMinions.ai",
61
  css="footer {visibility: hidden}")
62
 
63
  if __name__ == "__main__":
defaults.py ADDED
File without changes
requirements.txt CHANGED
@@ -1,2 +1,4 @@
1
  openai
2
  gradio
 
 
 
1
  openai
2
  gradio
3
+ # langchain
4
+ # autoGPT
supplier.py CHANGED
@@ -1,15 +1,56 @@
1
  import openai
 
2
  import os
3
 
 
 
 
 
4
  openai.api_key = os.environ.get("OPENAI_API_KEY")
 
 
5
 
6
  sys_msg = [{
7
  "role": "system",
8
  "content": "Hi, I'm a chatbot! Ask me a question."
9
  }]
10
- initial_msg = []
11
 
12
- def send_chat(text,messages=initial_msg):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  '''
14
  send_chat function takes two arguments: text and messages
15
 
@@ -19,7 +60,7 @@ def send_chat(text,messages=initial_msg):
19
  return: a tuple, the first element is an empty string, the second element is the updated messages list
20
 
21
  '''
22
- openai_messages = sys_msg.copy()
23
 
24
  for m in messages:
25
  openai_messages.extend([
@@ -48,6 +89,7 @@ def send_chat(text,messages=initial_msg):
48
  assistant_text = res.choices[0]["message"]["content"]
49
 
50
  messages.append((text,assistant_text))
 
51
 
52
  return "",messages
53
 
 
1
  import openai
2
+ import boto3
3
  import os
4
 
5
+ import pydub
6
+ import numpy as np
7
+
8
+ # setup the api keys
9
  openai.api_key = os.environ.get("OPENAI_API_KEY")
10
+ aws_access_key_id = os.environ.get("AWS_ACCESS_KEY_ID")
11
+ aws_secret_access_key = os.environ.get("AWS_SECRET_ACCESS_KEY")
12
 
13
  sys_msg = [{
14
  "role": "system",
15
  "content": "Hi, I'm a chatbot! Ask me a question."
16
  }]
 
17
 
18
+ App_state = {
19
+ "messages":[],
20
+ "sys_msg":[{
21
+ "role": "system",
22
+ "content": "Hi, I'm a chatbot! Ask me a question."
23
+ }],
24
+ "language":"en",
25
+ # "last_msg":"",
26
+ "voice":"Joanna",
27
+ }
28
+
29
+ # setup the polly client
30
+ polly_client = boto3.client(
31
+ "polly", # the service we want to use
32
+ aws_access_key_id=aws_access_key_id,
33
+ aws_secret_access_key=aws_secret_access_key,
34
+ region_name='us-east-1')
35
+
36
+ def get_voices():
37
+ return polly_client.describe_voices()["Voices"]
38
+
39
+ # return the audio based on the text
40
+ def text_to_audio(messages,voice="Joanna",sample_rate = 22050):
41
+ text = messages[-1][1]
42
+ response = polly_client.synthesize_speech(VoiceId=voice,
43
+ OutputFormat='mp3',
44
+ SampleRate=str(sample_rate),
45
+ Text = text)
46
+
47
+ buffer = response["AudioStream"].read()
48
+ audio_stream = np.frombuffer(buffer,dtype=np.uint16)
49
+
50
+
51
+ return sample_rate,audio_stream
52
+
53
+ def send_chat(text,messages=[]):
54
  '''
55
  send_chat function takes two arguments: text and messages
56
 
 
60
  return: a tuple, the first element is an empty string, the second element is the updated messages list
61
 
62
  '''
63
+ openai_messages = App_state["sys_msg"]
64
 
65
  for m in messages:
66
  openai_messages.extend([
 
89
  assistant_text = res.choices[0]["message"]["content"]
90
 
91
  messages.append((text,assistant_text))
92
+ App_state.update({"messages":messages})
93
 
94
  return "",messages
95