George-API commited on
Commit
505517a
·
verified ·
1 Parent(s): 22cec44

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. run_transformers_training.py +87 -3
  2. update_space.py +32 -19
run_transformers_training.py CHANGED
@@ -117,8 +117,9 @@ def load_env_variables():
117
  # Try to load from .env file if not in a Space
118
  try:
119
  from dotenv import load_dotenv
120
- # Updated path to .env file in the new directory structure
121
- env_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "shared", ".env")
 
122
  if os.path.exists(env_path):
123
  load_dotenv(env_path)
124
  logging.info(f"Loaded environment variables from {env_path}")
@@ -126,10 +127,22 @@ def load_env_variables():
126
  logging.info(f"HF_USERNAME loaded from .env file: {bool(os.environ.get('HF_USERNAME'))}")
127
  logging.info(f"HF_SPACE_NAME loaded from .env file: {bool(os.environ.get('HF_SPACE_NAME'))}")
128
  else:
129
- logging.warning(f"No .env file found at {env_path}")
 
 
 
 
 
 
 
 
 
130
  except ImportError:
131
  logging.warning("python-dotenv not installed, not loading from .env file")
132
 
 
 
 
133
  if not os.environ.get("HF_USERNAME"):
134
  logger.warning("HF_USERNAME is not set. Using default username.")
135
 
@@ -671,6 +684,70 @@ def check_dependencies():
671
 
672
  return True
673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
674
  def main():
675
  # Set up logging
676
  logger.info("Starting training process")
@@ -686,6 +763,9 @@ def main():
686
  # Load environment variables
687
  load_env_variables()
688
 
 
 
 
689
  # Load configuration
690
  try:
691
  transformers_config = load_configs(args.config)
@@ -941,6 +1021,10 @@ def main():
941
  log_info(f"Pushing model to Hugging Face Hub as {hub_id}...")
942
  trainer.push_to_hub()
943
  log_info("Model successfully pushed to Hub")
 
 
 
 
944
 
945
  return 0
946
  except Exception as e:
 
117
  # Try to load from .env file if not in a Space
118
  try:
119
  from dotenv import load_dotenv
120
+ # First check the current directory
121
+ env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env")
122
+
123
  if os.path.exists(env_path):
124
  load_dotenv(env_path)
125
  logging.info(f"Loaded environment variables from {env_path}")
 
127
  logging.info(f"HF_USERNAME loaded from .env file: {bool(os.environ.get('HF_USERNAME'))}")
128
  logging.info(f"HF_SPACE_NAME loaded from .env file: {bool(os.environ.get('HF_SPACE_NAME'))}")
129
  else:
130
+ # Try the shared directory as fallback
131
+ shared_env_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "shared", ".env")
132
+ if os.path.exists(shared_env_path):
133
+ load_dotenv(shared_env_path)
134
+ logging.info(f"Loaded environment variables from {shared_env_path}")
135
+ logging.info(f"HF_TOKEN loaded from shared .env file: {bool(os.environ.get('HF_TOKEN'))}")
136
+ logging.info(f"HF_USERNAME loaded from shared .env file: {bool(os.environ.get('HF_USERNAME'))}")
137
+ logging.info(f"HF_SPACE_NAME loaded from shared .env file: {bool(os.environ.get('HF_SPACE_NAME'))}")
138
+ else:
139
+ logging.warning(f"No .env file found in current or shared directory")
140
  except ImportError:
141
  logging.warning("python-dotenv not installed, not loading from .env file")
142
 
143
+ if not os.environ.get("HF_TOKEN"):
144
+ logger.warning("HF_TOKEN is not set. Pushing to Hugging Face Hub will not work.")
145
+
146
  if not os.environ.get("HF_USERNAME"):
147
  logger.warning("HF_USERNAME is not set. Using default username.")
148
 
 
684
 
685
  return True
686
 
687
+ def update_huggingface_space():
688
+ """Update the Hugging Face Space with the current code."""
689
+ log_info("Updating Hugging Face Space...")
690
+ update_script = os.path.join(os.path.dirname(os.path.abspath(__file__)), "update_space.py")
691
+
692
+ if not os.path.exists(update_script):
693
+ logger.warning(f"Update space script not found at {update_script}")
694
+ return False
695
+
696
+ try:
697
+ import subprocess
698
+ result = subprocess.run([sys.executable, update_script, "--force"],
699
+ capture_output=True, text=True, check=False)
700
+
701
+ if result.returncode == 0:
702
+ log_info("Hugging Face Space updated successfully!")
703
+ log_info(f"Space URL: https://huggingface.co/spaces/{os.environ.get('HF_USERNAME', 'George-API')}/{os.environ.get('HF_SPACE_NAME', 'phi4training')}")
704
+ return True
705
+ else:
706
+ logger.error(f"Failed to update Hugging Face Space: {result.stderr}")
707
+ return False
708
+ except Exception as e:
709
+ logger.error(f"Error updating Hugging Face Space: {str(e)}")
710
+ return False
711
+
712
+ def validate_huggingface_credentials():
713
+ """Validate Hugging Face credentials to ensure they work correctly."""
714
+ if not os.environ.get("HF_TOKEN"):
715
+ logger.warning("HF_TOKEN not found. Skipping Hugging Face credentials validation.")
716
+ return False
717
+
718
+ try:
719
+ # Import here to avoid requiring huggingface_hub if not needed
720
+ from huggingface_hub import HfApi, login
721
+
722
+ # Try to login with the token
723
+ login(token=os.environ.get("HF_TOKEN"))
724
+
725
+ # Check if we can access the API
726
+ api = HfApi()
727
+ username = os.environ.get("HF_USERNAME", "George-API")
728
+ space_name = os.environ.get("HF_SPACE_NAME", "phi4training")
729
+
730
+ # Try to get whoami info
731
+ user_info = api.whoami()
732
+ logger.info(f"Successfully authenticated with Hugging Face as {user_info['name']}")
733
+
734
+ # Check if the space exists
735
+ try:
736
+ space_id = f"{username}/{space_name}"
737
+ space_info = api.space_info(repo_id=space_id)
738
+ logger.info(f"Space {space_id} is accessible")
739
+ return True
740
+ except Exception as e:
741
+ logger.warning(f"Could not access Space {username}/{space_name}: {str(e)}")
742
+ logger.warning("Space updating may not work correctly")
743
+ return False
744
+ except ImportError:
745
+ logger.warning("huggingface_hub not installed. Cannot validate Hugging Face credentials.")
746
+ return False
747
+ except Exception as e:
748
+ logger.warning(f"Error validating Hugging Face credentials: {str(e)}")
749
+ return False
750
+
751
  def main():
