Matthias Kleiner commited on
Commit
9634f2d
β€’
1 Parent(s): 1f0d187

added storing of usage data to hf dataset with commitscheduler

Browse files
Files changed (1) hide show
  1. app.py +56 -7
app.py CHANGED
@@ -1,10 +1,30 @@
1
  from gradio_client import Client
2
  import gradio as gr
3
- import os
4
- import random
5
  import datetime
6
- from huggingface_hub import hf_api
7
- import time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  def check_password(username, password):
10
  if password == os.environ["ACCESS"]:
@@ -12,10 +32,26 @@ def check_password(username, password):
12
  else:
13
  return False
14
 
15
-
16
  def func(file, number_of_pages, secret):
 
17
  if secret != os.environ["ACCESS"]:
18
  return "Wrong password, please try again"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  space_runtime = hf_api.get_space_runtime("ByMatthew/deckify_private", token=read_key)
21
  print(f"Space runtime: {space_runtime}")
@@ -44,6 +80,7 @@ def func(file, number_of_pages, secret):
44
 
45
  print(f"Client: {client}")
46
 
 
47
  output = client.predict(file, number_of_pages)
48
  if "Error" in output:
49
  return output
@@ -52,14 +89,26 @@ def func(file, number_of_pages, secret):
52
  # with open(f"{s}.tex", "w", encoding="utf-8") as f:
53
  # f.write(text)
54
 
55
- date_string = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
56
- temp_string = "% The following slide is generated with [[SCIDECK]](https://huggingface.co/spaces/Nauryzbay/Scideck)"
57
  temp_string += "\n% Generated on " + date_string
58
  temp_string += "\n%" + "-"*100 + "\n"
59
  output = temp_string + output
 
 
 
60
  return output
61
 
 
 
 
 
 
 
 
 
 
62
  def upload_file(file):
 
63
  return file.name
64
 
65
  # πŸ“ If you get an error message, you can send me email with the PDF file attached to this email address: <b>nkoisheke [at] ethz [dot] ch</b>, and I will generate the slides for you. If there are any other issues or questions, please do not hesitate to contact me πŸ€— <br>
 
1
  from gradio_client import Client
2
  import gradio as gr
3
+ import os, uuid, json, random, time
 
4
  import datetime
5
+ from huggingface_hub import hf_api, CommitScheduler, HfApi
6
+ from pathlib import Path
7
+
8
+ feedback_file = Path("output_data/") / f"output_{uuid.uuid4()}.json"
9
+ feedback_folder = feedback_file.parent
10
+
11
+ scheduler = CommitScheduler(
12
+ repo_id="eth-zurich-cle/deckify-dataset",
13
+ repo_type="dataset",
14
+ folder_path=feedback_folder,
15
+ path_in_repo="output_data",
16
+ every=10,
17
+ )
18
+
19
+ # scheduler = CommitScheduler(
20
+ # repo_id="eth-zurich-cle/deckify-dataset",
21
+ # repo_type="dataset",
22
+ # folder_path=feedback_folder,
23
+ # path_in_repo="input_data",
24
+ # every=10,
25
+ # )
26
+
27
+ api = HfApi()
28
 
29
  def check_password(username, password):
30
  if password == os.environ["ACCESS"]:
 
32
  else:
33
  return False
34
 
 
35
  def func(file, number_of_pages, secret):
36
+
37
  if secret != os.environ["ACCESS"]:
38
  return "Wrong password, please try again"
39
+
40
+ date_string = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
41
+
42
+ print(file)
43
+ unique_filename = f"{file.split('.')[0]}_{date_string}.{file.split('.')[-1]}"
44
+
45
+ print(unique_filename)
46
+
47
+ api.upload_file(
48
+ # path_or_fileobj="/path/to/local/folder/README.md",
49
+ path_or_fileobj=file,
50
+ path_in_repo=f"input_files/{unique_filename}",
51
+ repo_id="eth-zurich-cle/deckify-dataset",
52
+ repo_type="dataset",
53
+ )
54
+
55
 
56
  space_runtime = hf_api.get_space_runtime("ByMatthew/deckify_private", token=read_key)
57
  print(f"Space runtime: {space_runtime}")
 
80
 
81
  print(f"Client: {client}")
82
 
83
+
84
  output = client.predict(file, number_of_pages)
85
  if "Error" in output:
86
  return output
 
89
  # with open(f"{s}.tex", "w", encoding="utf-8") as f:
90
  # f.write(text)
91
 
92
+ temp_string = "% The following slides are generated with [[SCIDECK]](https://huggingface.co/spaces/ByMatthew/Scideck)"
 
93
  temp_string += "\n% Generated on " + date_string
94
  temp_string += "\n%" + "-"*100 + "\n"
95
  output = temp_string + output
96
+
97
+ save_output(unique_filename, output, number_of_pages, date_string)
98
+
99
  return output
100
 
101
+ def save_output(unique_filename: str, output: str, num_pages:int, date_string: str) -> None:
102
+
103
+ # Append outputs and using a thread lock to avoid concurrent writes from different users.
104
+ with scheduler.lock:
105
+ with feedback_file.open("a") as f:
106
+ f.write(json.dumps({"input_name": unique_filename, "output": output, "num_pages": num_pages, "timestamp": date_string}))
107
+ f.write("\n")
108
+
109
+
110
  def upload_file(file):
111
+ print(file)
112
  return file.name
113
 
114
  # πŸ“ If you get an error message, you can send me email with the PDF file attached to this email address: <b>nkoisheke [at] ethz [dot] ch</b>, and I will generate the slides for you. If there are any other issues or questions, please do not hesitate to contact me πŸ€— <br>