import os import gradio as gr # Directory to store uploaded files UPLOAD_DIR = "uploaded_cards" os.makedirs(UPLOAD_DIR, exist_ok=True) # Function to handle file uploads def upload_config_card(file, description): if not file: return "Please upload a file." # Save file file_path = os.path.join(UPLOAD_DIR, file.name) with open(file_path, "wb") as f: f.write(file.read()) # Save description meta_path = file_path + "_meta.txt" with open(meta_path, "w") as meta_file: meta_file.write(description) return f"File '{file.name}' uploaded successfully!" # Function to list uploaded files def list_uploaded_files(): files = [] for file in os.listdir(UPLOAD_DIR): if not file.endswith("_meta.txt"): file_path = os.path.join(UPLOAD_DIR, file) meta_path = file_path + "_meta.txt" description = "No description provided." if os.path.exists(meta_path): with open(meta_path, "r") as meta_file: description = meta_file.read() files.append({"File Name": file, "Description": description, "Download Link": file_path}) return files # Gradio Interface def refresh_file_list(): return list_uploaded_files() with gr.Blocks() as app: gr.Markdown("# Configuration Card Sharing Space") gr.Markdown("Upload and share configuration cards with descriptions of their tasks.") with gr.Row(): with gr.Column(): file_input = gr.File(label="Upload Configuration Card") description_input = gr.Textbox(label="Task Description", placeholder="Describe the task here...") upload_button = gr.Button("Upload") upload_status = gr.Textbox(label="Upload Status", interactive=False) with gr.Column(): refresh_button = gr.Button("Refresh List") file_table = gr.Dataframe(headers=["File Name", "Description", "Download Link"], interactive=False) # Bind upload and refresh functions upload_button.click(upload_config_card, inputs=[file_input, description_input], outputs=[upload_status]) refresh_button.click(refresh_file_list, outputs=[file_table]) app.launch()