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] )