awacke1 commited on
Commit
2b51f30
Β·
1 Parent(s): 8e5e4f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -2
app.py CHANGED
@@ -13,7 +13,98 @@ Article = Article + " Originals go here: https://github.com/AaronCWacker/Yggdras
13
  # Aaron_Wacker_health_and_medical_icon_set_on_white_background_bba24b60-9fcf-411b-9c00-dd1ba1e3553c.png
14
 
15
 
16
- def fake_gan():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  images = [
18
  (random.choice(
19
  [
@@ -61,6 +152,10 @@ def fake_gan():
61
 
62
  with gr.Blocks() as demo:
63
  with gr.Column(variant="panel"):
 
 
 
 
64
  with gr.Row(variant="compact"):
65
  text = gr.Textbox(
66
  label="Health and Medical Icon Sets",
@@ -76,7 +171,7 @@ with gr.Blocks() as demo:
76
  label="Generated images", show_label=False, elem_id="gallery"
77
  ).style(grid=[2], height="auto")
78
 
79
- btn.click(fake_gan, None, gallery)
80
 
81
  if __name__ == "__main__":
82
  demo.launch()
 
13
  # Aaron_Wacker_health_and_medical_icon_set_on_white_background_bba24b60-9fcf-411b-9c00-dd1ba1e3553c.png
14
 
15
 
16
+ import os
17
+ import csv
18
+ import gradio as gr
19
+ from gradio import inputs, outputs
20
+ import huggingface_hub
21
+ from huggingface_hub import Repository
22
+ from datetime import datetime
23
+
24
+ DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/BookComposer"
25
+ DATA_FILENAME = "BookComposer.csv"
26
+ DATA_FILE = os.path.join("data", DATA_FILENAME)
27
+
28
+ HF_TOKEN = os.environ.get("HF_TOKEN")
29
+ print("is none?", HF_TOKEN is None)
30
+
31
+ print("hfh", huggingface_hub.__version__)
32
+
33
+ # overriding/appending to the gradio template
34
+ SCRIPT = """
35
+ <script>
36
+ if (!window.hasBeenRun) {
37
+ window.hasBeenRun = true;
38
+ console.log("should only happen once");
39
+ document.querySelector("button.submit").click();
40
+ }
41
+ </script>
42
+ """
43
+ with open(os.path.join(gr.networking.STATIC_TEMPLATE_LIB, "frontend", "index.html"), "a") as f:
44
+ f.write(SCRIPT)
45
+
46
+
47
+
48
+ repo = Repository(
49
+ local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
50
+ )
51
+
52
+
53
+ def generate_html() -> str:
54
+ with open(DATA_FILE) as csvfile:
55
+ reader = csv.DictReader(csvfile)
56
+ rows = []
57
+ for row in reader:
58
+ rows.append(row)
59
+ rows.reverse()
60
+ if len(rows) == 0:
61
+ return "no messages yet"
62
+ else:
63
+ html = "<div class='chatbot'>"
64
+ for row in rows:
65
+ html += "<div>"
66
+ html += f"<span>{row['name']}</span>"
67
+ html += f"<span class='message'>{row['message']}</span>"
68
+ html += "</div>"
69
+ html += "</div>"
70
+ return html
71
+
72
+
73
+ def store_message(name: str, message: str):
74
+ if name and message:
75
+ with open(DATA_FILE, "a") as csvfile:
76
+ writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
77
+ writer.writerow(
78
+ {"name": name, "message": message, "time": str(datetime.now())}
79
+ )
80
+ commit_url = repo.push_to_hub()
81
+ print(commit_url)
82
+
83
+ return generate_html()
84
+
85
+
86
+ iface = gr.Interface(
87
+ store_message,
88
+ [
89
+ inputs.Textbox(placeholder="Your name"),
90
+ inputs.Textbox(placeholder="Your message", lines=2),
91
+ ],
92
+ "html",
93
+ css="""
94
+ .message {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
95
+ """,
96
+ title="Reading/writing to a HuggingFace dataset repo from Spaces",
97
+ description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
98
+ article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL}) (open in new tab)",
99
+ )
100
+
101
+ iface.launch()
102
+
103
+
104
+
105
+ def gan():
106
+ r=store_message()
107
+
108
  images = [
109
  (random.choice(
110
  [
 
152
 
153
  with gr.Blocks() as demo:
154
  with gr.Column(variant="panel"):
155
+ [
156
+ inputs.Textbox(placeholder="Your name"),
157
+ inputs.Textbox(placeholder="Your message", lines=2),
158
+ ]
159
  with gr.Row(variant="compact"):
160
  text = gr.Textbox(
161
  label="Health and Medical Icon Sets",
 
171
  label="Generated images", show_label=False, elem_id="gallery"
172
  ).style(grid=[2], height="auto")
173
 
174
+ btn.click(gan, None, gallery)
175
 
176
  if __name__ == "__main__":
177
  demo.launch()