Johnny Lee commited on
Commit
b596b0e
·
1 Parent(s): a3c7493
Files changed (2) hide show
  1. app.py +68 -15
  2. requirements.txt +2 -0
app.py CHANGED
@@ -2,19 +2,19 @@
2
  from __future__ import annotations
3
  import asyncio
4
  import datetime
 
5
  import logging
6
  import os
7
  from enum import Enum
8
- import requests
9
  import json
10
  import uuid
11
  from pydantic import BaseModel
 
12
 
13
  from copy import deepcopy
14
  from typing import Any, Dict, List, Optional, Tuple, Union
15
 
16
  import gradio as gr
17
- import pytz
18
  import tiktoken
19
 
20
  # from dotenv import load_dotenv
@@ -46,6 +46,10 @@ CLAUDE_2_CONTEXT_LENGTH = 100000 # need to use claude tokenizer
46
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
47
  ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
48
  HF_TOKEN = os.getenv("HF_TOKEN")
 
 
 
 
49
 
50
  theme = gr.themes.Soft()
51
 
@@ -56,6 +60,26 @@ gradio_flagger = gr.HuggingFaceDatasetSaver(
56
  )
57
 
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  class ChatSystemMessage(str, Enum):
60
  CASE_SYSTEM_MESSAGE = """You are a helpful AI assistant for a Columbia Business School MBA student.
61
  Follow this message's instructions carefully. Respond using markdown.
@@ -132,17 +156,23 @@ def reset_textbox():
132
 
133
 
134
  def auth(username, password):
135
- auth_endpoint = "https://worker.johnnyclee.com/auth"
136
  try:
137
- auth_payload = {username: password}
138
- auth_response = requests.post(
139
- auth_endpoint,
140
- json=auth_payload,
141
- timeout=3,
142
  )
143
- auth_response.raise_for_status()
144
- return auth_response.status_code == 200
 
 
 
 
 
 
 
 
 
145
  except Exception as exc:
 
146
  LOG.error(exc)
147
  return (username, password) in creds
148
 
@@ -431,18 +461,31 @@ async def respond(
431
  LOG.info(f"""[{request.username}] ENDING CHAIN""")
432
  LOG.debug(f"History: {state.history}")
433
  LOG.debug(f"Memory: {state.chain.memory.json()}")
 
 
 
 
434
  data_to_flag = (
435
  {
436
  "history": deepcopy(state.history),
437
  "username": request.username,
438
- "timestamp": datetime.datetime.now(datetime.timezone.utc).isoformat(),
439
  "session_id": state.session_id,
440
  "metadata": state.chain.metadata,
441
  "langsmith_url": langsmith_url,
442
  },
443
  )
444
- LOG.debug(f"Data to flag: {data_to_flag}")
445
  gradio_flagger.flag(flag_data=data_to_flag, username=request.username)
 
 
 
 
 
 
 
 
 
 
446
  except Exception as e:
447
  LOG.error(e)
448
  raise e
@@ -450,7 +493,7 @@ async def respond(
450
 
451
  class ChatbotConfig(BaseModel):
452
  app_title: str = "CBS Technology Strategy - Fall 2023"
453
- chatbot_modes: List[ChatbotMode] = [mode for mode in ChatbotMode]
454
  case_options: List[str] = poll_questions.get_case_names()
455
  default_case_option: str = "Netflix"
456
 
@@ -464,14 +507,24 @@ def change_chatbot_mode(
464
  new_session = ChatSession.new(
465
  use_claude=False,
466
  system_msg=ChatSystemMessage.CASE_SYSTEM_MESSAGE,
467
- metadata=dict(username=request.username),
 
 
 
 
 
468
  poll_question_name=case_input,
469
  )
470
  else:
471
  new_session = ChatSession.new(
472
  use_claude=True,
473
  system_msg=ChatSystemMessage.RESEARCH_SYSTEM_MESSAGE,
474
- metadata=dict(username=request.username),
 
 
 
 
 
475
  poll_question_name=None,
476
  )
477
  state = new_session
 
2
  from __future__ import annotations
3
  import asyncio
4
  import datetime
5
+ import pytz
6
  import logging
7
  import os
8
  from enum import Enum
 
9
  import json
10
  import uuid
11
  from pydantic import BaseModel
12
+ import gspread
13
 
14
  from copy import deepcopy
15
  from typing import Any, Dict, List, Optional, Tuple, Union
16
 
17
  import gradio as gr
 
18
  import tiktoken
19
 
20
  # from dotenv import load_dotenv
 
46
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
47
  ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
48
  HF_TOKEN = os.getenv("HF_TOKEN")
49
+ GS_CREDS = json.loads(rf"""{os.getenv("GSPREAD_SERVICE")}""")
50
+ GSHEET_ID = os.getenv("GSHEET_ID")
51
+ AUTH_GSHEET_NAME = os.getenv("AUTH_GSHEET_NAME")
52
+ TURNS_GSHEET_NAME = os.getenv("TURNS_GSHEET_NAME")
53
 
54
  theme = gr.themes.Soft()
55
 
 
60
  )
61
 
62
 
63
+ def get_gsheet_rows(
64
+ sheet_id: str, sheet_name: str, creds: Dict[str, str]
65
+ ) -> List[Dict[str, str]]:
66
+ gc = gspread.service_account_from_dict(creds)
67
+ worksheet = gc.open_by_key(sheet_id).worksheet(sheet_name)
68
+ rows = worksheet.get_all_records()
69
+ return rows
70
+
71
+
72
+ def append_gsheet_rows(
73
+ sheet_id: str,
74
+ rows: List[List[str]],
75
+ sheet_name: str,
76
+ creds: Dict[str, str],
77
+ ) -> None:
78
+ gc = gspread.service_account_from_dict(creds)
79
+ worksheet = gc.open_by_key(sheet_id).worksheet(sheet_name)
80
+ worksheet.append_rows(values=rows, insert_data_option="INSERT_ROWS")
81
+
82
+
83
  class ChatSystemMessage(str, Enum):
84
  CASE_SYSTEM_MESSAGE = """You are a helpful AI assistant for a Columbia Business School MBA student.
