test commited on
Commit
48b9027
1 Parent(s): 4a18c89

fix storing message pair

Browse files
Files changed (2) hide show
  1. app.py +16 -8
  2. store.py +6 -10
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import os
2
- import time
3
  import uuid
4
  from typing import Dict, List, Optional, Tuple
5
 
@@ -65,11 +64,14 @@ def store_chat(
65
  msg_pair_medium: Tuple[str, str],
66
  medium_lang: str,
67
  ):
68
- store_message_pair(chat_id, msg_pair_bo, LANG_BO)
69
- store_message_pair(chat_id, msg_pair_medium, medium_lang)
 
 
 
70
 
71
 
72
- def bot(history_bo: list, request: gr.Request):
73
  """Translate user input to English, send to OpenAI, translate response to Tibetan, and return to user.
74
 
75
  Args:
@@ -96,7 +98,7 @@ def bot(history_bo: list, request: gr.Request):
96
  print("------------------------")
97
 
98
  store_chat(
99
- chat_id=request.client.host,
100
  msg_pair_bo=(input_bo, resopnse_bo),
101
  msg_pair_medium=(input_, response),
102
  medium_lang=LANG_MEDIUM,
@@ -104,11 +106,17 @@ def bot(history_bo: list, request: gr.Request):
104
  return history_bo
105
 
106
 
 
 
 
 
107
  with gr.Blocks() as demo:
 
108
  history_en = gr.State(value=[])
109
  history_bo = gr.Chatbot(label="Tibetan Chatbot").style(height=650)
110
  input_bo = gr.Textbox(
111
- show_label=False, placeholder="Type a message here and press enter"
 
112
  )
113
  input_bo.submit(
114
  fn=user,
@@ -117,11 +125,11 @@ with gr.Blocks() as demo:
117
  queue=False,
118
  ).then(
119
  fn=bot,
120
- inputs=[history_bo],
121
  outputs=[history_bo],
122
  )
123
 
124
  clear = gr.Button("New Chat")
125
- clear.click(lambda: ("", []), None, [input_bo, history_bo], queue=False)
126
 
127
  demo.launch()
 
1
  import os
 
2
  import uuid
3
  from typing import Dict, List, Optional, Tuple
4
 
 
64
  msg_pair_medium: Tuple[str, str],
65
  medium_lang: str,
66
  ):
67
+ msg_pair = {
68
+ "bo": msg_pair_bo,
69
+ medium_lang: msg_pair_medium,
70
+ }
71
+ store_message_pair(chat_id, msg_pair)
72
 
73
 
74
+ def bot(history_bo: list, chat_id: str):
75
  """Translate user input to English, send to OpenAI, translate response to Tibetan, and return to user.
76
 
77
  Args:
 
98
  print("------------------------")
99
 
100
  store_chat(
101
+ chat_id=chat_id,
102
  msg_pair_bo=(input_bo, resopnse_bo),
103
  msg_pair_medium=(input_, response),
104
  medium_lang=LANG_MEDIUM,
 
106
  return history_bo
107
 
108
 
109
+ def get_chat_id():
110
+ return str(uuid.uuid4())
111
+
112
+
113
  with gr.Blocks() as demo:
114
+ chat_id = gr.State(value=get_chat_id)
115
  history_en = gr.State(value=[])
116
  history_bo = gr.Chatbot(label="Tibetan Chatbot").style(height=650)
117
  input_bo = gr.Textbox(
118
+ show_label=False,
119
+ placeholder=f"Type a message here and press enter",
120
  )
121
  input_bo.submit(
122
  fn=user,
 
125
  queue=False,
126
  ).then(
127
  fn=bot,
128
+ inputs=[history_bo, chat_id],
129
  outputs=[history_bo],
130
  )
131
 
132
  clear = gr.Button("New Chat")
133
+ clear.click(lambda: [], None, history_bo, queue=False)
134
 
135
  demo.launch()
store.py CHANGED
@@ -1,7 +1,7 @@
1
  import json
2
  import uuid
3
  from datetime import datetime
4
- from typing import Tuple
5
 
6
  import boto3
7
 
@@ -9,7 +9,7 @@ dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
9
  all_chats_table = dynamodb.Table("ChatbotTibetanAllChats")
10
 
11
 
12
- def store_message_pair(chat_id: str, msg_pair: Tuple[str, str], lang: str):
13
  """Store the chat history to DynamoDB
14
 
15
  Args:
@@ -25,7 +25,6 @@ def store_message_pair(chat_id: str, msg_pair: Tuple[str, str], lang: str):
25
  Item={
26
  "msg_pair_id": msg_pair_id,
27
  "msg_pair": json.dumps(msg_pair, ensure_ascii=False),
28
- "lang": lang,
29
  "created_at": datetime.now().isoformat(),
30
  "chat_id": chat_id,
31
  }
@@ -35,12 +34,9 @@ def store_message_pair(chat_id: str, msg_pair: Tuple[str, str], lang: str):
35
 
36
  if __name__ == "__main__":
37
  # Replace with your own DynamoDB table name and chat ID
38
- chat_id = uuid.uuid4().hex[:4]
39
 
40
  # Replace with your own chat history (list of tuples or list of dictionaries)
41
- chat_history = [
42
- ("User", "Hello, how are you?"),
43
- ("Chatbot", "I am fine, thank you!"),
44
- ]
45
- for msg_pair in chat_history:
46
- store_message_pair(chat_id, msg_pair, "en")
 
1
  import json
2
  import uuid
3
  from datetime import datetime
4
+ from typing import Dict, Tuple
5
 
6
  import boto3
7
 
 
9
  all_chats_table = dynamodb.Table("ChatbotTibetanAllChats")
10
 
11
 
12
+ def store_message_pair(chat_id: str, msg_pair: Dict[str, Tuple[str, str]]):
13
  """Store the chat history to DynamoDB
14
 
15
  Args:
 
25
  Item={
26
  "msg_pair_id": msg_pair_id,
27
  "msg_pair": json.dumps(msg_pair, ensure_ascii=False),
 
28
  "created_at": datetime.now().isoformat(),
29
  "chat_id": chat_id,
30
  }
 
34
 
35
  if __name__ == "__main__":
36
  # Replace with your own DynamoDB table name and chat ID
37
+ chat_id = str(uuid.uuid4())
38
 
39
  # Replace with your own chat history (list of tuples or list of dictionaries)
40
+ msg_pair = {"bo": ("hello", "hello"), "en": ("hello", "hello")}
41
+ response = store_message_pair(chat_id, msg_pair)
42
+ print(response)