AjithKSenthil commited on
Commit
2971e95
1 Parent(s): 8b100e3

Made it so conversation history is displayed

Browse files
Files changed (1) hide show
  1. chatbot.py +99 -56
chatbot.py CHANGED
@@ -1,76 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import openai
2
  import gradio as gr
3
  import config
4
 
5
-
6
-
7
  openai.api_key = config.OPENAI_API_KEY
8
 
9
- messages = [
10
  {"role": "system", "content":"You are an attachment and personality surveyor"},
11
  {"role": "user", "content":"""ask me each question from this questionnaire and rewrite it as an open ended question and wait for each response. Empathize with me and regularly ask for clarification why I answered with a certain response. Here is the questionnaire:
12
- What is your romantic partner's name?
13
- How did you two meet?
14
- When did you know you wanted to be with this person?
15
- Can you describe your relationship for me?
16
- How serious is your relationship?
17
- Have you thought about having kids with your romantic partner?
18
- What kind of parent do you want to be?
19
- If they didn't elaborate on the reason why, ask them "Why"?
20
- How did your relationship with your mother and father impact your plans to have children?
21
- What was your relationship with your mother like as a kid?
22
- How has your relationship with your mother changed since you were a kid?
23
- What was your relaitonship with your father like as a kid?
24
- How has your relationship with your mother like as a kid?
25
- After asking these quesitons, you can bid them farewell and thank them for their time. Show a lot of gratitude here.
26
- """}
27
- ]
28
 
29
-
30
- def convert_audio_to_text(audio):
31
- audio_file = open(audio, "rb")
32
- transcript = openai.Audio.transcribe("whisper-1", audio_file)
33
- return transcript
34
 
35
  def chatbot(input):
36
- # if input_type == "audio":
37
- # input = convert_audio_to_text(input)
 
38
  if input:
39
- messages.append({"role": "user", "content": input})
40
  chat = openai.ChatCompletion.create(
41
- model="gpt-3.5-turbo", messages=messages
42
  )
43
  reply = chat.choices[0].message.content
44
- messages.append({"role": "assistant", "content": reply})
45
- return reply
46
-
47
-
48
- # Define the Gradio interfaces
49
- # text_interface = gr.Interface(fn=chatbot, inputs=gr.inputs.Textbox("Enter your message here"), outputs="text", title="Text Input")
50
- # audio_interface = gr.Interface(fn=chatbot, inputs=gr.inputs.Audio(source="microphone", type="numpy"), outputs="text", title="Audio Input")
51
-
52
- # inputs_text = gr.inputs.Textbox(lines=7, label="Chat with AI")
53
- # inputs_audio = gr.inputs.Audio(source="microphone", type="numpy")
54
- # outputs = gr.outputs.Textbox(label="Reply")
55
-
56
- # text_interface = gr.Interface(fn=chatbot, inputs=inputs_text, outputs=outputs, title="AttachmentBot",
57
- # description="Let me survey you about your attachment with certain people in your life",
58
- # theme="compact").launch(share=True)
59
-
60
- # audio_interface = gr.Interface(fn=chatbot, inputs=inputs_audio, outputs=outputs, title="AttachmentBot",
61
- # description="Let me survey you about your attachment with certain people in your life",
62
- # theme="compact").launch(share=True)
63
- # Create a tabbed interface
64
- # tabbed_interface = gr.TabbedInterface([text_interface, audio_interface])
65
 
66
- # Launch the interface
67
- # tabbed_interface.launch(share=True)
68
 
69
  inputs = gr.inputs.Textbox(lines=7, label="Chat with AttachmentBot")
70
- outputs = gr.outputs.Textbox(label="Reply")
71
-
72
-
73
 
74
  gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AttachmentBot",
75
- description="Let's learn more about the important relationships in your life. Type 'start' in the box below to begin.",
76
- theme="compact").launch()
 
