ccml commited on
Commit
4f944b8
·
1 Parent(s): 38a2159

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -12
app.py CHANGED
@@ -1,17 +1,22 @@
1
  import os
2
  import csv
3
  import gradio as gr
4
- from gradio import inputs, outputs
 
 
5
  import huggingface_hub
6
- from huggingface_hub import Repository
 
7
  from datetime import datetime
8
 
9
- DATASET_REPO_URL = "https://huggingface.co/datasets/ccml/dataset1"
 
 
10
  DATA_FILENAME = "data.csv"
11
- DATA_FILE = os.path.join("data", DATA_FILENAME)
12
 
13
  HF_TOKEN = os.environ.get("HF_TOKEN")
14
- print("is none?", HF_TOKEN is None)
15
 
16
  print("hfh", huggingface_hub.__version__)
17
 
@@ -29,10 +34,16 @@ with open(os.path.join(gr.networking.STATIC_TEMPLATE_LIB, "frontend", "index.htm
29
  f.write(SCRIPT)
30
 
31
 
32
-
33
- repo = Repository(
34
- local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
35
- )
 
 
 
 
 
 
36
 
37
 
38
  def generate_html() -> str:
@@ -62,7 +73,13 @@ def store_message(name: str, message: str):
62
  writer.writerow(
63
  {"name": name, "message": message, "time": str(datetime.now())}
64
  )
65
- commit_url = repo.push_to_hub()
 
 
 
 
 
 
66
  print(commit_url)
67
 
68
  return generate_html()
@@ -80,7 +97,7 @@ iface = gr.Interface(
80
  """,
81
  title="Reading/writing to a HuggingFace dataset repo from Spaces",
82
  description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
83
- article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL}) (open in new tab)",
84
  )
85
 
86
- iface.launch()
 
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/ccml/dataset1"
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
 
 
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:
 
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()
 
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()