awacke1 commited on
Commit
9b06b1e
·
1 Parent(s): 4f55cf4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -28
app.py CHANGED
@@ -14,11 +14,14 @@ from datetime import datetime
14
 
15
 
16
  # -------------------------------------------- For Memory - you will need to set up a dataset and HF_TOKEN ---------
17
- #DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/ChatbotMemory.csv"
18
- #DATASET_REPO_ID = "awacke1/ChatbotMemory.csv"
19
- #DATA_FILENAME = "ChatbotMemory.csv"
20
- #DATA_FILE = os.path.join("data", DATA_FILENAME)
21
- #HF_TOKEN = os.environ.get("HF_TOKEN")
 
 
 
22
 
23
  #SCRIPT = """
24
  #<script>
@@ -30,29 +33,30 @@ from datetime import datetime
30
  #</script>
31
  #"""
32
 
33
- #try:
34
- # hf_hub_download(
35
- # repo_id=DATASET_REPO_ID,
36
- # filename=DATA_FILENAME,
37
- # cache_dir=DATA_DIRNAME,
38
- # force_filename=DATA_FILENAME
39
- # )
40
- #except:
41
- # print("file not found")
42
- #repo = Repository(
43
- # local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
44
- #)
 
45
 
46
- #def store_message(name: str, message: str):
47
- # if name and message:
48
- # with open(DATA_FILE, "a") as csvfile:
49
- # writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
50
- # writer.writerow(
51
- # {"name": name.strip(), "message": message.strip(), "time": str(datetime.now())}
52
- # )
53
  # uncomment line below to begin saving. If creating your own copy you will need to add a access token called "HF_TOKEN" to your profile, then create a secret for your repo with the access code naming it "HF_TOKEN" For the CSV as well you can copy the header and first few lines to your own then update the paths above which should work to save to your own repository for datasets.
54
- # commit_url = repo.push_to_hub()
55
- # return ""
56
 
57
  #iface = gr.Interface(
58
  # store_message,
@@ -89,6 +93,8 @@ def add_note_to_history(note, note_history):
89
  note_history = '</s> <s>'.join(note_history)
90
  return [note_history]
91
 
 
 
92
  title = "State of the Art Chatbot with Memory Dataset"
93
  description = """Chatbot With Memory"""
94
 
@@ -106,9 +112,17 @@ def chat(message, history):
106
  history_useful = add_note_to_history(response, history_useful)
107
  list_history = history_useful[0].split('</s> <s>')
108
  history.append((list_history[-2], list_history[-1]))
109
- # store_message(message, response) # Save to dataset -- uncomment with code above, create a dataset to store and add your HF_TOKEN from profile to this repo to use.
 
 
 
 
 
 
110
  return history, history
111
 
 
 
112
  gr.Interface(
113
  fn=chat,
114
  theme="huggingface",
@@ -117,8 +131,11 @@ gr.Interface(
117
  outputs=["chatbot", "state"],
118
  title=title,
119
  allow_flagging="never",
 
 
120
  description=f"Gradio chatbot backed by memory in a dataset repository.",
121
- # article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})"
 
122
  ).launch(debug=True)
123
 
124
  #demo = gr.Blocks()
 
14
 
15
 
16
  # -------------------------------------------- For Memory - you will need to set up a dataset and HF_TOKEN ---------
17
+ UseMemory=True
18
+
19
+ if UseMemory:
20
+ DATASET_REPO_URL="https://huggingface.co/datasets/awacke1/ChatbotMemory.csv"
21
+ DATASET_REPO_ID="awacke1/ChatbotMemory.csv"
22
+ DATA_FILENAME="ChatbotMemory.csv"
23
+ DATA_FILE=os.path.join("data", DATA_FILENAME)
24
+ HF_TOKEN=os.environ.get("HF_TOKEN")
25
 
26
  #SCRIPT = """
27
  #<script>
 
33
  #</script>
34
  #"""
35
 
36
+ if UseMemory:
37
+ try:
38
+ hf_hub_download(
39
+ repo_id=DATASET_REPO_ID,
40
+ filename=DATA_FILENAME,
41
+ cache_dir=DATA_DIRNAME,
42
+ force_filename=DATA_FILENAME
43
+ )
44
+ except:
45
+ print("file not found")
46
+ repo = Repository(
47
+ local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
48
+ )
49
 
50
+ def store_message(name: str, message: str):
51
+ if name and message:
52
+ with open(DATA_FILE, "a") as csvfile:
53
+ writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
54
+ writer.writerow(
55
+ {"name": name.strip(), "message": message.strip(), "time": str(datetime.now())}
56
+ )
57
  # uncomment line below to begin saving. If creating your own copy you will need to add a access token called "HF_TOKEN" to your profile, then create a secret for your repo with the access code naming it "HF_TOKEN" For the CSV as well you can copy the header and first few lines to your own then update the paths above which should work to save to your own repository for datasets.
58
+ commit_url = repo.push_to_hub()
59
+ return ""
60
 
61
  #iface = gr.Interface(
62
  # store_message,
 
93
  note_history = '</s> <s>'.join(note_history)
94
  return [note_history]
95
 
96
+
97
+
98
  title = "State of the Art Chatbot with Memory Dataset"
99
  description = """Chatbot With Memory"""
100
 
 
112
  history_useful = add_note_to_history(response, history_useful)
113
  list_history = history_useful[0].split('</s> <s>')
114
  history.append((list_history[-2], list_history[-1]))
115
+
116
+ # AGI semantic and episodic memory here we come....
117
+
118
+ store_message(message, response) # Save to dataset -- uncomment with code above, create a dataset to store and add your HF_TOKEN from profile to this repo to use.
119
+
120
+ # ....
121
+
122
  return history, history
123
 
124
+
125
+
126
  gr.Interface(
127
  fn=chat,
128
  theme="huggingface",
 
131
  outputs=["chatbot", "state"],
132
  title=title,
133
  allow_flagging="never",
134
+
135
+
136
  description=f"Gradio chatbot backed by memory in a dataset repository.",
137
+ article=f"The memory dataset for saves is [{DATASET_REPO_URL}]({DATASET_REPO_URL}) Use the dataset preview tool here to check it out: https://huggingface.co/spaces/awacke1/FreddysDatasetViewer "
138
+
139
  ).launch(debug=True)
140
 
141
  #demo = gr.Blocks()