|
import gradio as gr |
|
import os |
|
from datasets import load_dataset, DatasetDict, Dataset, Features, Value |
|
|
|
|
|
HUGGINGFACE_TOKEN = os.getenv("hf_token_GCM") |
|
|
|
|
|
DATASET_NAME = "beetle_papers" |
|
|
|
|
|
def upload_files(file_paths): |
|
|
|
if not file_paths: |
|
return "Please select a file to upload." |
|
|
|
|
|
try: |
|
dataset = load_dataset(f"ChristopherMarais/{DATASET_NAME}") |
|
except (ValueError, FileNotFoundError): |
|
|
|
features = Features({ |
|
"filename": Value("string"), |
|
"content": Value("binary") |
|
}) |
|
dataset = DatasetDict({"train": Dataset.from_dict({"filename": [], "content": []}, features=features)}) |
|
|
|
|
|
dataset.push_to_hub(DATASET_NAME, token=HUGGINGFACE_TOKEN) |
|
|
|
|
|
uploaded_files = [] |
|
for file_path in file_paths: |
|
|
|
file_name = os.path.basename(file_path) |
|
|
|
|
|
with open(file_path, "rb") as f: |
|
content = f.read() |
|
|
|
|
|
new_data = {"filename": file_name, "content": content} |
|
dataset["train"] = dataset["train"].add_item(new_data) |
|
uploaded_files.append(file_name) |
|
|
|
|
|
dataset.push_to_hub(DATASET_NAME, token=HUGGINGFACE_TOKEN) |
|
|
|
|
|
return "File(s) uploaded successfully!" |
|
|
|
|
|
def clear_files(): |
|
return None |
|
|
|
|
|
with gr.Blocks() as iface: |
|
gr.Markdown("# Upload Bark and Ambrosia Beetle Files Here!") |
|
gr.Markdown("Upload text files, Word docs, or PDFs") |
|
|
|
upload_file = gr.File(label="Upload your files", interactive=True, file_count="multiple") |
|
|
|
with gr.Row(): |
|
submit_button = gr.Button("Upload", variant="primary", scale=2) |
|
clear_button = gr.Button("Cancel", variant="secondary", scale=1) |
|
|
|
output_text = gr.Textbox(label="", visible=True) |
|
|
|
clear_button.click(fn=clear_files, inputs=None, outputs=upload_file) |
|
submit_button.click(fn=upload_files, inputs=upload_file, outputs=[output_text]) |
|
|
|
|
|
iface.launch() |
|
|