File size: 1,267 Bytes
69724ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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/Hosted_RAG"  # 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()