File size: 2,227 Bytes
75eb05b
ad956b6
 
75eb05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad956b6
 
75eb05b
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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()