awacke1 commited on
Commit
0998cd5
ยท
1 Parent(s): 6376c17

Create app.py

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