Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,120 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# Function to handle file uploads
|
| 9 |
-
def upload_config_card(file, description):
|
| 10 |
-
if not file:
|
| 11 |
-
return "Please upload a file."
|
| 12 |
-
|
| 13 |
-
# Save file
|
| 14 |
-
file_path = os.path.join(UPLOAD_DIR, file.name)
|
| 15 |
-
with open(file_path, "wb") as f:
|
| 16 |
-
f.write(file.read())
|
| 17 |
-
|
| 18 |
-
# Save description
|
| 19 |
-
meta_path = file_path + "_meta.txt"
|
| 20 |
-
with open(meta_path, "w") as meta_file:
|
| 21 |
-
meta_file.write(description)
|
| 22 |
-
|
| 23 |
-
return f"File '{file.name}' uploaded successfully!"
|
| 24 |
-
|
| 25 |
-
# Function to list uploaded files
|
| 26 |
-
def list_uploaded_files():
|
| 27 |
-
files = []
|
| 28 |
-
for file in os.listdir(UPLOAD_DIR):
|
| 29 |
-
if not file.endswith("_meta.txt"):
|
| 30 |
-
file_path = os.path.join(UPLOAD_DIR, file)
|
| 31 |
-
meta_path = file_path + "_meta.txt"
|
| 32 |
-
description = "No description provided."
|
| 33 |
-
if os.path.exists(meta_path):
|
| 34 |
-
with open(meta_path, "r") as meta_file:
|
| 35 |
-
description = meta_file.read()
|
| 36 |
-
files.append({"File Name": file, "Description": description, "Download Link": file_path})
|
| 37 |
-
return files
|
| 38 |
-
|
| 39 |
-
# Gradio Interface
|
| 40 |
-
def refresh_file_list():
|
| 41 |
-
return list_uploaded_files()
|
| 42 |
|
|
|
|
| 43 |
with gr.Blocks() as app:
|
| 44 |
-
gr.Markdown("# Configuration
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
app.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Directory to store submissions
|
| 6 |
+
DATA_DIR = "submissions"
|
| 7 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 8 |
+
|
| 9 |
+
# Predefined task types
|
| 10 |
+
TASK_TYPES = ["Classification", "Regression", "Translation", "Custom"]
|
| 11 |
+
|
| 12 |
+
# Function to handle task submission
|
| 13 |
+
def submit_task(task_type, description, yaml_text):
|
| 14 |
+
if not yaml_text.strip():
|
| 15 |
+
return "YAML/Text input cannot be empty."
|
| 16 |
+
|
| 17 |
+
# Save task data
|
| 18 |
+
data = {
|
| 19 |
+
"task_type": task_type,
|
| 20 |
+
"description": description,
|
| 21 |
+
"yaml": yaml_text
|
| 22 |
+
}
|
| 23 |
+
file_path = os.path.join(DATA_DIR, f"{task_type}_{len(os.listdir(DATA_DIR))}.json")
|
| 24 |
+
with open(file_path, "w") as f:
|
| 25 |
+
json.dump(data, f)
|
| 26 |
+
|
| 27 |
+
return f"Task submitted successfully under type '{task_type}'!"
|
| 28 |
+
|
| 29 |
+
# Function to get tasks by type
|
| 30 |
+
def get_tasks_by_type(task_type):
|
| 31 |
+
tasks = []
|
| 32 |
+
for file in os.listdir(DATA_DIR):
|
| 33 |
+
with open(os.path.join(DATA_DIR, file), "r") as f:
|
| 34 |
+
data = json.load(f)
|
| 35 |
+
if data["task_type"] == task_type:
|
| 36 |
+
tasks.append(data)
|
| 37 |
+
return tasks
|
| 38 |
|
| 39 |
+
# Function to switch tabs
|
| 40 |
+
def switch_tab(tab_name):
|
| 41 |
+
return gr.Tabs.update(visible_tab=tab_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Gradio App
|
| 44 |
with gr.Blocks() as app:
|
| 45 |
+
gr.Markdown("# Task Configuration Sharing Space")
|
| 46 |
+
|
| 47 |
+
with gr.Tabs() as tabs:
|
| 48 |
+
with gr.Tab("Submit Task"):
|
| 49 |
+
gr.Markdown("## Submit a New Task Configuration")
|
| 50 |
+
|
| 51 |
+
# Input fields for the task submission
|
| 52 |
+
task_type_input = gr.Dropdown(
|
| 53 |
+
label="Task Type",
|
| 54 |
+
choices=TASK_TYPES,
|
| 55 |
+
value="Classification",
|
| 56 |
+
interactive=True
|
| 57 |
+
)
|
| 58 |
+
description_input = gr.Textbox(
|
| 59 |
+
label="Task Description",
|
| 60 |
+
placeholder="Provide a brief description of the task.",
|
| 61 |
+
lines=3
|
| 62 |
+
)
|
| 63 |
+
yaml_input = gr.Textbox(
|
| 64 |
+
label="YAML/Text Input",
|
| 65 |
+
placeholder="Paste your YAML or text configuration here.",
|
| 66 |
+
lines=10
|
| 67 |
+
)
|
| 68 |
+
submit_button = gr.Button("Submit Task")
|
| 69 |
+
go_to_view_tab = gr.Button("Go to View Tasks")
|
| 70 |
+
submission_status = gr.Textbox(label="Status", interactive=False)
|
| 71 |
+
|
| 72 |
+
# Handle task submission
|
| 73 |
+
submit_button.click(
|
| 74 |
+
submit_task,
|
| 75 |
+
inputs=[task_type_input, description_input, yaml_input],
|
| 76 |
+
outputs=[submission_status]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Button to switch to "View Tasks" tab
|
| 80 |
+
go_to_view_tab.click(
|
| 81 |
+
switch_tab,
|
| 82 |
+
inputs=None,
|
| 83 |
+
outputs=[tabs],
|
| 84 |
+
fn_kwargs={"tab_name": "View Tasks"}
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
with gr.Tab("View Tasks"):
|
| 88 |
+
gr.Markdown("## View Submitted Tasks")
|
| 89 |
+
|
| 90 |
+
task_type_filter = gr.Dropdown(
|
| 91 |
+
label="Filter by Task Type",
|
| 92 |
+
choices=TASK_TYPES,
|
| 93 |
+
value="Classification",
|
| 94 |
+
interactive=True
|
| 95 |
+
)
|
| 96 |
+
view_button = gr.Button("View Tasks")
|
| 97 |
+
task_table = gr.Dataframe(headers=["Task Type", "Description", "YAML/Text"], interactive=False)
|
| 98 |
+
go_to_submit_tab = gr.Button("Go to Submit Task")
|
| 99 |
+
|
| 100 |
+
# Function to filter tasks by type
|
| 101 |
+
def filter_tasks(task_type):
|
| 102 |
+
tasks = get_tasks_by_type(task_type)
|
| 103 |
+
return [{"Task Type": t["task_type"], "Description": t["description"], "YAML/Text": t["yaml"]} for t in tasks]
|
| 104 |
+
|
| 105 |
+
# Handle task filtering
|
| 106 |
+
view_button.click(
|
| 107 |
+
filter_tasks,
|
| 108 |
+
inputs=[task_type_filter],
|
| 109 |
+
outputs=[task_table]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Button to switch to "Submit Task" tab
|
| 113 |
+
go_to_submit_tab.click(
|
| 114 |
+
switch_tab,
|
| 115 |
+
inputs=None,
|
| 116 |
+
outputs=[tabs],
|
| 117 |
+
fn_kwargs={"tab_name": "Submit Task"}
|
| 118 |
+
)
|
| 119 |
|
| 120 |
app.launch()
|