import gradio as gr
import os
import json
import random
# Directory to store submissions
DATA_DIR = "submissions"
os.makedirs(DATA_DIR, exist_ok=True)
# Predefined task types
TASK_TYPES = ["Classification", "Regression", "Translation"]
# Colors for task cards
CARD_COLORS = ["#FFDDC1", "#FFABAB", "#FFC3A0", "#D5AAFF", "#85E3FF", "#B9FBC0"]
# Function to handle task submission
def submit_task(task_type, description, yaml_text):
if not yaml_text.strip():
return "YAML/Text input cannot be empty."
# Prepare data
data = {
"task_type": task_type,
"description": description,
"yaml": yaml_text
}
file_path = os.path.join(DATA_DIR, f"{task_type}_{len(os.listdir(DATA_DIR))}.json")
try:
with open(file_path, "w") as f:
json.dump(data, f)
print(f"Saved file: {file_path}, Contents: {data}")
return f"Task submitted successfully under type '{task_type}'!"
except Exception as e:
return f"Error saving task: {e}"
# Function to get tasks by type
def get_tasks_by_type(task_type):
tasks = []
for file in os.listdir(DATA_DIR):
# Skip non-JSON files
if not file.endswith(".json"):
continue
try:
with open(os.path.join(DATA_DIR, file), "r") as f:
data = json.load(f)
print(f"File: {file}, Content: {data}")
# Filter by task type
if data.get("task_type") == task_type:
tasks.append(data)
except (json.JSONDecodeError, KeyError) as e:
print(f"Error reading file {file}: {e}")
return tasks
# Function to dynamically add a new task type
def add_new_task_type(new_type):
if new_type and new_type not in TASK_TYPES:
TASK_TYPES.append(new_type)
return gr.update(choices=TASK_TYPES), f"Task type '{new_type}' added successfully!"
return gr.update(choices=TASK_TYPES), "Task type already exists or invalid input."
# Function to display tasks as clickable cards
def display_tasks(task_type):
tasks = get_tasks_by_type(task_type)
html_content = "
"
for idx, task in enumerate(tasks):
color = random.choice(CARD_COLORS)
html_content += f"""