Abhishek Thakur commited on
Commit
6a5164b
·
1 Parent(s): d3e4a05

creator work

Browse files
Files changed (2) hide show
  1. competitions/__init__.py +1 -0
  2. competitions/create.py +182 -5
competitions/__init__.py CHANGED
@@ -10,5 +10,6 @@ COMPETITION_ID = os.getenv("COMPETITION_ID")
10
  AUTOTRAIN_USERNAME = os.getenv("AUTOTRAIN_USERNAME")
11
  AUTOTRAIN_TOKEN = os.getenv("AUTOTRAIN_TOKEN")
12
  AUTOTRAIN_BACKEND_API = os.getenv("AUTOTRAIN_BACKEND_API", "https://api.autotrain.huggingface.co")
 
13
 
14
  competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=AUTOTRAIN_TOKEN)
 
10
  AUTOTRAIN_USERNAME = os.getenv("AUTOTRAIN_USERNAME")
11
  AUTOTRAIN_TOKEN = os.getenv("AUTOTRAIN_TOKEN")
12
  AUTOTRAIN_BACKEND_API = os.getenv("AUTOTRAIN_BACKEND_API", "https://api.autotrain.huggingface.co")
13
+ BOT_TOKEN = os.getenv("BOT_TOKEN")
14
 
15
  competition_info = CompetitionInfo(competition_id=COMPETITION_ID, autotrain_token=AUTOTRAIN_TOKEN)
competitions/create.py CHANGED
@@ -1,8 +1,12 @@
 
 
1
  import uuid
2
 
3
  import gradio as gr
4
  import pandas as pd
 
5
 
 
6
  from .utils import user_authentication
7
 
8
 
@@ -36,11 +40,11 @@ def create_competition(
36
  ):
37
  # generate a random id
38
  suffix = str(uuid.uuid4())
39
- private_dataset_name = f"{competition_name}{suffix}"
40
- public_dataset_name = f"{competition_name}"
41
- space_name = f"{competition_name}"
42
 
43
- sample_submission_df = pd.read_csv(sample_submission_file, nrows=10)
44
  submission_columns = ",".join(sample_submission_df.columns)
45
 
46
  conf = {
@@ -49,11 +53,183 @@ def create_competition(
49
  "END_DATE": end_date,
50
  "EVAL_HIGHER_IS_BETTER": True,
51
  "COMPETITION_NAME": competition_name,
 
52
  "SUBMISSION_COLUMNS": submission_columns,
 
53
  "EVAL_METRIC": eval_metric,
54
  }
55
 
56
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
 
59
  def check_if_user_can_create_competition(user_token):
@@ -201,4 +377,5 @@ with gr.Blocks() as demo:
201
  sample_submission_file,
202
  solution_file,
203
  ]
 
204
  create_button.click(create_competition, inputs=create_inputs, outputs=[message_box])
 
1
+ import io
2
+ import json
3
  import uuid
4
 
5
  import gradio as gr
6
  import pandas as pd
7
+ from huggingface_hub import HfApi, create_repo
8
 
9
+ from . import BOT_TOKEN
10
  from .utils import user_authentication
11
 
12
 
 
40
  ):
41
  # generate a random id
42
  suffix = str(uuid.uuid4())
43
+ private_dataset_name = f"{who_pays}/{competition_name}{suffix}"
44
+ public_dataset_name = f"{who_pays}/{competition_name}"
45
+ space_name = f"competitions/{competition_name}"
46
 
47
+ sample_submission_df = pd.read_csv(sample_submission_file.name, nrows=10)
48
  submission_columns = ",".join(sample_submission_df.columns)
49
 
50
  conf = {
 
53
  "END_DATE": end_date,
54
  "EVAL_HIGHER_IS_BETTER": True,
55
  "COMPETITION_NAME": competition_name,
56
+ "SUBMISSION_ID_COLUMN": "id",
57
  "SUBMISSION_COLUMNS": submission_columns,
58
+ "SUBMISSION_ROWS": len(sample_submission_df),
59
  "EVAL_METRIC": eval_metric,
60
  }
61
 
