File size: 2,920 Bytes
f1c0686
c9117ba
 
 
 
f1c0686
 
 
 
 
 
 
8a15dbd
f1c0686
3efb4a6
f1c0686
 
 
 
7bd4b6e
f1c0686
 
7bd4b6e
f1c0686
 
 
7bd4b6e
66ac827
 
 
 
 
 
 
f1c0686
 
66ac827
f1c0686
7bd4b6e
f1c0686
c9117ba
f1c0686
0e7d0f3
 
f1c0686
 
 
 
 
 
 
 
 
 
 
 
66ac827
f1c0686
 
 
66ac827
 
f1c0686
 
 
7bd4b6e
f1c0686
 
 
 
 
 
 
 
 
 
66ac827
 
 
 
 
 
 
 
 
f1c0686
3efb4a6
 
 
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
import gradio as gr
from os import path
from src.services.huggingface import init_huggingface, update_dataset
from src.services.json_generator import generate_json
from src.ui.form_components import (
    create_header_tab,
    create_task_tab,
    create_measures_tab,
    create_system_tab,
    create_software_tab,
    create_infrastructure_tab,
    create_environment_tab,
    create_quality_tab
)
css_path = path.join(path.dirname(__file__), "./assets/styles/app.css")

# Initialize Hugging Face
init_huggingface()


def handle_submit(*inputs):
    message, file_output, json_output = generate_json(*inputs)

    # Check if the message indicates validation failure
    if message.startswith("The following fields are required"):
        return message, file_output, json_output

    publish_button = gr.Button(
        "Share your data to the public repository", interactive=True, elem_classes="pubbutton")

    return "Report sucessefully created", file_output, json_output, publish_button


def handle_publi(json_output):
    # If validation passed, proceed to update_dataset
    update_output = update_dataset(json_output)
    return update_output


# Create Gradio interface
with gr.Blocks(css_paths=css_path) as app:
    gr.Markdown("## Data Collection Form")
    gr.Markdown("Welcome to this Huggingface space, where you can create a report on the energy consumption of an AI task in BoAmps format, by filling in a form.<br>"
                "Parts/fields in bold red are mandatory.")

    # Create form tabs
    header_components = create_header_tab()
    task_components = create_task_tab()
    measures_components = create_measures_tab()
    system_components = create_system_tab()
    software_components = create_software_tab()
    infrastructure_components = create_infrastructure_tab()
    environment_components = create_environment_tab()
    quality_components = create_quality_tab()

    # Submit and Download Buttons
    submit_button = gr.Button("Submit", elem_classes="subbutton")
    output = gr.Textbox(label="Output", lines=1)
    json_output = gr.Textbox(visible=False)
    file_output = gr.File(label="Downloadable JSON")
    publish_button = gr.Button(
        "Share your data to the public repository", interactive=False, elem_classes="pubbutton")

    # Event Handlers
    submit_button.click(
        handle_submit,
        inputs=[
            *header_components,
            *task_components,
            *measures_components,
            *system_components,
            *software_components,
            *infrastructure_components,
            *environment_components,
            *quality_components,
        ],
        outputs=[output, file_output, json_output, publish_button]
    )
    # Event Handlers
    publish_button.click(
        handle_publi,
        inputs=[
            json_output
        ],
        outputs=[output]
    )

if __name__ == "__main__":
    app.launch()