752
  # Set up logging
753
  logger.info("Starting training process")
 
763
  # Load environment variables
764
  load_env_variables()
765
 
766
+ # Validate Hugging Face credentials if we're going to use them
767
+ validate_huggingface_credentials()
768
+
769
  # Load configuration
770
  try:
771
  transformers_config = load_configs(args.config)
 
1021
  log_info(f"Pushing model to Hugging Face Hub as {hub_id}...")
1022
  trainer.push_to_hub()
1023
  log_info("Model successfully pushed to Hub")
1024
+
1025
+ # Update the Hugging Face Space with current code
1026
+ if os.environ.get("HF_TOKEN") and os.environ.get("HF_USERNAME") and os.environ.get("HF_SPACE_NAME"):
1027
+ update_huggingface_space()
1028
 
1029
  return 0
1030
  except Exception as e:
update_space.py CHANGED
@@ -204,30 +204,43 @@ def create_space(username, space_name):
204
  raise RuntimeError(f"Error with Space {space_id}: {str(e)}")
205
 
206
  def main():
207
- parser = argparse.ArgumentParser(description='Update Hugging Face Space for Phi-4 training')
208
- parser.add_argument('--space_name', type=str, help='Space name (default: from env)')
209
- parser.add_argument('--force', action='store_true', help='Skip confirmation')
210
- args = parser.parse_args()
211
-
212
- if not args.force:
213
- print("\n" + "!"*80)
214
- print("WARNING: Updating the Space will INTERRUPT any ongoing training!")
215
- print("Make sure all checkpoints are saved before proceeding.")
216
- print("!"*80 + "\n")
217
-
218
- confirm = input("Type 'update' to confirm: ")
219
- if confirm.lower() != 'update':
220
- logger.info("Update cancelled")
221
- return False
222
-
223
  try:
 
 
 
 
 
 
224
  # Load environment variables
225
  env_vars = load_env_variables()
 
 
 
 
 
 
 
 
 
 
226
  logger.info(f"Environment variables loaded: USERNAME={env_vars['HF_USERNAME']}, SPACE_NAME={env_vars['HF_SPACE_NAME']}")
227
 
228
- # Verify configurations
229
- verify_configs()
230
- logger.info("All configuration files verified successfully")
 
 
 
 
 
 
 
 
 
 
 
 
231
 
232
  # Update requirements
233
  update_requirements()
 
204
  raise RuntimeError(f"Error with Space {space_id}: {str(e)}")
205
 
206
  def main():
207
+ """Main function to update the Space."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  try:
209
+ # Parse command line arguments
210
+ parser = argparse.ArgumentParser(description='Update Hugging Face Space for Phi-4 training')
211
+ parser.add_argument('--space_name', type=str, help='Space name (default: from env)')
212
+ parser.add_argument('--force', action='store_true', help='Skip confirmation when updating Space')
213
+ args = parser.parse_args()
214
+
215
  # Load environment variables
216
  env_vars = load_env_variables()
217
+ verify_configs()
218
+
219
+ # Verify we have the necessary variables
220
+ if not all(k in env_vars and env_vars[k] for k in ["HF_TOKEN", "HF_USERNAME", "HF_SPACE_NAME"]):
221
+ logger.error("Missing required environment variables. Please check your .env file.")
222
+ logger.error(f"HF_TOKEN: {'Set' if 'HF_TOKEN' in env_vars and env_vars['HF_TOKEN'] else 'Not Set'}")
223
+ logger.error(f"HF_USERNAME: {'Set' if 'HF_USERNAME' in env_vars and env_vars['HF_USERNAME'] else 'Not Set'}")
224
+ logger.error(f"HF_SPACE_NAME: {'Set' if 'HF_SPACE_NAME' in env_vars and env_vars['HF_SPACE_NAME'] else 'Not Set'}")
225
+ return False
226
+
227
  logger.info(f"Environment variables loaded: USERNAME={env_vars['HF_USERNAME']}, SPACE_NAME={env_vars['HF_SPACE_NAME']}")
228
 
229
+ # Ask for confirmation unless forced
230
+ if not args.force:
231
+ print("\nWARNING: Updating the Space will INTERRUPT any ongoing training!")
232
+ confirm = input("Are you sure you want to update the Space? Type 'yes' to confirm: ")
233
+ if confirm.lower() != 'yes':
234
+ logger.info("Update cancelled by user")
235
+ return False
236
+
237
+ # Additional password check for safety
238
+ password = getpass.getpass("Enter your password to confirm update: ")
239
+ if password.strip() == "":
240
+ logger.info("No password entered. Update cancelled.")
241
+ return False
242
+ else:
243
+ logger.info("Skipping confirmation due to --force flag")
244
 
245
  # Update requirements
246
  update_requirements()