Spaces:
Runtime error
Runtime error
add template to push to dataset
Browse files- local/push_to_dataset.py +86 -0
local/push_to_dataset.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import csv
|
3 |
+
import gradio as gr
|
4 |
+
from gradio import inputs, outputs
|
5 |
+
import huggingface_hub
|
6 |
+
from huggingface_hub import Repository
|
7 |
+
from datetime import datetime
|
8 |
+
|
9 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/julien-c/persistent-space-dataset"
|
10 |
+
DATA_FILENAME = "data.csv"
|
11 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
12 |
+
|
13 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
14 |
+
print("is none?", HF_TOKEN is None)
|
15 |
+
|
16 |
+
print("hfh", huggingface_hub.__version__)
|
17 |
+
|
18 |
+
# overriding/appending to the gradio template
|
19 |
+
SCRIPT = """
|
20 |
+
<script>
|
21 |
+
if (!window.hasBeenRun) {
|
22 |
+
window.hasBeenRun = true;
|
23 |
+
console.log("should only happen once");
|
24 |
+
document.querySelector("button.submit").click();
|
25 |
+
}
|
26 |
+
</script>
|
27 |
+
"""
|
28 |
+
with open(os.path.join(gr.networking.STATIC_TEMPLATE_LIB, "frontend", "index.html"), "a") as f:
|
29 |
+
f.write(SCRIPT)
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
repo = Repository(
|
34 |
+
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
35 |
+
)
|
36 |
+
|
37 |
+
|
38 |
+
def generate_html() -> str:
|
39 |
+
with open(DATA_FILE) as csvfile:
|
40 |
+
reader = csv.DictReader(csvfile)
|
41 |
+
rows = []
|
42 |
+
for row in reader:
|
43 |
+
rows.append(row)
|
44 |
+
rows.reverse()
|
45 |
+
if len(rows) == 0:
|
46 |
+
return "no messages yet"
|
47 |
+
else:
|
48 |
+
html = "<div class='chatbot'>"
|
49 |
+
for row in rows:
|
50 |
+
html += "<div>"
|
51 |
+
html += f"<span>{row['name']}</span>"
|
52 |
+
html += f"<span class='message'>{row['message']}</span>"
|
53 |
+
html += "</div>"
|
54 |
+
html += "</div>"
|
55 |
+
return html
|
56 |
+
|
57 |
+
|
58 |
+
def store_message(name: str, message: str):
|
59 |
+
if name and message:
|
60 |
+
with open(DATA_FILE, "a") as csvfile:
|
61 |
+
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
62 |
+
writer.writerow(
|
63 |
+
{"name": name, "message": message, "time": str(datetime.now())}
|
64 |
+
)
|
65 |
+
commit_url = repo.push_to_hub()
|
66 |
+
print(commit_url)
|
67 |
+
|
68 |
+
return generate_html()
|
69 |
+
|
70 |
+
|
71 |
+
iface = gr.Interface(
|
72 |
+
store_message,
|
73 |
+
[
|
74 |
+
inputs.Textbox(placeholder="Your name"),
|
75 |
+
inputs.Textbox(placeholder="Your message", lines=2),
|
76 |
+
],
|
77 |
+
"html",
|
78 |
+
css="""
|
79 |
+
.message {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
|
80 |
+
""",
|
81 |
+
title="Reading/writing to a HuggingFace dataset repo from Spaces",
|
82 |
+
description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
|
83 |
+
article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL}) (open in new tab)",
|
84 |
+
)
|
85 |
+
|
86 |
+
iface.launch()
|