awacke1 commited on
Commit
de6d7ec
·
1 Parent(s): bcd92de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -71
app.py CHANGED
@@ -12,80 +12,63 @@ import huggingface_hub
12
  from huggingface_hub import Repository, hf_hub_download, upload_file
13
  from datetime import datetime
14
 
15
- DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/ChatbotMemory.csv"
16
- DATASET_REPO_ID = "awacke1/ChatbotMemory.csv"
17
- DATA_FILENAME = "ChatbotMemory.csv"
18
- DATA_FILE = os.path.join("data", DATA_FILENAME)
19
- HF_TOKEN = os.environ.get("HF_TOKEN")
20
 
21
- SCRIPT = """
22
- <script>
23
- if (!window.hasBeenRun) {
24
- window.hasBeenRun = true;
25
- console.log("should only happen once");
26
- document.querySelector("button.submit").click();
27
- }
28
- </script>
29
- """
30
- try:
31
- hf_hub_download(
32
- repo_id=DATASET_REPO_ID,
33
- filename=DATA_FILENAME,
34
- cache_dir=DATA_DIRNAME,
35
- force_filename=DATA_FILENAME
36
- )
37
- except:
38
- print("file not found")
39
- repo = Repository(
40
- local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
41
- )
42
 
43
- def generate_html() -> str:
44
- with open(DATA_FILE) as csvfile:
45
- reader = csv.DictReader(csvfile)
46
- rows = []
47
- for row in reader:
48
- rows.append(row)
49
- rows.reverse()
50
- if len(rows) == 0:
51
- return "no messages yet"
52
- else:
53
- html = "<div class='chatbot'>"
54
- for row in rows:
55
- html += "<div>"
56
- html += f"<span>{row['inputs']}</span>"
57
- html += f"<span class='outputs'>{row['outputs']}</span>"
58
- html += "</div>"
59
- html += "</div>"
60
- return html
61
-
 
 
 
62
 
63
- def store_message(name: str, message: str):
64
- if name and message:
65
- with open(DATA_FILE, "a") as csvfile:
66
- writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
67
- writer.writerow(
68
- {"name": name.strip(), "message": message.strip(), "time": str(datetime.now())}
69
- )
70
- # 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.
71
- commit_url = repo.push_to_hub()
72
- return ""
73
 
74
- iface = gr.Interface(
75
- store_message,
76
- [
77
- inputs.Textbox(placeholder="Your name"),
78
- inputs.Textbox(placeholder="Your message", lines=2),
79
- ],
80
- "html",
81
- css="""
82
- .message {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
83
- """,
84
- title="Reading/writing to a HuggingFace dataset repo from Spaces",
85
- description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
86
- article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})",
87
- )
88
-
89
 
90
  mname = "facebook/blenderbot-400M-distill"
91
  model = BlenderbotForConditionalGeneration.from_pretrained(mname)
@@ -135,7 +118,7 @@ gr.Interface(
135
  title=title,
136
  allow_flagging="never",
137
  description=f"Gradio chatbot backed by memory in a dataset repository.",
138
- article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})"
139
  ).launch()
140
 
141
  #demo = gr.Blocks()
 
12
  from huggingface_hub import Repository, hf_hub_download, upload_file
13
  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>
25
+ #if (!window.hasBeenRun) {
26
+ # window.hasBeenRun = true;
27
+ # console.log("should only happen once");
28
+ # document.querySelector("button.submit").click();
29
+ #}
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,
59
+ # [
60
+ # inputs.Textbox(placeholder="Your name"),
61
+ # inputs.Textbox(placeholder="Your message", lines=2),
62
+ # ],
63
+ # "html",
64
+ # css="""
65
+ # .message {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
66
+ # """,
67
+ # title="Reading/writing to a HuggingFace dataset repo from Spaces",
68
+ # description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
69
+ # article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})",
70
+ #)
71
+ # --------------------------------------------------- For Memory
72
 
73
  mname = "facebook/blenderbot-400M-distill"
74
  model = BlenderbotForConditionalGeneration.from_pretrained(mname)
 
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()
123
 
124
  #demo = gr.Blocks()