awacke1 commited on
Commit
6bbb8ab
·
verified ·
1 Parent(s): 83fd510

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -20
app.py CHANGED
@@ -44,31 +44,31 @@ def store_message(name: str, message: str, outputfileName: str):
44
  basedir = os.path.dirname(__file__)
45
  savePath = outputfileName
46
 
47
- # if file doesnt exist, create it with labels
48
  from os.path import exists
49
  file_exists = exists(savePath)
50
 
51
- if (file_exists==False):
52
- with open(savePath, "w") as f: #write
53
- f.write(str("time, message, text\n")) # one time only to get column headers for CSV file
54
- if name and message:
55
- writer = csv.DictWriter(f, fieldnames=["time", "message", "name"])
56
- writer.writerow(
57
- {"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
58
- )
59
- df = pd.read_csv(savePath)
60
- df = df.sort_values(df.columns[0],ascending=False)
61
- else:
62
- if name and message:
63
- with open(savePath, "a") as csvfile:
64
- writer = csv.DictWriter(csvfile, fieldnames=[ "time", "message", "name", ])
65
- writer.writerow(
66
- {"time": str(datetime.now()), "message": message.strip(), "name": name.strip() }
67
- )
68
- df = pd.read_csv(savePath)
69
- df = df.sort_values(df.columns[0],ascending=False)
70
  return df
71
 
 
72
  mname = "facebook/blenderbot-400M-distill"
73
  model = BlenderbotForConditionalGeneration.from_pretrained(mname)
74
  tokenizer = BlenderbotTokenizer.from_pretrained(mname)
 
44
  basedir = os.path.dirname(__file__)
45
  savePath = outputfileName
46
 
47
+ # if file doesn't exist, create it with labels and a few default rows
48
  from os.path import exists
49
  file_exists = exists(savePath)
50
 
51
+ if not file_exists:
52
+ with open(savePath, "w") as f: # Create and write column headers and default content
53
+ f.write("time, message, name\n") # Column headers
54
+ # Write a few default rows (if needed)
55
+ f.write(f"{str(datetime.now())}, Welcome to Chatback!, System\n")
56
+ f.write(f"{str(datetime.now())}, How can I assist you today?, System\n")
57
+
58
+ # Proceed to add the actual message if name and message are provided
59
+ if name and message:
60
+ with open(savePath, "a") as csvfile:
61
+ writer = csv.DictWriter(csvfile, fieldnames=["time", "message", "name"])
62
+ writer.writerow(
63
+ {"time": str(datetime.now()), "message": message.strip(), "name": name.strip()}
64
+ )
65
+
66
+ # Load and sort the dataframe
67
+ df = pd.read_csv(savePath)
68
+ df = df.sort_values(df.columns[0], ascending=False)
 
69
  return df
70
 
71
+
72
  mname = "facebook/blenderbot-400M-distill"
73
  model = BlenderbotForConditionalGeneration.from_pretrained(mname)
74
  tokenizer = BlenderbotTokenizer.from_pretrained(mname)