awacke1 commited on
Commit
9416d62
ยท
1 Parent(s): 9d63078

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -7
app.py CHANGED
@@ -3,13 +3,16 @@ 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/awacke1/data.csv"
10
- #DATA_FILENAME = "data.csv"
11
- #DATA_FILE = os.path.join("data", DATA_FILENAME)
12
- DATA_FILE = DATASET_REPO_URL
 
13
 
14
  HF_TOKEN = os.environ.get("HF_TOKEN")
15
  print("is none?", HF_TOKEN is None)
@@ -30,6 +33,16 @@ with open(os.path.join(gr.networking.STATIC_TEMPLATE_LIB, "frontend", "index.htm
30
  f.write(SCRIPT)
31
 
32
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  repo = Repository(
35
  local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
@@ -63,7 +76,13 @@ def store_message(name: str, message: str):
63
  writer.writerow(
64
  {"name": name, "message": message, "time": str(datetime.now())}
65
  )
66
- commit_url = repo.push_to_hub()
 
 
 
 
 
 
67
  print(commit_url)
68
 
69
  return generate_html()
@@ -81,7 +100,7 @@ iface = gr.Interface(
81
  """,
82
  title="Reading/writing to a HuggingFace dataset repo from Spaces",
83
  description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
84
- article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL}) (open in new tab)",
85
  )
86
 
87
- iface.launch()
 
3
  import gradio as gr
4
  from gradio import inputs, outputs
5
  import huggingface_hub
6
+ from huggingface_hub import Repository, hf_hub_download, upload_file
7
  from datetime import datetime
8
 
9
+ print("hfh", huggingface_hub.__version__)
10
+
11
  DATASET_REPO_URL = "https://huggingface.co/datasets/awacke1/data.csv"
12
+ DATA_FILENAME = "data.csv"
13
+ #DATA_FILE = os.path.join(DATA_DIRNAME, DATA_FILENAME)
14
+ #DATA_DIRNAME = "data"
15
+ DATA_FILE = os.path.join("data", DATA_FILENAME)
16
 
17
  HF_TOKEN = os.environ.get("HF_TOKEN")
18
  print("is none?", HF_TOKEN is None)
 
33
  f.write(SCRIPT)
34
 
35
 
36
+ try:
37
+ hf_hub_download(
38
+ repo_id=DATASET_REPO_ID,
39
+ filename=DATA_FILENAME,
40
+ cache_dir=DATA_DIRNAME,
41
+ force_filename=DATA_FILENAME
42
+ )
43
+ except:
44
+ # file not found, we'll create it on the fly.
45
+ print("file not found, probably")
46
 
47
  repo = Repository(
48
  local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
 
76
  writer.writerow(
77
  {"name": name, "message": message, "time": str(datetime.now())}
78
  )
79
+ commit_url = upload_file(
80
+ DATA_FILE,
81
+ path_in_repo=DATA_FILENAME,
82
+ repo_id=DATASET_REPO_ID,
83
+ token=HF_TOKEN,
84
+ )
85
+
86
  print(commit_url)
87
 
88
  return generate_html()
 
100
  """,
101
  title="Reading/writing to a HuggingFace dataset repo from Spaces",
102
  description=f"This is a demo of how to do simple *shared data persistence* in a Gradio Space, backed by a dataset repo.",
103
+ article=f"The dataset repo is [{DATASET_REPO_URL}]({DATASET_REPO_URL})",
104
  )
105
 
106
+ iface.launch()