Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,10 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import HfApi, list_repo_files
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# 1. This grabs the
|
| 6 |
api = HfApi(token=os.getenv("HF_TOKEN"))
|
| 7 |
|
| 8 |
-
# 2. This is the address
|
| 9 |
REPO_ID = "abhy60098/my-cloud-storage"
|
| 10 |
|
| 11 |
def upload_file(file):
|
|
@@ -14,48 +14,40 @@ def upload_file(file):
|
|
| 14 |
|
| 15 |
try:
|
| 16 |
file_name = os.path.basename(file.name)
|
| 17 |
-
# 3.
|
| 18 |
api.upload_file(
|
| 19 |
path_or_fileobj=file.name,
|
| 20 |
path_in_repo=file_name,
|
| 21 |
repo_id=REPO_ID,
|
| 22 |
repo_type="dataset"
|
| 23 |
)
|
| 24 |
-
return f"β
Success!
|
| 25 |
except Exception as e:
|
|
|
|
| 26 |
return f"β Error: {str(e)}", get_files()
|
| 27 |
|
| 28 |
def get_files():
|
| 29 |
try:
|
| 30 |
-
# 4. Looking inside the dataset to see your files
|
| 31 |
files = list_repo_files(repo_id=REPO_ID, repo_type="dataset")
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# 5. Create links so you can see/download your files
|
| 39 |
-
links = [f"π [{f}](https://huggingface.co/datasets/{REPO_ID}/resolve/main/{f})" for f in filtered_files]
|
| 40 |
return "\n".join(links)
|
| 41 |
except:
|
| 42 |
return "Connecting to storage..."
|
| 43 |
|
| 44 |
-
# 6. Building the Website Look
|
| 45 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 46 |
gr.Markdown("# βοΈ My Public Cloud Storage")
|
| 47 |
-
gr.Markdown(f"Files are saved to: [{REPO_ID}](https://huggingface.co/datasets/{REPO_ID})")
|
| 48 |
-
|
| 49 |
with gr.Row():
|
| 50 |
with gr.Column():
|
| 51 |
file_input = gr.File(label="Upload any file")
|
| 52 |
btn = gr.Button("π Upload to Dataset", variant="primary")
|
| 53 |
status = gr.Textbox(label="Status")
|
| 54 |
-
|
| 55 |
with gr.Column():
|
| 56 |
gr.Markdown("### π Your Files")
|
| 57 |
file_display = gr.Markdown(get_files())
|
| 58 |
-
|
| 59 |
btn.click(upload_file, inputs=file_input, outputs=[status, file_display])
|
| 60 |
|
| 61 |
demo.launch()
|
|
|
|
| 2 |
from huggingface_hub import HfApi, list_repo_files
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# 1. This grabs the Secret Key you just re-saved
|
| 6 |
api = HfApi(token=os.getenv("HF_TOKEN"))
|
| 7 |
|
| 8 |
+
# 2. This is the house address for your files
|
| 9 |
REPO_ID = "abhy60098/my-cloud-storage"
|
| 10 |
|
| 11 |
def upload_file(file):
|
|
|
|
| 14 |
|
| 15 |
try:
|
| 16 |
file_name = os.path.basename(file.name)
|
| 17 |
+
# 3. This moves the file into your dataset
|
| 18 |
api.upload_file(
|
| 19 |
path_or_fileobj=file.name,
|
| 20 |
path_in_repo=file_name,
|
| 21 |
repo_id=REPO_ID,
|
| 22 |
repo_type="dataset"
|
| 23 |
)
|
| 24 |
+
return f"β
Success! {file_name} is now in the cloud.", get_files()
|
| 25 |
except Exception as e:
|
| 26 |
+
# If the key is wrong, it tells us here
|
| 27 |
return f"β Error: {str(e)}", get_files()
|
| 28 |
|
| 29 |
def get_files():
|
| 30 |
try:
|
|
|
|
| 31 |
files = list_repo_files(repo_id=REPO_ID, repo_type="dataset")
|
| 32 |
+
# Hide the system files
|
| 33 |
+
clean_list = [f for f in files if not f.startswith(".") and f != "README.md"]
|
| 34 |
+
if not clean_list: return "The storage is currently empty."
|
| 35 |
+
# Create clickable links
|
| 36 |
+
links = [f"π [{f}](https://huggingface.co/datasets/{REPO_ID}/resolve/main/{f})" for f in clean_list]
|
|
|
|
|
|
|
|
|
|
| 37 |
return "\n".join(links)
|
| 38 |
except:
|
| 39 |
return "Connecting to storage..."
|
| 40 |
|
|
|
|
| 41 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 42 |
gr.Markdown("# βοΈ My Public Cloud Storage")
|
|
|
|
|
|
|
| 43 |
with gr.Row():
|
| 44 |
with gr.Column():
|
| 45 |
file_input = gr.File(label="Upload any file")
|
| 46 |
btn = gr.Button("π Upload to Dataset", variant="primary")
|
| 47 |
status = gr.Textbox(label="Status")
|
|
|
|
| 48 |
with gr.Column():
|
| 49 |
gr.Markdown("### π Your Files")
|
| 50 |
file_display = gr.Markdown(get_files())
|
|
|
|
| 51 |
btn.click(upload_file, inputs=file_input, outputs=[status, file_display])
|
| 52 |
|
| 53 |
demo.launch()
|