Spaces:
Running
Running
import os | |
import time | |
from huggingface_hub import HfApi | |
import crediantials | |
# Set up Hugging Face credentials | |
HF_TOKEN = crediantials.HF_TOKEN | |
REPO_ID = crediantials.REPO_ID # Replace with your repo | |
# Set the full file path | |
def create_temp_file(local_file_path): | |
"""Creates a temporary file at the given path with unique content.""" | |
os.makedirs(os.path.dirname(local_file_path), exist_ok=True) # Ensure the folder exists | |
with open(local_file_path, "w") as temp_file: | |
temp_file.write(f"Temporary file content - {time.time()}") # Add timestamp | |
def upload_file(local_file_path): | |
"""Uploads the local file to Hugging Face Hub.""" | |
api = HfApi() | |
api.upload_file( | |
path_or_fileobj=local_file_path, | |
path_in_repo=os.path.basename(local_file_path), # Keep the same name on HF | |
repo_id=REPO_ID, | |
repo_type="space", | |
token=HF_TOKEN, | |
) | |
print(f"File {local_file_path} uploaded successfully! to hugging face ") | |
def main(): | |
local_file_path = "/path/to/your/folder/uploaded_file.txt" | |
create_temp_file(local_file_path) # Create file at specified path | |
upload_file(local_file_path) # Upload to Hugging Face | |
os.remove(local_file_path) # Clean up local file after upload | |
if __name__ == "__main__": | |
main() | |