peterpull commited on
Commit
f7a821d
·
1 Parent(s): 8966b93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -13,7 +13,7 @@ os.environ["OPENAI_API_KEY"] = os.environ['SECRET_CODE']
13
 
14
  # Need to write to persistent dataset because cannot store temp data on spaces
15
  DATASET_REPO_URL = "https://huggingface.co/datasets/peterpull/MediatorBot"
16
- DATA_FILENAME = "data.csv"
17
  DATA_FILE = os.path.join("data", DATA_FILENAME)
18
 
19
  # I am guessing we need a write access token.
@@ -28,36 +28,33 @@ repo = Repository(
28
 
29
 
30
  def generate_html() -> str:
31
- with open(DATA_FILE) as csvfile:
32
- reader = csv.DictReader(csvfile)
33
- rows = []
34
- for row in reader:
35
- rows.append(row)
36
  rows.reverse()
37
  if len(rows) == 0:
38
  return "no messages yet"
39
  else:
40
  html = "<div class='chatbot'>"
41
  for row in rows:
 
 
 
 
42
  html += "<div>"
43
- html += f"<span>{row['User']}</span>"
44
- html += f"<span class='message'>{row['Chatbot']}</span>"
45
- html += f"<span class='time'>{row['time']}</span>"
46
  html += "</div>"
47
  html += "</div>"
48
  return html
49
 
50
  def store_message(chatinput: str, chatresponse: str):
51
  if chatinput and chatresponse:
52
- with open(DATA_FILE, "a") as csvfile:
53
- writer = csv.DictWriter(csvfile, fieldnames=["User", "Chatbot", "time"])
54
- print({"User": chatinput, "Chatbot": chatresponse, "time": str(datetime.now())})
55
- writer.writerow(
56
- {"User": chatinput, "Chatbot": chatresponse, "time": str(datetime.now())}
57
- )
58
  commit_url = repo.push_to_hub()
59
  print(commit_url)
60
-
61
  return generate_html()
62
 
63
 
 
13
 
14
  # Need to write to persistent dataset because cannot store temp data on spaces
15
  DATASET_REPO_URL = "https://huggingface.co/datasets/peterpull/MediatorBot"
16
+ DATA_FILENAME = "data.txt"
17
  DATA_FILE = os.path.join("data", DATA_FILENAME)
18
 
19
  # I am guessing we need a write access token.
 
28
 
29
 
30
  def generate_html() -> str:
31
+ with open(DATA_FILE) as file:
32
+ rows = file.readlines()
 
 
 
33
  rows.reverse()
34
  if len(rows) == 0:
35
  return "no messages yet"
36
  else:
37
  html = "<div class='chatbot'>"
38
  for row in rows:
39
+ row_parts = row.split(",")
40
+ user = row_parts[0].strip()
41
+ chatbot = row_parts[1].strip()
42
+ time = row_parts[2].strip()
43
  html += "<div>"
44
+ html += f"<span>{user}</span>"
45
+ html += f"<span class='message'>{chatbot}</span>"
46
+ html += f"<span class='time'>{time}</span>"
47
  html += "</div>"
48
  html += "</div>"
49
  return html
50
 
51
  def store_message(chatinput: str, chatresponse: str):
52
  if chatinput and chatresponse:
53
+ with open(DATA_FILE, "a") as file:
54
+ file.write(f"User: {chatinput}, Chatbot: {chatresponse}, Time: {str(datetime.now())}\n")
55
+ # commit changes to repo
 
 
 
56
  commit_url = repo.push_to_hub()
57
  print(commit_url)
 
58
  return generate_html()
59
 
60