Spaces:
Sleeping
Sleeping
import gradio as gr | |
from huggingface_hub import HfApi | |
import os | |
# Replace these with your own values | |
HUGGING_FACE_TOKEN = os.getenv("HUGGING_FACE_TOKEN") | |
REPO_ID = "Krishnavardhan/data_upload" # Space to upload the files | |
FOLDER_NAME = "data" | |
# Hugging Face API instance | |
api = HfApi() | |
# Function to upload file to Hugging Face space | |
def upload_to_huggingface(file): | |
# Extract only the file name, without the full path | |
file_name = os.path.basename(file.name) | |
path_in_repo = f"{FOLDER_NAME}/{file_name}" | |
try: | |
# Upload the file to the specified space | |
api.upload_file( | |
path_or_fileobj=file.name, # Upload the actual file from the full local path | |
path_in_repo=path_in_repo, # Use only the file name when uploading to Hugging Face | |
repo_id=REPO_ID, | |
repo_type="space", | |
token=HUGGING_FACE_TOKEN | |
) | |
return f"File '{file_name}' successfully uploaded to Hugging Face space!" | |
except Exception as e: | |
return f"Error uploading file: {str(e)}" | |
# Gradio interface for file upload | |
def file_upload_interface(file): | |
return upload_to_huggingface(file) | |
# Launch the Gradio interface | |
gr.Interface(fn=file_upload_interface, inputs="file", outputs="text").launch() | |