AjithKSenthil commited on
Commit
2733ff9
·
verified ·
1 Parent(s): 844eeba

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +18 -19
chatbot.py CHANGED
@@ -115,62 +115,61 @@ Do you usually discuss your problems and concerns with your mother or a mother-l
115
  """},
116
  ]
117
 
118
- def chatbot(user_id, input, state):
119
- if state is None:
120
- state = initial_messages.copy()
121
 
122
  if input:
123
- state.append({"role": "user", "content": input})
124
  response = client.chat.completions.create(
125
  model="gpt-3.5-turbo",
126
- messages=state
127
  )
128
  reply = response.choices[0].message["content"]
129
- state.append({"role": "assistant", "content": reply})
130
 
131
  conversation = ""
132
- for message in state[2:]:
133
  role = "You" if message["role"] == "user" else "AttachmentBot"
134
  conversation += f"{role}: {message['content']}\n"
135
 
136
  store_transcript(user_id, conversation)
137
- return conversation, state
138
 
139
  with gr.Blocks() as demo:
140
  username = gr.Textbox(label="Username")
141
  password = gr.Textbox(label="Password", type="password")
142
  login_button = gr.Button("Login")
143
  register_button = gr.Button("Register")
144
- user_id_state = gr.State()
145
  auth_message = gr.Textbox(visible=False)
146
 
147
  chat_input = gr.Textbox(lines=7, label="Chat with AttachmentBot", visible=False)
148
- chat_output = gr.Textbox(label="Conversation")
149
- state = gr.State()
150
 
151
  def login(username, password):
152
  user_id = authenticate(username, password)
153
  if user_id:
154
- return gr.update(visible=True), user_id, ""
155
  else:
156
- return gr.update(visible=False), None, "Invalid credentials"
157
 
158
  def register(username, password):
159
  user_id = register_user(username, password)
160
  if user_id:
161
- return gr.update(visible=True), user_id, "Registration successful, you can now login."
162
  else:
163
- return gr.update(visible=False), None, "Registration failed, try a different username."
164
 
165
- login_button.click(login, inputs=[username, password], outputs=[chat_input, user_id_state, auth_message])
166
- register_button.click(register, inputs=[username, password], outputs=[chat_input, user_id_state, auth_message])
167
 
168
  chat_interface = gr.Interface(
169
  fn=chatbot,
170
- inputs=[user_id_state, chat_input, state],
171
  outputs=[chat_output, state],
172
  title="AttachmentBot",
173
- description="Let me survey you about your attachment with certain people in your life. To begin, enter 'start'.",
174
  )
175
 
176
  demo.launch()
 
115
  """},
116
  ]
117
 
118
+ def chatbot(input, state):
119
+ user_id = state[0]
120
+ messages = state[1]
121
 
122
  if input:
123
+ messages.append({"role": "user", "content": input})
124
  response = client.chat.completions.create(
125
  model="gpt-3.5-turbo",
126
+ messages=messages
127
  )
128
  reply = response.choices[0].message["content"]
129
+ messages.append({"role": "assistant", "content": reply})
130
 
131
  conversation = ""
132
+ for message in messages[2:]:
133
  role = "You" if message["role"] == "user" else "AttachmentBot"
134
  conversation += f"{role}: {message['content']}\n"
135
 
136
  store_transcript(user_id, conversation)
137
+ return conversation, [user_id, messages]
138
 
139
  with gr.Blocks() as demo:
140
  username = gr.Textbox(label="Username")
141
  password = gr.Textbox(label="Password", type="password")
142
  login_button = gr.Button("Login")
143
  register_button = gr.Button("Register")
 
144
  auth_message = gr.Textbox(visible=False)
145
 
146
  chat_input = gr.Textbox(lines=7, label="Chat with AttachmentBot", visible=False)
147
+ chat_output = gr.Textbox(label="Conversation", visible=False)
148
+ state = gr.State([None, initial_messages.copy()])
149
 
150
  def login(username, password):
151
  user_id = authenticate(username, password)
152
  if user_id:
153
+ return gr.update(visible=True), gr.update(visible=True), [user_id, initial_messages.copy()], ""
154
  else:
155
+ return gr.update(visible=False), gr.update(visible=False), [None, initial_messages.copy()], "Invalid credentials"
156
 
157
  def register(username, password):
158
  user_id = register_user(username, password)
159
  if user_id:
160
+ return gr.update(visible=True), gr.update(visible=True), [user_id, initial_messages.copy()], "Registration successful, you can now login."
161
  else:
162
+ return gr.update(visible=False), gr.update(visible=False), [None, initial_messages.copy()], "Registration failed, try a different username."
163
 
164
+ login_button.click(login, inputs=[username, password], outputs=[chat_input, chat_output, state, auth_message])
165
+ register_button.click(register, inputs=[username, password], outputs=[chat_input, chat_output, state, auth_message])
166
 
167
  chat_interface = gr.Interface(
168
  fn=chatbot,
169
+ inputs=[chat_input, state],
170
  outputs=[chat_output, state],
171
  title="AttachmentBot",
172
+ description="Let me survey you about your attachment with certain people in your life. To begin, enter 'start'."
173
  )
174
 
175
  demo.launch()