Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,86 @@ import streamlit as st
|
|
2 |
from PIL import Image
|
3 |
from components import GamePlay, Player, Dealer, Deck
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Game settings
|
7 |
number_of_decks = 6
|
|
|
2 |
from PIL import Image
|
3 |
from components import GamePlay, Player, Dealer, Deck
|
4 |
|
5 |
+
# PersistDataset -----
|
6 |
+
import os
|
7 |
+
import csv
|
8 |
+
import gradio as gr
|
9 |
+
from gradio import inputs, outputs
|
10 |
+
import huggingface_hub
|
11 |
+
from huggingface_hub import Repository, hf_hub_download, upload_file
|
12 |
+
from datetime import datetime
|
13 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/Carddata.csv"
|
14 |
+
DATASET_REPO_ID = "awacke1/Carddata.csv"
|
15 |
+
DATA_FILENAME = "Carddata.csv"
|
16 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
17 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
18 |
+
# overriding/appending to the gradio template
|
19 |
+
SCRIPT = """
|
20 |
+
<script>
|
21 |
+
if (!window.hasBeenRun) {
|
22 |
+
window.hasBeenRun = true;
|
23 |
+
console.log("should only happen once");
|
24 |
+
document.querySelector("button.submit").click();
|
25 |
+
}
|
26 |
+
</script>
|
27 |
+
"""
|
28 |
+
with open(os.path.join(gr.networking.STATIC_TEMPLATE_LIB, "frontend", "index.html"), "a") as f:
|
29 |
+
f.write(SCRIPT)
|
30 |
+
try:
|
31 |
+
hf_hub_download(
|
32 |
+
repo_id=DATASET_REPO_ID,
|
33 |
+
filename=DATA_FILENAME,
|
34 |
+
cache_dir=DATA_DIRNAME,
|
35 |
+
force_filename=DATA_FILENAME
|
36 |
+
)
|
37 |
+
except:
|
38 |
+
print("file not found")
|
39 |
+
repo = Repository(
|
40 |
+
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
41 |
+
)
|
42 |
+
def generate_html() -> str:
|
43 |
+
with open(DATA_FILE) as csvfile:
|
44 |
+
reader = csv.DictReader(csvfile)
|
45 |
+
rows = []
|
46 |
+
for row in reader:
|
47 |
+
rows.append(row)
|
48 |
+
rows.reverse()
|
49 |
+
if len(rows) == 0:
|
50 |
+
return "no messages yet"
|
51 |
+
else:
|
52 |
+
html = "<div class='chatbot'>"
|
53 |
+
for row in rows:
|
54 |
+
html += "<div>"
|
55 |
+
html += f"<span>{row['name']}</span>"
|
56 |
+
html += f"<span class='message'>{row['message']}</span>"
|
57 |
+
html += "</div>"
|
58 |
+
html += "</div>"
|
59 |
+
return html
|
60 |
+
def store_message(name: str, message: str):
|
61 |
+
if name and message:
|
62 |
+
with open(DATA_FILE, "a") as csvfile:
|
63 |
+
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
64 |
+
writer.writerow(
|
65 |
+
{"name": name, "message": message, "time": str(datetime.now())}
|
66 |
+
)
|
67 |
+
commit_url = repo.push_to_hub()
|
68 |
+
return generate_html()
|
69 |
+
iface = gr.Interface(
|
70 |
+
store_message,
|
71 |
+
[
|
72 |
+
inputs.Textbox(placeholder="Your name"),
|
73 |
+
inputs.Textbox(placeholder="Your message", lines=2),
|
74 |
+
],
|
75 |
+
"html",
|
76 |
+
css="""
|
77 |
+
.message {background-color:cornflowerblue;color:white; padding:4px;margin:4px;border-radius:4px; }
|
78 |
+
""",
|
79 |
+
title="Reading/writing to a HuggingFace dataset repo from Spaces",
|
80 |
+
description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
|
81 |
+
article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})",
|
82 |
+
)
|
83 |
+
iface.launch()
|
84 |
+
# -------
|
85 |
|
86 |
# Game settings
|
87 |
number_of_decks = 6
|