sjw commited on
Commit
f867f74
·
1 Parent(s): ec76bbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -13
app.py CHANGED
@@ -5,6 +5,7 @@ from gradio import components
5
  from huggingface_hub import Repository, hf_hub_download
6
  from datetime import datetime
7
 
 
8
  DATASET_REPO_URL = "https://huggingface.co/datasets/sjw/data.csv"
9
  DATASET_REPO_ID = "sjw/data.csv"
10
  DATA_FILENAME = "data.csv"
@@ -13,17 +14,31 @@ DATA_FILE = os.path.join(DIRNAME, DATA_FILENAME)
13
  HF_TOKEN = os.environ.get("HF_TOKEN")
14
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  try:
17
  hf_hub_download(
18
  repo_id=DATASET_REPO_ID,
19
  filename=DATA_FILENAME,
20
- force_filename=DATA_FILENAME
21
  )
22
  except:
23
  print("file not found")
24
 
 
25
  repo = Repository(
26
- local_dir = DIRNAME, clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
 
 
27
  )
28
 
29
 
@@ -40,29 +55,38 @@ def generate_html() -> str:
40
  html = "<div class='chatbot'>"
41
  for row in rows:
42
  html += "<div>"
43
- html += f"<span><b>{row['name']}</b></span> " # Make the name bold and add a space after it
44
  html += f"<span class='message'>{row['message']}</span>"
45
  html += "</div>"
46
  html += "</div>"
47
  return html
48
-
49
- def store_message(name: str, message: str):
50
 
 
 
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, "message": message, "time": str(datetime.now())}
56
  )
 
57
  return generate_html()
58
 
59
- with gr.Blocks() as demo:
60
- gr.Markdown("# Title")
61
- name_input = components.Textbox(label="Your Username")
62
- message_input = components.Textbox(label="Your Feedback", lines=2)
63
- output_html = gr.HTML()
64
- submit_button = gr.Button("Submit")
65
 
66
- submit_button.click(store_message, inputs=[name_input, message_input], outputs=output_html)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- demo.launch()
 
5
  from huggingface_hub import Repository, hf_hub_download
6
  from datetime import datetime
7
 
8
+
9
  DATASET_REPO_URL = "https://huggingface.co/datasets/sjw/data.csv"
10
  DATASET_REPO_ID = "sjw/data.csv"
11
  DATA_FILENAME = "data.csv"
 
14
  HF_TOKEN = os.environ.get("HF_TOKEN")
15
 
16
 
17
+ # overriding/appending to the gradio template
18
+ SCRIPT = """
19
+ <script>
20
+ if (!window.hasBeenRun) {
21
+ window.hasBeenRun = true;
22
+ console.log("should only happen once");
23
+ document.querySelector("button.submit").click();
24
+ }
25
+ </script>
26
+ """
27
+
28
+
29
  try:
30
  hf_hub_download(
31
  repo_id=DATASET_REPO_ID,
32
  filename=DATA_FILENAME,
 
33
  )
34
  except:
35
  print("file not found")
36
 
37
+
38
  repo = Repository(
39
+ local_dir=DIRNAME,
40
+ clone_from=DATASET_REPO_URL,
41
+ use_auth_token=HF_TOKEN
42
  )
43
 
44
 
 
55
  html = "<div class='chatbot'>"
56
  for row in rows:
57
  html += "<div>"
58
+ html += f"<span>{row['name']}</span>"
59
  html += f"<span class='message'>{row['message']}</span>"
60
  html += "</div>"
61
  html += "</div>"
62
  return html
 
 
63
 
64
+
65
+ def store_message(name: str, message: str):
66
  if name and message:
67
  with open(DATA_FILE, "a") as csvfile:
68
  writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
69
  writer.writerow(
70
  {"name": name, "message": message, "time": str(datetime.now())}
71
  )
72
+ commit_url = repo.push_to_hub()
73
  return generate_html()
74
 
 
 
 
 
 
 
75
 
76
+ iface = gr.Interface(
77
+ store_message,
78
+ [
79
+ components.Textbox(placeholder="Your name"),
80
+ components.Textbox(placeholder="Your message", lines=2),
81
+ ],
82
+ "html",
83
+ css="""
84
+ .name {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
85
+ """,
86
+ title="Reading/writing to a HuggingFace dataset repo from Spaces",
87
+ description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
88
+ article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})",
89
+ allow_flagging="never"
90
+ )
91
 
92
+ iface.launch()