soury commited on
Commit
beee3d3
·
1 Parent(s): e794a4b

fix pb of local files generated

Browse files
app.py CHANGED
@@ -19,21 +19,21 @@ init_huggingface()
19
 
20
 
21
  def handle_submit(*inputs):
22
- message, filename, json_output = generate_json(*inputs)
23
 
24
  # Check if the message indicates validation failure
25
  if message.startswith("The following fields are required"):
26
- return message, filename, json_output
27
 
28
  publish_button = gr.Button(
29
  "Share your data to the public repository", interactive=True, elem_classes="pubbutton")
30
 
31
- return "Report sucessefully created", filename, json_output, publish_button
32
 
33
 
34
- def handle_publi(filename, json_output):
35
  # If validation passed, proceed to update_dataset
36
- update_output = update_dataset(filename, json_output)
37
  return update_output
38
 
39
 
@@ -57,7 +57,7 @@ with gr.Blocks(css_paths=css_path) as app:
57
  submit_button = gr.Button("Submit", elem_classes="subbutton")
58
  output = gr.Textbox(label="Output", lines=1)
59
  json_output = gr.Textbox(visible=False)
60
- filename = gr.File(label="Downloadable JSON")
61
  publish_button = gr.Button(
62
  "Share your data to the public repository", interactive=False, elem_classes="pubbutton")
63
 
@@ -74,13 +74,13 @@ with gr.Blocks(css_paths=css_path) as app:
74
  *environment_components,
75
  *quality_components,
76
  ],
77
- outputs=[output, filename, json_output, publish_button]
78
  )
79
  # Event Handlers
80
  publish_button.click(
81
  handle_publi,
82
  inputs=[
83
- filename, json_output
84
  ],
85
  outputs=[output]
86
  )
 
19
 
20
 
21
  def handle_submit(*inputs):
22
+ message, file_path, json_output = generate_json(*inputs)
23
 
24
  # Check if the message indicates validation failure
25
  if message.startswith("The following fields are required"):
26
+ return message, file_path, json_output
27
 
28
  publish_button = gr.Button(
29
  "Share your data to the public repository", interactive=True, elem_classes="pubbutton")
30
 
31
+ return "Report sucessefully created", file_path, json_output, publish_button
32
 
33
 
34
+ def handle_publi(file_path, json_output):
35
  # If validation passed, proceed to update_dataset
36
+ update_output = update_dataset(file_path, json_output)
37
  return update_output
38
 
39
 
 
57
  submit_button = gr.Button("Submit", elem_classes="subbutton")
58
  output = gr.Textbox(label="Output", lines=1)
59
  json_output = gr.Textbox(visible=False)
60
+ json_file = gr.File(label="Downloadable JSON")
61
  publish_button = gr.Button(
62
  "Share your data to the public repository", interactive=False, elem_classes="pubbutton")
63
 
 
74
  *environment_components,
75
  *quality_components,
76
  ],
77
+ outputs=[output, json_file, json_output, publish_button]
78
  )
79
  # Event Handlers
80
  publish_button.click(
81
  handle_publi,
82
  inputs=[
83
+ json_file, json_output
84
  ],
85
  outputs=[output]
86
  )
src/services/huggingface.py CHANGED
@@ -15,7 +15,7 @@ def init_huggingface():
15
  login(token=HF_TOKEN)
16
 
17
 
18
- def update_dataset(filename, json_data):
19
  """Update the Hugging Face dataset with new data."""
20
 
21
  if json_data is None or json_data.startswith("The following fields are required"):
@@ -25,27 +25,16 @@ def update_dataset(filename, json_data):
25
  init_huggingface()
26
  api = HfApi()
27
 
28
- # to transform the string into a well formatted json structured string
29
- json_dic = json.loads(json_data)
30
- json_str = json.dumps(json_dic, indent=4, ensure_ascii=False)
31
 
