AjithKSenthil commited on
Commit
587a8b7
·
verified ·
1 Parent(s): 14208b2

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +11 -10
chatbot.py CHANGED
@@ -115,26 +115,26 @@ Do you usually discuss your problems and concerns with your mother or a mother-l
115
  """},
116
  ]
117
 
118
- def chatbot(user_id, input):
119
- if not hasattr(chatbot, "messages"):
120
- chatbot.messages = initial_messages.copy()
121
 
122
  if input:
123
- chatbot.messages.append({"role": "user", "content": input})
124
  response = client.chat.completions.create(
125
  model="gpt-3.5-turbo",
126
- messages=chatbot.messages
127
  )
128
  reply = response.choices[0].message["content"]
129
- chatbot.messages.append({"role": "assistant", "content": reply})
130
 
131
  conversation = ""
132
- for message in chatbot.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
138
 
139
  with gr.Blocks() as demo:
140
  username = gr.Textbox(label="Username")
@@ -145,11 +145,12 @@ with gr.Blocks() as demo:
145
 
146
  chat_input = gr.Textbox(lines=7, label="Chat with AttachmentBot")
147
  chat_output = gr.Textbox(label="Conversation")
 
148
 
149
  chat_interface = gr.Interface(
150
  fn=chatbot,
151
- inputs=[user_id_state, chat_input],
152
- outputs=chat_output,
153
  title="AttachmentBot",
154
  description="Let me survey you about your attachment with certain people in your life. To begin, enter 'start'.",
155
  visible=False # Initially hidden, shown after login
 
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")
 
145
 
146
  chat_input = gr.Textbox(lines=7, label="Chat with AttachmentBot")
147
  chat_output = gr.Textbox(label="Conversation")
148
+ state = gr.State()
149
 
150
  chat_interface = gr.Interface(
151
  fn=chatbot,
152
+ inputs=[user_id_state, chat_input, state],
153
+ outputs=[chat_output, state],
154
  title="AttachmentBot",
155
  description="Let me survey you about your attachment with certain people in your life. To begin, enter 'start'.",
156
  visible=False # Initially hidden, shown after login