Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -65,27 +65,11 @@ import urllib.request
|
|
65 |
import gradio as gr
|
66 |
from gradio import inputs, outputs
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
# HF_TOKEN = os.environ.get("HF_TOKEN")
|
75 |
-
# st.write("is none?", HF_TOKEN)
|
76 |
-
|
77 |
-
# st.write("hfh", huggingface_hub.__version__)
|
78 |
-
|
79 |
-
# repo = Repository(
|
80 |
-
# local_dir="level2", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
81 |
-
# )
|
82 |
-
# scheduler = CommitScheduler(
|
83 |
-
# repo_id="Visualization",
|
84 |
-
# repo_type="dataset",
|
85 |
-
# folder_path=feedback_folder,
|
86 |
-
# path_in_repo="level2",
|
87 |
-
# every=10,
|
88 |
-
# )
|
89 |
|
90 |
def main():
|
91 |
|
@@ -557,7 +541,72 @@ def main():
|
|
557 |
with open('detailedResults.json','w') as fi:
|
558 |
#data = json.load(fi)
|
559 |
fi.write(json_data)
|
560 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
561 |
def convert_df(df):
|
562 |
|
563 |
#IMPORTANT: Cache the conversion to prevent computation on every rerun
|
|
|
65 |
import gradio as gr
|
66 |
from gradio import inputs, outputs
|
67 |
|
68 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/Seetha/Visualization/level2.json"
|
69 |
+
DATASET_REPO_ID = "Seetha/Visualizaation/level2.json"
|
70 |
+
DATA_FILENAME = "level2.json"
|
71 |
+
DATA_FILE = os.path.join("level2", DATA_FILENAME)
|
72 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
def main():
|
75 |
|
|
|
541 |
with open('detailedResults.json','w') as fi:
|
542 |
#data = json.load(fi)
|
543 |
fi.write(json_data)
|
544 |
+
|
545 |
+
# overriding/appending to the gradio template
|
546 |
+
SCRIPT = """
|
547 |
+
<script>
|
548 |
+
if (!window.hasBeenRun) {
|
549 |
+
window.hasBeenRun = true;
|
550 |
+
console.log("should only happen once");
|
551 |
+
document.querySelector("button.submit").click();
|
552 |
+
}
|
553 |
+
</script>
|
554 |
+
"""
|
555 |
+
with open(os.path.join(gr.networking.STATIC_TEMPLATE_LIB, "frontend", "index.html"), "a") as f:
|
556 |
+
f.write(SCRIPT)
|
557 |
+
|
558 |
+
try:
|
559 |
+
hf_hub_download(
|
560 |
+
repo_id=DATASET_REPO_ID,
|
561 |
+
filename=DATA_FILENAME,
|
562 |
+
cache_dir=DATA_DIRNAME,
|
563 |
+
force_filename=DATA_FILENAME
|
564 |
+
)
|
565 |
+
except:
|
566 |
+
st.write("file not found")
|
567 |
+
|
568 |
+
repo = Repository(
|
569 |
+
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
570 |
+
)
|
571 |
+
|
572 |
+
def generate_html() -> str:
|
573 |
+
with open(DATA_FILE) as csvfile:
|
574 |
+
reader = json.dumps(json_data)
|
575 |
+
rows = []
|
576 |
+
for row in reader:
|
577 |
+
rows.append(row)
|
578 |
+
rows.reverse()
|
579 |
+
if len(rows) == 0:
|
580 |
+
return "no messages yet"
|
581 |
+
else:
|
582 |
+
html = "<div class='chatbot'>"
|
583 |
+
for row in rows:
|
584 |
+
html += "<div>"
|
585 |
+
html += f"<span>{row['name']}</span>"
|
586 |
+
html += f"<span class='message'>{row['message']}</span>"
|
587 |
+
html += "</div>"
|
588 |
+
html += "</div>"
|
589 |
+
return html
|
590 |
+
|
591 |
+
def store_message():
|
592 |
+
if name and message:
|
593 |
+
with open(DATA_FILE, "a") as csvfile:
|
594 |
+
writer = json.dumps(json_data)
|
595 |
+
commit_url = repo.push_to_hub()
|
596 |
+
return generate_html()
|
597 |
+
|
598 |
+
iface = gr.Interface(
|
599 |
+
store_message,
|
600 |
+
"html",
|
601 |
+
css="""
|
602 |
+
.message {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
|
603 |
+
""",
|
604 |
+
title="Reading/writing to a HuggingFace dataset repo from Spaces",
|
605 |
+
description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
|
606 |
+
article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})",
|
607 |
+
)
|
608 |
+
|
609 |
+
iface.launch()
|
610 |
def convert_df(df):
|
611 |
|
612 |
#IMPORTANT: Cache the conversion to prevent computation on every rerun
|