32
- # Write JSON to a temporary file with the desired filename
33
- with tempfile.TemporaryDirectory():
34
- with open(filename, "w", encoding="utf-8") as f:
35
- f.write(json_str)
36
-
37
- # Upload the file to the repo with the given filename
38
- # Extract only the filename after the last '/' or '\' (for Windows paths)
39
- short_filename = re.split(r"[\\/]", filename)[-1]
40
  api.upload_file(
41
- path_or_fileobj=filename,
42
  repo_id=DATASET_NAME,
43
  path_in_repo=f"data/{short_filename}",
44
  repo_type="dataset",
45
- commit_message=f"Add new BoAmps report data - {short_filename}",
46
  create_pr=True,
47
  )
48
- # os.unlink(file_path) # Clean up
49
 
50
  except Exception as e:
51
  return f"Error updating dataset: {str(e)}"
 
15
  login(token=HF_TOKEN)
16
 
17
 
18
+ def update_dataset(file_path, json_data):
19
  """Update the Hugging Face dataset with new data."""
20
 
21
  if json_data is None or json_data.startswith("The following fields are required"):
 
25
  init_huggingface()
26
  api = HfApi()
27
 
28
+ short_filename = os.path.basename(file_path)
 
 
29
 
 
 
 
 
 
 
 
 
30
  api.upload_file(
31
+ path_or_fileobj=file_path,
32
  repo_id=DATASET_NAME,
33
  path_in_repo=f"data/{short_filename}",
34
  repo_type="dataset",
35
+ commit_message=f"Add new BoAmps report data: {short_filename}",
36
  create_pr=True,
37
  )
 
38
 
39
  except Exception as e:
40
  return f"Error updating dataset: {str(e)}"
src/services/json_generator.py CHANGED
@@ -3,6 +3,8 @@ import tempfile
3
  from datetime import datetime
4
  import uuid
5
  from assets.utils.validation import validate_obligatory_fields
 
 
6
 
7
 
8
  def generate_json(
@@ -21,7 +23,7 @@ def generate_json(
21
  durationCalibrationMeasurement, powerConsumption,
22
  measurementDuration, measurementDateTime,
23
  # System
24
- os, distribution, distributionVersion,
25
  # Software
26
  language, version_software,
27
  # Infrastructure
@@ -157,8 +159,8 @@ def generate_json(
157
 
158
  # proceed system
159
  system = {}
160
- if os:
161
- system["os"] = os
162
  if distribution:
163
  system["distribution"] = distribution
164
  if distributionVersion:
@@ -227,9 +229,11 @@ def generate_json(
227
  # Create the JSON string
228
  json_str = json.dumps(report, indent=4, ensure_ascii=False)
229
 
230
- # Write JSON to a temporary file with the desired filename
231
- with tempfile.TemporaryDirectory():
232
- with open(filename, "w", encoding="utf-8") as f:
233
- f.write(json_str)
 
234
 
235
- return message, filename, json_str
 
 
3
  from datetime import datetime
4
  import uuid
5
  from assets.utils.validation import validate_obligatory_fields
6
+ import tempfile
7
+ import os
8
 
9
 
10
  def generate_json(
 
23
  durationCalibrationMeasurement, powerConsumption,
24
  measurementDuration, measurementDateTime,
25
  # System
26
+ osystem, distribution, distributionVersion,
27
  # Software
28
  language, version_software,
29
  # Infrastructure
 
159
 
160
  # proceed system
161
  system = {}
162
+ if osystem:
163
+ system["os"] = osystem
164
  if distribution:
165
  system["distribution"] = distribution
166
  if distributionVersion:
 
229
  # Create the JSON string
230
  json_str = json.dumps(report, indent=4, ensure_ascii=False)
231
 
232
+ # Write JSON to a temporary file with the desired filename (not permanent)
233
+ temp_dir = tempfile.gettempdir()
234
+ temp_path = os.path.join(temp_dir, filename)
235
+ with open(temp_path, "w", encoding="utf-8") as tmp:
236
+ tmp.write(json_str)
237
 
238
+ # Return logical filename, JSON string, and temp file path for upload
239
+ return message, temp_path, json_str