File size: 3,227 Bytes
54b9ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
from modules.utils import fetch_survey, fetch_plot, generate_report
from solar_report.analyze import send_report_email
from pathlib import Path

default_csv_path = Path("data/samples/export.csv")


def file():
    def _inspect_data(file):
        df = pd.read_csv(file)
        # replace name of column "ACDC_InlteTemp" -> "AC/DC Inlet Temp"
        df.rename(columns={"ACDC_InlteTemp": "AC/DC Inlet"}, inplace=True)
        return df

    gr.Markdown("### Device Readings")
    with gr.Row():
        with gr.Group():
            current_file = gr.Text(
                label="Current Target",
                value="data/samples/export.csv",
                interactive=False,
            )
            with gr.Accordion("📁", open=False):
                file_upload = gr.File(label="Upload")
        inspect_button = gr.Button("Load")

    inspect_output = gr.Dataframe(show_label=False)
    inspect_button.click(
        fn=_inspect_data, inputs=[current_file], outputs=[inspect_output]
    )

    def update_file(file):
        if file is not None:
            return file.name
        return current_file.value

    file_upload.upload(fn=update_file, inputs=[file_upload], outputs=[current_file])

    return current_file


def data(current_file):
    def _process_data(file, verbosity):
        survey = fetch_survey(file)
        report = generate_report(survey, verbosity)
        return report

    with gr.Row():
        verbosity = gr.Slider(minimum=0, maximum=3, value=2, step=1, label="Verbosity")
        process_button = gr.Button("Send to LLM", variant="primary")

    process_output = gr.Markdown(label="Processed Data")
    temp_plot = gr.LinePlot(
        x="Time",
        y="Temperature",
        color="Sensor",
        title="Temperature Trends",
        tooltip=["Time", "Temperature", "Sensor"],
        height=400,
        width=700,
    )
    process_button.click(fn=fetch_plot, inputs=[current_file], outputs=[temp_plot])

    process_button.click(
        fn=_process_data, inputs=[current_file, verbosity], outputs=process_output
    )

    return process_output


def email(process_output):
    with gr.Row():
        email_input = gr.Dropdown(
            label="Recipients",
            multiselect=True,
            allow_custom_value=True,
            info="Enter email address(es) to send the report to (press Enter to add multiple addresses)",
        )
        email_button = gr.Button("Send Email", size="sm")

    def send_email(email_addresses, report_content):
        if not email_addresses:
            return "Please enter at least one email address."

        if not report_content:
            return "No report content available. Please generate a report first."

        success = send_report_email(email_addresses, report_content)
        if success:
            return f"Email sent successfully to {', '.join(email_addresses)}"
        else:
            return (
                "Failed to send email. Please check your configuration and try again."
            )

    email_output = gr.Markdown(label="Email Status")
    email_button.click(
        fn=send_email, inputs=[email_input, process_output], outputs=[email_output]
    )