julien-c HF staff commited on
Commit
906df3a
1 Parent(s): a9fdb6e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 hf_hub_download, upload_file
7
+ from datetime import datetime
8
+
9
+ DATASET_REPO_ID = "datasets/elonmuskceo/persistent-space-dataset"
10
+ DATASET_REPO_URL = f"https://huggingface.co/{DATASET_REPO_ID}"
11
+ DATA_DIRNAME = "data"
12
+ DATA_FILENAME = "data.csv"
13
+ DATA_FILE = os.path.join(DATA_DIRNAME, DATA_FILENAME)
14
+
15
+ HF_TOKEN = os.environ.get("HF_TOKEN")
16
+
17
+
18
+ print("hfh", huggingface_hub.__version__)
19
+
20
+ # overriding/appending to the gradio template
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
+ with open(os.path.join(gr.networking.STATIC_TEMPLATE_LIB, "frontend", "index.html"), "a") as f:
31
+ f.write(SCRIPT)
32
+
33
+
34
+ try:
35
+ hf_hub_download(
36
+ repo_id=DATASET_REPO_ID,
37
+ filename=DATA_FILENAME,
38
+ cache_dir=DATA_DIRNAME,
39
+ force_download=DATA_FILENAME
40
+ )
41
+ except:
42
+ # file not found, we'll create it on the fly.
43
+ print("file not found, probably")
44
+
45
+
46
+ def generate_html() -> str:
47
+ with open(DATA_FILE) as csvfile:
48
+ reader = csv.DictReader(csvfile)
49
+ rows = []
50
+ for row in reader:
51
+ rows.append(row)
52
+ rows.reverse()
53
+ if len(rows) == 0:
54
+ return "no messages yet"
55
+ else:
56
+ html = "<div class='chatbot'>"
57
+ for row in rows:
58
+ html += "<div>"
59
+ html += f"<span>{row['name']}</span>"
60
+ html += f"<span class='message'>{row['message']}</span>"
61
+ html += "</div>"
62
+ html += "</div>"
63
+ return html
64
+
65
+
66
+ def store_message(name: str, message: str):
67
+ if name and message:
68
+ with open(DATA_FILE, "a") as csvfile:
69
+ writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
70
+ writer.writerow(
71
+ {"name": name, "message": message, "time": str(datetime.now())}
72
+ )
73
+ commit_url = upload_file(
74
+ DATA_FILE,
75
+ path_in_repo=DATA_FILENAME,
76
+ repo_id=DATASET_REPO_ID,
77
+ token=HF_TOKEN,
78
+ )
79
+
80
+ print(commit_url)
81
+
82
+ return generate_html()
83
+
84
+
85
+ iface = gr.Interface(
86
+ store_message,
87
+ [
88
+ inputs.Textbox(placeholder="Your name"),
89
+ inputs.Textbox(placeholder="Your message", lines=2),
90
+ ],
91
+ "html",
92
+ css="""
93
+ .message {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
94
+ """,
95
+ title="Reading/writing to a HuggingFace dataset repo from Spaces",
96
+ description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
97
+ article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})",
98
+ )
99
+
100
+ iface.launch()