1
+ # import openai
2
+ # import gradio as gr
3
+ # import config
4
+
5
+
6
+
7
+ # openai.api_key = config.OPENAI_API_KEY
8
+
9
+ # messages = [
10
+ # {"role": "system", "content":"You are an attachment and personality surveyor"},
11
+ # {"role": "user", "content":"""ask me each question from this questionnaire and rewrite it as an open-ended question and wait for each response. Empathize with me and regularly ask for clarification why I answered with a certain response. Here is the questionnaire:
12
+ # What is your romantic partner's name?
13
+ # How did you two meet?
14
+ # When did you know you wanted to be with this person?
15
+ # Can you describe your relationship for me?
16
+ # How serious is your relationship?
17
+ # Have you thought about having kids with your romantic partner?
18
+ # What kind of parent do you want to be?
19
+ # If they didn't elaborate on the reason why, ask them "Why"?
20
+ # How did your relationship with your mother and father impact your plans to have children?
21
+ # What was your relationship with your mother like as a kid?
22
+ # How has your relationship with your mother changed since you were a kid?
23
+ # What was your relationship with your father like as a kid?
24
+ # How has your relationship with your mother like as a kid?
25
+ # After asking these questions, you can bid them farewell and thank them for their time. Show a lot of gratitude here.
26
+ # """}
27
+ # ]
28
+
29
+
30
+ # def convert_audio_to_text(audio):
31
+ # audio_file = open(audio, "rb")
32
+ # transcript = openai.Audio.transcribe("whisper-1", audio_file)
33
+ # return transcript
34
+
35
+ # def chatbot(input):
36
+ # # if input_type == "audio":
37
+ # # input = convert_audio_to_text(input)
38
+ # if input:
39
+ # messages.append({"role": "user", "content": input})
40
+ # chat = openai.ChatCompletion.create(
41
+ # model="gpt-3.5-turbo", messages=messages
42
+ # )
43
+ # reply = chat.choices[0].message.content
44
+ # messages.append({"role": "assistant", "content": reply})
45
+ # return reply
46
+
47
+
48
+ # # Define the Gradio interfaces
49
+ # # text_interface = gr.Interface(fn=chatbot, inputs=gr.inputs.Textbox("Enter your message here"), outputs="text", title="Text Input")
50
+ # # audio_interface = gr.Interface(fn=chatbot, inputs=gr.inputs.Audio(source="microphone", type="numpy"), outputs="text", title="Audio Input")
51
+
52
+ # # inputs_text = gr.inputs.Textbox(lines=7, label="Chat with AI")
53
+ # # inputs_audio = gr.inputs.Audio(source="microphone", type="numpy")
54
+ # # outputs = gr.outputs.Textbox(label="Reply")
55
+
56
+ # # text_interface = gr.Interface(fn=chatbot, inputs=inputs_text, outputs=outputs, title="AttachmentBot",
57
+ # # description="Let me survey you about your attachment with certain people in your life",
58
+ # # theme="compact").launch(share=True)
59
+
60
+ # # audio_interface = gr.Interface(fn=chatbot, inputs=inputs_audio, outputs=outputs, title="AttachmentBot",
61
+ # # description="Let me survey you about your attachment with certain people in your life",
62
+ # # theme="compact").launch(share=True)
63
+ # # Create a tabbed interface
64
+ # # tabbed_interface = gr.TabbedInterface([text_interface, audio_interface])
65
+
66
+ # # Launch the interface
67
+ # # tabbed_interface.launch(share=True)
68
+
69
+ # inputs = gr.inputs.Textbox(lines=7, label="Chat with AttachmentBot")
70
+ # outputs = gr.outputs.Textbox(label="Reply")
71
+
72
+
73
+
74
+ # gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AttachmentBot",
75
+ # description="Let's learn more about the important relationships in your life. Type 'start' in the box below to begin.",
76
+ # theme="compact").launch()
77
+
78
+
79
+
80
  import openai
81
  import gradio as gr
82
  import config
83
 
 
 
84
  openai.api_key = config.OPENAI_API_KEY
85
 
86
+ initial_messages = [
87
  {"role": "system", "content":"You are an attachment and personality surveyor"},
88
  {"role": "user", "content":"""ask me each question from this questionnaire and rewrite it as an open ended question and wait for each response. Empathize with me and regularly ask for clarification why I answered with a certain response. Here is the questionnaire:
89
+ Can you describe your relationship with your mother or a mother-like figure in your life?
90
+ Do you usually discuss your problems and concerns with your mother or a mother-like figure?
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
+ """},
93
+ ]
 
 
 
94
 
95
  def chatbot(input):
96
+ if not hasattr(chatbot, "messages"):
97
+ chatbot.messages = initial_messages.copy()
98
+
99
  if input:
100
+ chatbot.messages.append({"role": "user", "content": input})
101
  chat = openai.ChatCompletion.create(
102
+ model="gpt-3.5-turbo", messages=chatbot.messages
103
  )
104
  reply = chat.choices[0].message.content
105
+ chatbot.messages.append({"role": "assistant", "content": reply})
106
+
107
+ conversation = ""
108
+ for message in chatbot.messages[2:]:
109
+ role = "You" if message["role"] == "user" else "AttachmentBot"
110
+ conversation += f"{role}: {message['content']}\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
+ return conversation
 
113
 
114
  inputs = gr.inputs.Textbox(lines=7, label="Chat with AttachmentBot")
115
+ outputs = gr.outputs.Textbox(label="Conversation")
 
 
116
 
117
  gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AttachmentBot",
118
+ description="Let me survey you about your attachment with certain people in your life, to begin enter start",
119
+ theme="compact").launch()