mojakob commited on
Commit
478321c
·
verified ·
1 Parent(s): c9a6315

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -61
app.py CHANGED
@@ -56,56 +56,6 @@ def create_excel_if_missing(filename: str):
56
  print(f"Created empty Excel file: {filename}")
57
 
58
 
59
- # def save_to_excel(df, user_state):
60
- # """
61
- # Saves the user's data into a unique Excel file named:
62
- # Cond3_<ProlificID>_<timestamp>.xlsx
63
- # If a file with that name already exists (e.g., user
64
- # revisits or calls this function multiple times quickly),
65
- # we merge the new data with the existing file.
66
- # Afterwards, uploads the file to Hugging Face under the same unique name.
67
- # """
68
- # hf_token = os.getenv("tiliah_write3")
69
-
70
- # dataset_repo_id = "Tiliah/user_data"
71
-
72
- # # 1) Generate a unique filename from Prolific ID and timestamp
73
- # local_file = user_state.get("excel_file")
74
-
75
- # try:
76
- # with file_lock:
77
- # # 2) Merge existing data if the same file name exists
78
- # if os.path.exists(local_file):
79
- # existing = pd.read_excel(local_file, engine="openpyxl")
80
- # merged = pd.concat([existing, df], ignore_index=True)
81
- # else:
82
- # merged = df
83
-
84
- # # 3) Save locally
85
- # merged.to_excel(local_file, index=False, engine="openpyxl")
86
-
87
- # # 4) Upload to Hugging Face
88
- # if hf_token and dataset_repo_id:
89
- # api = HfApi()
90
- # path_in_repo = f"Cond3/{local_file}"
91
- # future_upload = api.upload_file(
92
- # path_or_fileobj=local_file,
93
- # path_in_repo=path_in_repo, # Keep the same name in the repo
94
- # repo_id=dataset_repo_id,
95
- # repo_type="dataset",
96
- # token=hf_token,
97
- # run_as_future=True
98
- # )
99
- # future_upload
100
- # future_upload.result()
101
- # print(f"Uploaded {local_file} to HF dataset.")
102
- # else:
103
- # print("No HF token or dataset repo provided, skipping HF upload.")
104
-
105
- # except Exception as e:
106
- # print(f"Error in save_cond3_data_in_unique_file: {e}")
107
- # raise
108
-
109
  def save_to_excel(df, user_state):
110
  """
111
  Saves the user's data into a unique Excel file named:
@@ -116,6 +66,7 @@ def save_to_excel(df, user_state):
116
  Afterwards, uploads the file to an S3-compatible bucket
117
  (e.g., Hetzner) under "Cond3/<filename>".
118
  """
 
119
  # 1) Grab the unique local filename
120
  local_file = user_state.get("excel_file")
121
  if not local_file:
@@ -126,7 +77,6 @@ def save_to_excel(df, user_state):
126
  s3_access_key = os.getenv("HETZNER_ACCESS_KEY_ID")
127
  s3_secret_key = os.getenv("HETZNER_SECRET_ACCESS_KEY")
128
  s3_endpoint = os.getenv("HETZNER_S3_ENDPOINT")
129
- # s3_region = os.getenv("HETZNER_S3_REGION")
130
  bucket_name = os.getenv("HETZNER_BUCKET_NAME")
131
 
132
  try:
@@ -145,26 +95,23 @@ def save_to_excel(df, user_state):
145
 
146
  # 5) Upload to S3 only if credentials/bucket are present
147
  if s3_access_key and s3_secret_key and s3_endpoint and bucket_name:
148
- my_config = Config(signature_version='s3v4')
149
-
 
 
150
  s3_client = boto3.client(
151
  "s3",
152
  aws_access_key_id=s3_access_key,
153
  aws_secret_access_key=s3_secret_key,
154
- # region_name=s3_region,
155
  endpoint_url=s3_endpoint,
156
  config=my_config
157
  )
158
 
159
- # We'll store it in a 'Cond3' subfolder
160
- s3_key = f"Cond3/{local_file}"
161
 
162
  # 6) Upload the file
163
- s3_client.upload_file(
164
- local_file,
165
- bucket_name,
166
- s3_key
167
- )
168
  print(f"Uploaded {local_file} to S3 bucket '{bucket_name}' at key: {s3_key}")
169
  else:
170
  print("No S3 credentials or bucket provided, skipping S3 upload.")
 
56
  print(f"Created empty Excel file: {filename}")
57
 
58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  def save_to_excel(df, user_state):
60
  """
61
  Saves the user's data into a unique Excel file named:
 
66
  Afterwards, uploads the file to an S3-compatible bucket
67
  (e.g., Hetzner) under "Cond3/<filename>".
68
  """
69
+
70
  # 1) Grab the unique local filename
71
  local_file = user_state.get("excel_file")
72
  if not local_file:
 
77
  s3_access_key = os.getenv("HETZNER_ACCESS_KEY_ID")
78
  s3_secret_key = os.getenv("HETZNER_SECRET_ACCESS_KEY")
79
  s3_endpoint = os.getenv("HETZNER_S3_ENDPOINT")
 
80
  bucket_name = os.getenv("HETZNER_BUCKET_NAME")
81
 
82
  try:
 
95
 
96
  # 5) Upload to S3 only if credentials/bucket are present
97
  if s3_access_key and s3_secret_key and s3_endpoint and bucket_name:
98
+ my_config = Config(
99
+ signature_version="s3v4",
100
+ s3={"addressing_style": "path"}
101
+ )
102
  s3_client = boto3.client(
103
  "s3",
104
  aws_access_key_id=s3_access_key,
105
  aws_secret_access_key=s3_secret_key,
 
106
  endpoint_url=s3_endpoint,
107
  config=my_config
108
  )
109
 
110
+ # We'll store it in a 'Cond3' subfolder, using the basename to avoid unwanted path details
111
+ s3_key = f"Cond3/{os.path.basename(local_file)}"
112
 
113
  # 6) Upload the file
114
+ s3_client.upload_file(local_file, bucket_name, s3_key)
 
 
 
 
115
  print(f"Uploaded {local_file} to S3 bucket '{bucket_name}' at key: {s3_key}")
116
  else:
117
  print("No S3 credentials or bucket provided, skipping S3 upload.")