Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,16 +12,16 @@ DIRNAME = "data"
|
|
| 12 |
DATA_FILE = os.path.join(DIRNAME, DATA_FILENAME)
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
|
| 15 |
-
# overriding/appending to the gradio template
|
| 16 |
-
SCRIPT = """
|
| 17 |
-
<script>
|
| 18 |
-
if (!window.hasBeenRun) {
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
}
|
| 23 |
-
</script>
|
| 24 |
-
"""
|
| 25 |
|
| 26 |
try:
|
| 27 |
hf_hub_download(
|
|
@@ -36,6 +36,52 @@ repo = Repository(
|
|
| 36 |
local_dir = DIRNAME, clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
| 37 |
)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def generate_html() -> str:
|
| 40 |
with open(DATA_FILE) as csvfile:
|
| 41 |
reader = csv.DictReader(csvfile)
|
|
@@ -49,13 +95,14 @@ def generate_html() -> str:
|
|
| 49 |
html = "<div class='chatbot'>"
|
| 50 |
for row in rows:
|
| 51 |
html += "<div>"
|
| 52 |
-
html += f"<span>{row['name']}</span>"
|
| 53 |
html += f"<span class='message'>{row['message']}</span>"
|
| 54 |
html += "</div>"
|
| 55 |
html += "</div>"
|
| 56 |
return html
|
| 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"])
|
|
@@ -65,20 +112,13 @@ def store_message(name: str, message: str):
|
|
| 65 |
commit_url = repo.push_to_hub()
|
| 66 |
return generate_html()
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
.name {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
|
| 77 |
-
""",
|
| 78 |
-
title="Reading/writing to a HuggingFace dataset repo from Spaces",
|
| 79 |
-
description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
|
| 80 |
-
article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})",
|
| 81 |
-
allow_flagging="never"
|
| 82 |
-
)
|
| 83 |
|
| 84 |
-
|
|
|
|
| 12 |
DATA_FILE = os.path.join(DIRNAME, DATA_FILENAME)
|
| 13 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
|
| 15 |
+
# # overriding/appending to the gradio template
|
| 16 |
+
# SCRIPT = """
|
| 17 |
+
# <script>
|
| 18 |
+
# if (!window.hasBeenRun) {
|
| 19 |
+
# window.hasBeenRun = true;
|
| 20 |
+
# console.log("should only happen once");
|
| 21 |
+
# document.querySelector("button.submit").click();
|
| 22 |
+
# }
|
| 23 |
+
# </script>
|
| 24 |
+
# """
|
| 25 |
|
| 26 |
try:
|
| 27 |
hf_hub_download(
|
|
|
|
| 36 |
local_dir = DIRNAME, clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# def generate_html() -> str:
|
| 40 |
+
# with open(DATA_FILE) as csvfile:
|
| 41 |
+
# reader = csv.DictReader(csvfile)
|
| 42 |
+
# rows = []
|
| 43 |
+
# for row in reader:
|
| 44 |
+
# rows.append(row)
|
| 45 |
+
# rows.reverse()
|
| 46 |
+
# if len(rows) == 0:
|
| 47 |
+
# return "no messages yet"
|
| 48 |
+
# else:
|
| 49 |
+
# html = "<div class='chatbot'>"
|
| 50 |
+
# for row in rows:
|
| 51 |
+
# html += "<div>"
|
| 52 |
+
# html += f"<span>{row['name']}</span>"
|
| 53 |
+
# html += f"<span class='message'>{row['message']}</span>"
|
| 54 |
+
# html += "</div>"
|
| 55 |
+
# html += "</div>"
|
| 56 |
+
# return html
|
| 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 |
+
# return generate_html()
|
| 67 |
+
|
| 68 |
+
# iface = gr.Interface(
|
| 69 |
+
# store_message,
|
| 70 |
+
# [
|
| 71 |
+
# components.Textbox(placeholder="Your name"),
|
| 72 |
+
# components.Textbox(placeholder="Your message", lines=2),
|
| 73 |
+
# ],
|
| 74 |
+
# "html",
|
| 75 |
+
# css="""
|
| 76 |
+
# .name {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
|
| 77 |
+
# """,
|
| 78 |
+
# title="Reading/writing to a HuggingFace dataset repo from Spaces",
|
| 79 |
+
# description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
|
| 80 |
+
# article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})",
|
| 81 |
+
# allow_flagging="never"
|
| 82 |
+
# )
|
| 83 |
+
|
| 84 |
+
# iface.launch()
|
| 85 |
def generate_html() -> str:
|
| 86 |
with open(DATA_FILE) as csvfile:
|
| 87 |
reader = csv.DictReader(csvfile)
|
|
|
|
| 95 |
html = "<div class='chatbot'>"
|
| 96 |
for row in rows:
|
| 97 |
html += "<div>"
|
| 98 |
+
html += f"<span><b>{row['name']}</b></span> " # Make the name bold and add a space after it
|
| 99 |
html += f"<span class='message'>{row['message']}</span>"
|
| 100 |
html += "</div>"
|
| 101 |
html += "</div>"
|
| 102 |
return html
|
| 103 |
+
|
| 104 |
def store_message(name: str, message: str):
|
| 105 |
+
|
| 106 |
if name and message:
|
| 107 |
with open(DATA_FILE, "a") as csvfile:
|
| 108 |
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
|
|
|
| 112 |
commit_url = repo.push_to_hub()
|
| 113 |
return generate_html()
|
| 114 |
|
| 115 |
+
with gr.Blocks() as demo:
|
| 116 |
+
gr.Markdown("# Title")
|
| 117 |
+
name_input = components.Textbox(label="Your Username")
|
| 118 |
+
message_input = components.Textbox(label="Your Feedback", lines=2)
|
| 119 |
+
output_html = gr.HTML()
|
| 120 |
+
submit_button = gr.Button("Submit")
|
| 121 |
+
|
| 122 |
+
submit_button.click(store_message, inputs=[name_input, message_input], outputs=output_html)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
+
demo.launch()
|