Spaces:
Sleeping
Sleeping
Krishnavardhan
commited on
Commit
•
deadd78
1
Parent(s):
b94278b
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import HfApi
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Replace these with your own values
|
6 |
+
HUGGING_FACE_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
|
7 |
+
REPO_ID = "Krishnavardhan/data_upload" # Space to upload the files
|
8 |
+
FOLDER_NAME = "data"
|
9 |
+
# Hugging Face API instance
|
10 |
+
api = HfApi()
|
11 |
+
|
12 |
+
# Function to upload file to Hugging Face space
|
13 |
+
def upload_to_huggingface(file):
|
14 |
+
# Extract only the file name, without the full path
|
15 |
+
file_name = os.path.basename(file.name)
|
16 |
+
path_in_repo = f"{FOLDER_NAME}/{file_name}"
|
17 |
+
try:
|
18 |
+
# Upload the file to the specified space
|
19 |
+
api.upload_file(
|
20 |
+
path_or_fileobj=file.name, # Upload the actual file from the full local path
|
21 |
+
path_in_repo=path_in_repo, # Use only the file name when uploading to Hugging Face
|
22 |
+
repo_id=REPO_ID,
|
23 |
+
repo_type="space",
|
24 |
+
token=HUGGING_FACE_TOKEN
|
25 |
+
)
|
26 |
+
return f"File '{file_name}' successfully uploaded to Hugging Face space!"
|
27 |
+
except Exception as e:
|
28 |
+
return f"Error uploading file: {str(e)}"
|
29 |
+
|
30 |
+
# Gradio interface for file upload
|
31 |
+
def file_upload_interface(file):
|
32 |
+
return upload_to_huggingface(file)
|
33 |
+
|
34 |
+
# Launch the Gradio interface
|
35 |
+
gr.Interface(fn=file_upload_interface, inputs="file", outputs="text").launch()
|