Fred808 commited on
Commit
ffc809a
·
verified ·
1 Parent(s): 9c6f43e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -20
app.py CHANGED
@@ -21,10 +21,10 @@ if not HF_TOKEN:
21
  raise ValueError("HUGGINGFACE_TOKEN environment variable is not set")
22
 
23
  # Hardcoded model repository ID
24
- HARDCODED_MODEL_REPO_ID = "openai/gpt-oss-20b" # Change this to your desired model
25
 
26
  # Hardcoded dataset repository ID
27
- HARDCODED_DATASET_REPO_ID = "Fred808/helium_memory" # Change this to your dataset
28
 
29
  # Hardcoded path in repository
30
  HARDCODED_PATH_IN_REPO = "model_data/"
@@ -52,7 +52,7 @@ def download_model_files() -> str:
52
 
53
  print(f"Found {len(files)} files to download")
54
 
55
- # Download each file
56
  for file_path in files:
57
  if file_path.endswith('/'): # Skip directories
58
  continue
@@ -63,22 +63,26 @@ def download_model_files() -> str:
63
  file_dir = os.path.join(temp_dir, os.path.dirname(file_path))
64
  os.makedirs(file_dir, exist_ok=True)
65
 
66
- # Download the file
67
  local_path = hf_hub_download(
68
  repo_id=HARDCODED_MODEL_REPO_ID,
69
  filename=file_path,
70
- cache_dir=temp_dir,
71
  force_download=True,
72
- resume_download=False,
73
  token=HF_TOKEN
74
  )
75
 
76
- # Move from cache to the desired location
77
- final_path = os.path.join(temp_dir, file_path)
78
- if local_path != final_path:
79
- shutil.move(local_path, final_path)
 
 
 
 
80
 
81
  print(f"All files downloaded to: {temp_dir}")
 
82
  return temp_dir
83
 
84
  except Exception as e:
@@ -91,14 +95,28 @@ def upload_folder_to_dataset(folder_path: str):
91
  """Uploads a folder to the hardcoded Hugging Face dataset repository."""
92
  api = HfApi(token=HF_TOKEN)
93
  print(f"Uploading {folder_path} to {HARDCODED_DATASET_REPO_ID} at {HARDCODED_PATH_IN_REPO}...")
 
 
 
 
94
  try:
95
- api.upload_folder(
96
- folder_path=folder_path,
97
- path_in_repo=HARDCODED_PATH_IN_REPO,
98
- repo_id=HARDCODED_DATASET_REPO_ID,
99
- repo_type="dataset",
100
- token=HF_TOKEN
101
- )
 
 
 
 
 
 
 
 
 
 
102
  print("Upload complete!")
103
  except Exception as e:
104
  print(f"Upload failed: {str(e)}")
@@ -129,7 +147,7 @@ async def run_transfer():
129
  if not os.listdir(temp_dir):
130
  raise Exception("No files were downloaded")
131
 
132
- print(f"Downloaded files: {os.listdir(temp_dir)}")
133
 
134
  # Upload to dataset
135
  await loop.run_in_executor(
@@ -145,7 +163,6 @@ async def run_transfer():
145
  error_msg = f"Transfer failed: {str(e)}"
146
  print(error_msg)
147
  transfer_error = error_msg
148
- raise
149
  finally:
150
  # Clean up downloaded files
151
  if temp_dir:
@@ -185,4 +202,4 @@ async def startup_event():
185
  if __name__ == "__main__":
186
  import uvicorn
187
  # Run the server indefinitely
188
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
21
  raise ValueError("HUGGINGFACE_TOKEN environment variable is not set")
22
 
23
  # Hardcoded model repository ID
24
+ HARDCODED_MODEL_REPO_ID = "openai/gpt-oss-20b" # Your model
25
 
26
  # Hardcoded dataset repository ID
27
+ HARDCODED_DATASET_REPO_ID = "Fred808/helium_memory" # Your dataset
28
 
29
  # Hardcoded path in repository
30
  HARDCODED_PATH_IN_REPO = "model_data/"
 
52
 
53
  print(f"Found {len(files)} files to download")
54
 
55
+ # Download each file directly without using cache
56
  for file_path in files:
57
  if file_path.endswith('/'): # Skip directories
58
  continue
 
63
  file_dir = os.path.join(temp_dir, os.path.dirname(file_path))
64
  os.makedirs(file_dir, exist_ok=True)
65
 
66
+ # Download the file directly to the target location
67
  local_path = hf_hub_download(
68
  repo_id=HARDCODED_MODEL_REPO_ID,
69
  filename=file_path,
70
+ local_dir=temp_dir,
71
  force_download=True,
 
72
  token=HF_TOKEN
73
  )
74
 
75
+ print(f"Downloaded to: {local_path}")
76
+
77
+ # Remove any cache directories that might have been created
78
+ for item in os.listdir(temp_dir):
79
+ item_path = os.path.join(temp_dir, item)
80
+ if item.startswith('models--') and os.path.isdir(item_path):
81
+ shutil.rmtree(item_path)
82
+ print(f"Removed cache directory: {item_path}")
83
 
84
  print(f"All files downloaded to: {temp_dir}")
85
+ print(f"Final directory contents: {os.listdir(temp_dir)}")
86
  return temp_dir
87
 
88
  except Exception as e:
 
95
  """Uploads a folder to the hardcoded Hugging Face dataset repository."""
96
  api = HfApi(token=HF_TOKEN)
97
  print(f"Uploading {folder_path} to {HARDCODED_DATASET_REPO_ID} at {HARDCODED_PATH_IN_REPO}...")
98
+
99
+ # Check what we're actually trying to upload
100
+ print(f"Folder contents: {os.listdir(folder_path)}")
101
+
102
  try:
103
+ # Upload each file individually to avoid cache issues
104
+ for root, dirs, files in os.walk(folder_path):
105
+ for file in files:
106
+ file_path = os.path.join(root, file)
107
+ relative_path = os.path.relpath(file_path, folder_path)
108
+ repo_path = os.path.join(HARDCODED_PATH_IN_REPO, relative_path).replace('\\', '/')
109
+
110
+ print(f"Uploading {file_path} to {repo_path}")
111
+
112
+ api.upload_file(
113
+ path_or_fileobj=file_path,
114
+ path_in_repo=repo_path,
115
+ repo_id=HARDCODED_DATASET_REPO_ID,
116
+ repo_type="dataset",
117
+ token=HF_TOKEN
118
+ )
119
+
120
  print("Upload complete!")
121
  except Exception as e:
122
  print(f"Upload failed: {str(e)}")
 
147
  if not os.listdir(temp_dir):
148
  raise Exception("No files were downloaded")
149
 
150
+ print(f"Final downloaded files: {os.listdir(temp_dir)}")
151
 
152
  # Upload to dataset
153
  await loop.run_in_executor(
 
163
  error_msg = f"Transfer failed: {str(e)}"
164
  print(error_msg)
165
  transfer_error = error_msg
 
166
  finally:
167
  # Clean up downloaded files
168
  if temp_dir:
 
202
  if __name__ == "__main__":
203
  import uvicorn
204
  # Run the server indefinitely
205
+ uvicorn.run(app, host="0.0.0.0", port=7860, timeout_keep_alive=600)