CoolFace
Apppublic

interview-eval/config-collection

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py170 linesDownload Raw Back to root
1import gradio as gr2import os3import json4import random5 6# Directory to store submissions7DATA_DIR = "submissions"8os.makedirs(DATA_DIR, exist_ok=True)9 10# Predefined task types11TASK_TYPES = ["Classification", "Regression", "Translation"]12 13# Colors for task cards14CARD_COLORS = ["#FFDDC1", "#FFABAB", "#FFC3A0", "#D5AAFF", "#85E3FF", "#B9FBC0"]15 16# Function to handle task submission17def submit_task(task_type, description, yaml_text):18    if not yaml_text.strip():19        return "YAML/Text input cannot be empty."20 21    # Prepare data22    data = {23        "task_type": task_type,24        "description": description,25        "yaml": yaml_text26    }27    file_path = os.path.join(DATA_DIR, f"{task_type}_{len(os.listdir(DATA_DIR))}.json")28    29    try:30        with open(file_path, "w") as f:31            json.dump(data, f)32        print(f"Saved file: {file_path}, Contents: {data}")33        return f"Task submitted successfully under type '{task_type}'!"34    except Exception as e:35        return f"Error saving task: {e}"36 37# Function to get tasks by type38def get_tasks_by_type(task_type):39    tasks = []40    for file in os.listdir(DATA_DIR):41        # Skip non-JSON files42        if not file.endswith(".json"):43            continue44        45        try:46            with open(os.path.join(DATA_DIR, file), "r") as f:47                data = json.load(f)48                print(f"File: {file}, Content: {data}")49                # Filter by task type50                if data.get("task_type") == task_type:51                    tasks.append(data)52        except (json.JSONDecodeError, KeyError) as e:53            print(f"Error reading file {file}: {e}")54    return tasks55 56# Function to dynamically add a new task type57def add_new_task_type(new_type):58    if new_type and new_type not in TASK_TYPES:59        TASK_TYPES.append(new_type)60        return gr.update(choices=TASK_TYPES), f"Task type '{new_type}' added successfully!"61    return gr.update(choices=TASK_TYPES), "Task type already exists or invalid input."62 63# Function to display tasks as clickable cards64def display_tasks(task_type):65    tasks = get_tasks_by_type(task_type)66    html_content = "<div style='display: flex; flex-wrap: wrap; gap: 10px; color: #000;'>"67    for idx, task in enumerate(tasks):68        color = random.choice(CARD_COLORS)69        html_content += f"""70        <div style='background-color: {color}; color: #000; padding: 10px; border-radius: 5px; cursor: pointer;' onclick="document.getElementById('task-details-{idx}').style.display='block';">71            <b>{task['description']}</b>72        </div>73        <div id='task-details-{idx}' style='display: none; margin-top: 10px; border: 1px solid #ccc; background: #fff; padding: 10px; border-radius: 5px;'>74            <b>Task Type:</b> {task['task_type']}<br>75            <b>Description:</b> {task['description']}<br>76            <b>YAML/Text:</b><pre>{task['yaml']}</pre>77            <button style='margin-top: 10px; color: #000;' onclick="document.getElementById('task-details-{idx}').style.display='none';">Close</button>78            <span style='cursor: pointer; float: right; font-size: 18px; color: #000;' onclick="document.getElementById('task-details-{idx}').style.display='none';">&times;</span>79        </div>80        """81    html_content += "</div>"82    return html_content83 84# Gradio App85with gr.Blocks() as app:86    gr.Markdown("# Task Configuration Sharing Space")87 88    with gr.Tabs() as tabs:89        with gr.Tab("Submit Task"):90            gr.Markdown("## Submit a New Task Configuration")91            92            # Input fields for the task submission93            task_type_input = gr.Dropdown(94                label="Task Type",95                choices=TASK_TYPES,96                value="Classification",97                interactive=True98            )99            new_task_input = gr.Textbox(100                label="Add New Task Type",101                placeholder="Enter a new task type",102                interactive=True103            )104            add_task_button = gr.Button("Add Task Type")105            add_task_status = gr.Textbox(label="Status", interactive=False)106 107            description_input = gr.Textbox(108                label="Task Description",109                placeholder="Provide a brief description of the task.",110                lines=3111            )112            yaml_input = gr.Textbox(113                label="YAML/Text Input",114                placeholder="Paste your YAML or text configuration here.",115                lines=20116            )117            submit_button = gr.Button("Submit Task")118            go_to_view_tab = gr.Button("Go to View Tasks")119            submission_status = gr.Textbox(label="Status", interactive=False)120 121            # Handle adding new task type122            add_task_button.click(123                add_new_task_type,124                inputs=[new_task_input],125                outputs=[task_type_input, add_task_status]  # Update dropdown and status126            )127 128            # Handle task submission129            submit_button.click(130                submit_task,131                inputs=[task_type_input, description_input, yaml_input],132                outputs=[submission_status]133            )134 135            # Button to switch to "View Tasks" tab136            go_to_view_tab.click(137                lambda: gr.Tabs.update(visible_tab="View Tasks"),138                inputs=None,139                outputs=[tabs]140            )141 142        with gr.Tab("View Tasks"):143            gr.Markdown("## View Submitted Tasks")144 145            task_type_filter = gr.Dropdown(146                label="Filter by Task Type",147                choices=TASK_TYPES,148                value="Classification",149                interactive=True150            )151            view_button = gr.Button("View Tasks")152            task_display = gr.HTML(label="Submitted Tasks")153            go_to_submit_tab = gr.Button("Go to Submit Task")154 155            # Handle task display156            view_button.click(157                display_tasks,158                inputs=[task_type_filter],159                outputs=[task_display]160            )161 162            # Button to switch to "Submit Task" tab163            go_to_submit_tab.click(164                lambda: gr.Tabs.update(visible_tab="Submit Task"),165                inputs=None,166                outputs=[tabs]167            )168 169app.launch()170