File size: 7,008 Bytes
ad956b6
46cbdf9
 
7724700
46cbdf9
 
 
 
 
 
52c683f
46cbdf9
7724700
 
 
46cbdf9
 
 
 
 
52c683f
46cbdf9
 
 
 
 
 
52c683f
 
 
 
ce31d8d
52c683f
 
 
46cbdf9
 
 
 
 
52c683f
 
 
 
 
 
 
c91d84c
52c683f
 
 
c91d84c
 
46cbdf9
ad956b6
1a67d0f
 
 
 
542f5af
 
75eb05b
ce31d8d
 
 
 
0cd8e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46cbdf9
75eb05b
46cbdf9
 
 
 
 
 
 
 
 
 
52c683f
1a67d0f
 
 
 
 
46cbdf9
 
1a67d0f
 
 
46cbdf9
 
 
 
 
 
 
 
c91d84c
46cbdf9
 
 
 
 
1a67d0f
 
 
 
 
 
 
46cbdf9
 
 
 
 
 
 
 
 
63c6341
46cbdf9
63c6341
46cbdf9
 
 
 
 
 
 
 
52c683f
46cbdf9
 
 
7724700
46cbdf9
 
7724700
46cbdf9
7724700
46cbdf9
7724700
46cbdf9
 
 
 
63c6341
46cbdf9
63c6341
46cbdf9
75eb05b
0cd8e4c
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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}")  # Debug line
        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}")  # Debug line
                # 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}")  # Debug line
    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 switch tabs
def switch_tab(tab_name):
    return gr.Tabs.update(visible_tab=tab_name)

# Function to display tasks as clickable cards with modal popups
def display_tasks(task_type):
    tasks = get_tasks_by_type(task_type)
    html_content = """
    <style>
        .modal {{
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0,0,0,0.4);
        }}
        .modal-content {{
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            border-radius: 10px;
        }}
        .close {{
            color: #aaa;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }}
        .close:hover,
        .close:focus {{
            color: black;
            text-decoration: none;
            cursor: pointer;
        }}
    </style>
    """
    html_content += "<div style='display: flex; flex-wrap: wrap; gap: 10px;'>"
    for idx, task in enumerate(tasks):
        color = random.choice(CARD_COLORS)
        html_content += f"""
        <div style='background-color: {color}; padding: 10px; border-radius: 5px; cursor: pointer;' onclick="document.getElementById('modal-{idx}').style.display='block';">
            <b>{task['description']}</b>
        </div>
        <div id='modal-{idx}' class='modal'>
            <div class='modal-content'>
                <span class='close' onclick="document.getElementById('modal-{idx}').style.display='none';">&times;</span>
                <b>Task Type:</b> {task['task_type']}<br>
                <b>Description:</b> {task['description']}<br>
                <b>YAML/Text:</b><pre>{task['yaml']}</pre>
            </div>
        </div>
        """
    html_content += "</div>"
    return html_content

# Gradio App
with gr.Blocks() as app:
    gr.Markdown("# Task Configuration Sharing Space")

    with gr.Tabs() as tabs:
        with gr.Tab("Submit Task"):
            gr.Markdown("## Submit a New Task Configuration")
            
            # Input fields for the task submission
            task_type_input = gr.Dropdown(
                label="Task Type",
                choices=TASK_TYPES,
                value="Classification",
                interactive=True
            )
            new_task_input = gr.Textbox(
                label="Add New Task Type",
                placeholder="Enter a new task type",
                interactive=True
            )
            add_task_button = gr.Button("Add Task Type")
            add_task_status = gr.Textbox(label="Status", interactive=False)

            description_input = gr.Textbox(
                label="Task Description",
                placeholder="Provide a brief description of the task.",
                lines=3
            )
            yaml_input = gr.Textbox(
                label="YAML/Text Input",
                placeholder="Paste your YAML or text configuration here.",
                lines=20
            )
            submit_button = gr.Button("Submit Task")
            go_to_view_tab = gr.Button("Go to View Tasks")
            submission_status = gr.Textbox(label="Status", interactive=False)

            # Handle adding new task type
            add_task_button.click(
                add_new_task_type,
                inputs=[new_task_input],
                outputs=[task_type_input, add_task_status]
            )

            # Handle task submission
            submit_button.click(
                submit_task,
                inputs=[task_type_input, description_input, yaml_input],
                outputs=[submission_status]
            )

            # Button to switch to "View Tasks" tab
            go_to_view_tab.click(
                lambda: gr.Tabs.update(visible_tab="View Tasks"),
                inputs=None,
                outputs=[tabs]
            )

        with gr.Tab("View Tasks"):
            gr.Markdown("## View Submitted Tasks")

            task_type_filter = gr.Dropdown(
                label="Filter by Task Type",
                choices=TASK_TYPES,
                value="Classification",
                interactive=True
            )
            view_button = gr.Button("View Tasks")
            task_display = gr.HTML(label="Submitted Tasks")
            go_to_submit_tab = gr.Button("Go to Submit Task")

            # Handle task display
            view_button.click(
                display_tasks,
                inputs=[task_type_filter],
                outputs=[task_display]
            )

            # Button to switch to "Submit Task" tab
            go_to_submit_tab.click(
                lambda: gr.Tabs.update(visible_tab="Submit Task"),
                inputs=None,
                outputs=[tabs]
            )

app.launch()