File size: 3,851 Bytes
497a0a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# src/app.py
import gradio as gr
import os
from src.langgraph_workflow import create_workflow, WorkflowState # Ensure WorkflowState is imported if needed

langgraph_app = create_workflow()

def process_feedback_file(uploaded_file):
    if uploaded_file is None:
        return "Please upload a file.", None, "No file uploaded."

    file_path = uploaded_file.name
    print(f"Processing file: {file_path}")

    # Initialize state (match the new WorkflowState definition)
    initial_state = {
        "file_path": file_path,
        "raw_df": None, "processed_df": None, "feedback_distribution": {},
        "instructor_rating_distribution": {}, "average_scores": {}, "correlations": {},
        "charts_output_dir": "", "chart_filepaths": {}, "analysis_text": "",
        "report_path": "", "error_message": None
    }

    try:
        final_state = langgraph_app.invoke(initial_state)

        if final_state.get("error_message"):
            error_msg = f"Workflow error: {final_state['error_message']}"
            print(error_msg)
            # Return None for HTML content if error occurs
            return None, None, error_msg

        final_report_path = final_state.get("report_path")

        if final_report_path and os.path.exists(final_report_path):
            status_message = f"Report generated successfully! Stored at: {final_report_path}"
            print(status_message)

            # Read HTML content for preview (might be large)
            # Alternative: Use an iframe in gr.HTML pointing to the file if possible & allowed
            try:
                with open(final_report_path, 'r', encoding='utf-8') as f:
                     report_html_content = f.read()
            except Exception as read_e:
                 report_html_content = f"<p>Report generated at {final_report_path}, but failed to read preview: {read_e}</p>"
                 status_message += " (Preview load error)"

            # Return HTML content for preview, file path for download, and status
            return report_html_content, final_report_path, status_message
        else:
            no_report_msg = "Workflow completed, but the final HTML report path is missing or file doesn't exist."
            print(no_report_msg)
            return None, None, no_report_msg # Ensure None is returned for HTML content

    except Exception as e:
        err_msg = f"An unexpected error occurred in Gradio app: {str(e)}"
        print(err_msg)
        import traceback
        traceback.print_exc()
        return None, None, "Unexpected application error." # Return None for HTML

# Define Gradio Interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# Employee Training Evaluation System ")
    gr.Markdown("Upload your CSV/Excel file (wide format). Generates an HTML report with analysis text and linked chart images (saved in 'outputs').")

    with gr.Row():
        file_input = gr.File(label="Upload Feedback File (.csv, .xlsx)", file_types=[".csv", ".xlsx"])

    submit_button = gr.Button("Generate Report", variant="primary")

    gr.Markdown("## Report Output")
    status_output = gr.Textbox(label="Processing Status", interactive=False)
    # Display HTML content directly
    html_output = gr.HTML(label="Generated Report Preview")
    # Provide download link to the generated HTML file
    download_output = gr.File(label="Download Full Report (.html)", interactive=False)

    submit_button.click(
        fn=process_feedback_file,
        inputs=[file_input],
        outputs=[html_output, download_output, status_output] # Match return order
    )

    gr.Markdown("---")
    gr.Markdown("### Instructions :after reprt be generated you can download it:")


if __name__ == "__main__":
    # Ensure necessary directories exist
    if not os.path.exists("outputs"): os.makedirs("outputs")
    demo.launch()