85
  Follow this message's instructions carefully. Respond using markdown.
 
156
 
157
 
158
  def auth(username, password):
 
159
  try:
160
+ auth_records = get_gsheet_rows(
161
+ sheet_id=GSHEET_ID, sheet_name=AUTH_GSHEET_NAME, creds=GS_CREDS
 
 
 
162
  )
163
+ auth_dict = {user["username"]: user["password"] for user in auth_records}
164
+ search_auth_user = auth_dict.get(username)
165
+ if search_auth_user:
166
+ autheticated = search_auth_user == password
167
+ if autheticated:
168
+ LOG.info(f"{username} successfully logged in.")
169
+ return autheticated
170
+ else:
171
+ LOG.info(f"{username} failed to login.")
172
+ return False
173
+
174
  except Exception as exc:
175
+ LOG.info(f"{username} failed to login")
176
  LOG.error(exc)
177
  return (username, password) in creds
178
 
 
461
  LOG.info(f"""[{request.username}] ENDING CHAIN""")
462
  LOG.debug(f"History: {state.history}")
463
  LOG.debug(f"Memory: {state.chain.memory.json()}")
464
+ current_timestamp = datetime.datetime.now(pytz.timezone("US/Eastern")).replace(
465
+ tzinfo=None
466
+ )
467
+ timestamp_string = current_timestamp.strftime("%Y-%m-%d %H:%M:%S")
468
  data_to_flag = (
469
  {
470
  "history": deepcopy(state.history),
471
  "username": request.username,
472
+ "timestamp": timestamp_string,
473
  "session_id": state.session_id,
474
  "metadata": state.chain.metadata,
475
  "langsmith_url": langsmith_url,
476
  },
477
  )
 
478
  gradio_flagger.flag(flag_data=data_to_flag, username=request.username)
479
+ (flagged_data,) = data_to_flag
480
+ metadata_to_gsheet = flagged_data.get("metadata").values()
481
+ gsheet_row = [[timestamp_string, *metadata_to_gsheet]]
482
+ LOG.info(f"Data to GSHEET: {gsheet_row}")
483
+ append_gsheet_rows(
484
+ sheet_id=GSHEET_ID,
485
+ sheet_name=TURNS_GSHEET_NAME,
486
+ rows=gsheet_row,
487
+ creds=GS_CREDS,
488
+ )
489
  except Exception as e:
490
  LOG.error(e)
491
  raise e
 
493
 
494
  class ChatbotConfig(BaseModel):
495
  app_title: str = "CBS Technology Strategy - Fall 2023"
496
+ chatbot_modes: List[ChatbotMode] = [mode.value for mode in ChatbotMode]
497
  case_options: List[str] = poll_questions.get_case_names()
498
  default_case_option: str = "Netflix"
499
 
 
507
  new_session = ChatSession.new(
508
  use_claude=False,
509
  system_msg=ChatSystemMessage.CASE_SYSTEM_MESSAGE,
510
+ metadata=ChatSession.set_metadata(
511
+ username=request.username,
512
+ chatbot_mode=chatbot_mode,
513
+ turns_completed=0,
514
+ case=poll_question_name,
515
+ ),
516
  poll_question_name=case_input,
517
  )
518
  else:
519
  new_session = ChatSession.new(
520
  use_claude=True,
521
  system_msg=ChatSystemMessage.RESEARCH_SYSTEM_MESSAGE,
522
+ metadata=ChatSession.set_metadata(
523
+ username=request.username,
524
+ chatbot_mode=chatbot_mode,
525
+ turns_completed=0,
526
+ case=poll_question_name,
527
+ ),
528
  poll_question_name=None,
529
  )
530
  state = new_session
requirements.txt CHANGED
@@ -1,5 +1,7 @@
1
  anthropic==0.3.7
2
  gradio==3.39.0
 
3
  langchain==0.0.265
4
  openai==0.27.8
 
5
  tiktoken==0.4.0
 
1
  anthropic==0.3.7
2
  gradio==3.39.0
3
+ gspread==5.10.0
4
  langchain==0.0.265
5
  openai==0.27.8
6
+ pytz==2023.3
7
  tiktoken==0.4.0