62
+ api = HfApi()
63
+
64
+ # create private dataset repo
65
+ create_repo(
66
+ repo_id=private_dataset_name,
67
+ repo_type="dataset",
68
+ private=True,
69
+ token=user_token,
70
+ exist_ok=False,
71
+ )
72
+ competition_desc = f"""
73
+ # Welcome to {competition_name}
74
+
75
+ This is a competition description.
76
+
77
+ You can use markdown to format your description.
78
+ """
79
+
80
+ dataset_desc = f"""
81
+ # Dataset Description
82
+
83
+ This is a dataset description.
84
+
85
+ You can use markdown to format your description.
86
+
87
+ Dataset can be downloaded from [here](https://hf.co/datasets/{public_dataset_name})
88
+ """
89
+
90
+ conf_json = json.dumps(conf)
91
+ conf_bytes = conf_json.encode("utf-8")
92
+ conf_buffer = io.BytesIO(conf_bytes)
93
+
94
+ api.upload_file(
95
+ path_or_fileobj=conf_buffer,
96
+ path_in_repo="conf.json",
97
+ repo_id=private_dataset_name,
98
+ repo_type="dataset",
99
+ token=user_token,
100
+ )
101
+
102
+ # convert competition description to bytes
103
+ competition_desc_bytes = competition_desc.encode("utf-8")
104
+ competition_desc_buffer = io.BytesIO(competition_desc_bytes)
105
+
106
+ api.upload_file(
107
+ path_or_fileobj=competition_desc_buffer,
108
+ path_in_repo="COMPETITION_DESC.md",
109
+ repo_id=private_dataset_name,
110
+ repo_type="dataset",
111
+ token=user_token,
112
+ )
113
+
114
+ # convert dataset description to bytes
115
+ dataset_desc_bytes = dataset_desc.encode("utf-8")
116
+ dataset_desc_buffer = io.BytesIO(dataset_desc_bytes)
117
+
118
+ api.upload_file(
119
+ path_or_fileobj=dataset_desc_buffer,
120
+ path_in_repo="DATASET_DESC.md",
121
+ repo_id=private_dataset_name,
122
+ repo_type="dataset",
123
+ token=user_token,
124
+ )
125
+
126
+ if solution_file is not None:
127
+
128
+ with open(solution_file.name, "rb") as f:
129
+ solution_bytes_data = f.read()
130
+ # upload solution file
131
+ api.upload_file(
132
+ path_or_fileobj=solution_bytes_data,
133
+ path_in_repo="solution.csv",
134
+ repo_id=private_dataset_name,
135
+ repo_type="dataset",
136
+ token=user_token,
137
+ )
138
+
139
+ # create public dataset repo
140
+ create_repo(
141
+ repo_id=public_dataset_name,
142
+ repo_type="dataset",
143
+ private=False,
144
+ token=user_token,
145
+ exist_ok=False,
146
+ )
147
+ if sample_submission_file is not None:
148
+ # upload sample submission file
149
+ with open(sample_submission_file.name, "rb") as f:
150
+ sample_submission_bytes_data = f.read()
151
+
152
+ api.upload_file(
153
+ path_or_fileobj=sample_submission_bytes_data,
154
+ path_in_repo="sample_submission.csv",
155
+ repo_id=public_dataset_name,
156
+ repo_type="dataset",
157
+ token=user_token,
158
+ )
159
+
160
+ dockerfile = """
161
+ FROM huggingface/competitions:latest
162
+ CMD competitions run
163
+ """
164
+ dockerfile = dockerfile.strip()
165
+ dockerfile = dockerfile.replace(" ", "")
166
+
167
+ # create competition space
168
+ create_repo(
169
+ repo_id=space_name,
170
+ repo_type="space",
171
+ private=False,
172
+ token=BOT_TOKEN,
173
+ space_sdk="docker",
174
+ exist_ok=False,
175
+ )
176
+
177
+ # upload dockerfile
178
+ dockerfile_bytes = dockerfile.encode("utf-8")
179
+ dockerfile_buffer = io.BytesIO(dockerfile_bytes)
180
+
181
+ api.upload_file(
182
+ path_or_fileobj=dockerfile_buffer,
183
+ path_in_repo="Dockerfile",
184
+ repo_id=space_name,
185
+ repo_type="space",
186
+ token=BOT_TOKEN,
187
+ )
188
+
189
+ space_readme = f"""
190
+ ---
191
+ title: {competition_name}
192
+ emoji: 🏆
193
+ colorFrom: blue
194
+ colorTo: gray
195
+ sdk: docker
196
+ pinned: false
197
+ ---
198
+ """
199
+ space_readme = space_readme.strip()
200
+ space_readme = space_readme.replace(" ", "")
201
+ print(repr(space_readme))
202
+
203
+ # upload space readme
204
+ space_readme_bytes = space_readme.encode("utf-8")
205
+ space_readme_buffer = io.BytesIO(space_readme_bytes)
206
+
207
+ api.upload_file(
208
+ path_or_fileobj=space_readme_buffer,
209
+ path_in_repo="README.md",
210
+ repo_id=space_name,
211
+ repo_type="space",
212
+ token=BOT_TOKEN,
213
+ )
214
+
215
+ api.add_space_secret(
216
+ repo_id=space_name,
217
+ key="COMPETITION_ID",
218
+ value=private_dataset_name,
219
+ token=BOT_TOKEN,
220
+ )
221
+ api.add_space_secret(
222
+ repo_id=space_name,
223
+ key="AUTOTRAIN_USERNAME",
224
+ value=who_pays,
225
+ token=BOT_TOKEN,
226
+ )
227
+ api.add_space_secret(
228
+ repo_id=space_name,
229
+ key="AUTOTRAIN_TOKEN",
230
+ value=user_token,
231
+ token=BOT_TOKEN,
232
+ )
233
 
234
 
235
  def check_if_user_can_create_competition(user_token):
 
377
  sample_submission_file,
378
  solution_file,
379
  ]
380
+ print(create_inputs)
381
  create_button.click(create_competition, inputs=create_inputs, outputs=[message_box])