File size: 5,676 Bytes
333f068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import database
import app_logic
import visualization

# 1. Initialize Database Schema and Sample Data on startup
database.init_db()

# --- Helper Functions for UI Logic ---

def update_dropdown(category):
    """

    Triggered when Category changes. 

    Updates the Product list and clears the review textbox.

    """
    names = app_logic.get_products_by_category(category)
    return gr.Dropdown(choices=names, value=names[0] if names else None), ""

def refresh_owner_dashboard():
    """

    Fetches the latest data for the Table and the latest Plotly Chart.

    Used by the Refresh button and automatic updates.

    """
    table_data = app_logic.get_all_reviews()
    chart_figure = visualization.generate_sentiment_pie_chart()
    return table_data, chart_figure

def validate_input(text):
    """

    Real-time 'Sense Check'. 

    Disables the Submit button unless at least 3 words are typed.

    """
    word_count = len(text.strip().split())
    if word_count >= 3:
        # Enable button and make it primary (colored)
        return gr.update(interactive=True, variant="primary")
    else:
        # Disable button and make it secondary (gray)
        return gr.update(interactive=False, variant="secondary")

# --- UI Layout using Gradio Blocks ---

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# ๐Ÿ‘— Women's Clothing Portal")
    gr.Markdown("### AI-Powered Customer Feedback & Business Intelligence")

    with gr.Tabs():
        
        # TAB 1: CUSTOMER INTERFACE
        with gr.TabItem("Customer"):
            gr.Markdown("#### Submit your Review")
            with gr.Row():
                with gr.Column():
                    category_input = gr.Dropdown(
                        choices=["Jeans", "Tops", "Kurti", "Leggings"], 
                        value="Jeans", 
                        label="Category"
                    )
                    product_input = gr.Dropdown(choices=[], label="Product Name")
                    rating_input = gr.Slider(1, 5, step=1, value=5, label="Rating (1-5)")
                    
                    review_input = gr.Textbox(
                        label="Review Text", 
                        placeholder="Please write at least 3 words about the fit or quality...", 
                        lines=4
                    )
                    
                    # Submit button starts as DISABLED
                    submit_btn = gr.Button("Submit Review", variant="secondary", interactive=False)
                    status_output = gr.Textbox(label="Submission Status")

        # TAB 2: OWNER DASHBOARD
        with gr.TabItem("Owner Dashboard"):
            gr.Markdown("#### Live Sentiment Analytics")
            with gr.Row():
                with gr.Column(scale=2):
                    # Plotly Chart from visualization.py
                    sentiment_plot = gr.Plot(label="Sentiment Distribution")
                
                with gr.Column(scale=1):
                    refresh_btn = gr.Button("๐Ÿ”„ Refresh Database", variant="secondary")
                    gr.Markdown("Updates the chart and table with the latest customer entries.")
            
            data_table = gr.Dataframe(label="Detailed Customer Feedback Log", interactive=False)

        # TAB 3: AI BUSINESS INSIGHTS
        with gr.TabItem("๐Ÿ“ˆ AI Insights"):
            gr.Markdown("#### Strategic Business Analysis")
            gr.Markdown("Generate a deep-dive report based on all current customer feedback using Generative AI.")
            
            generate_report_btn = gr.Button("Generate AI Strategy Report", variant="primary")
            report_output = gr.Markdown(value="*Report will appear here after clicking the button...*")

    # --- UI Event Listeners ---

    # 1. Real-time Word Count Validation
    review_input.change(
        fn=validate_input, 
        inputs=review_input, 
        outputs=submit_btn
    )

    # 2. Category -> Product Dropdown Sync
    category_input.change(
        fn=update_dropdown, 
        inputs=category_input, 
        outputs=[product_input, review_input]
    )

    # 3. Clear review text when product changes
    product_input.change(lambda: "", outputs=review_input)

    # 4. Main Submission Logic (Saves to DB, Updates Chart, Resets UI)
    submit_btn.click(
        fn=app_logic.save_review, 
        inputs=[product_input, rating_input, review_input], 
        outputs=status_output
    ).then(
        # Auto-update the dashboard
        fn=refresh_owner_dashboard, 
        outputs=[data_table, sentiment_plot]
    ).then(
        # Clear the text box
        fn=lambda: "", outputs=review_input
    ).then(
        # Re-disable the button
        fn=lambda: gr.update(interactive=False, variant="secondary"), 
        outputs=submit_btn
    )

    # 5. Manual Dashboard Refresh
    refresh_btn.click(
        fn=refresh_owner_dashboard, 
        outputs=[data_table, sentiment_plot]
    )

    # 6. AI Report Generation
    generate_report_btn.click(
        fn=app_logic.generate_business_report,
        outputs=report_output
    )

    # 7. Initial Load (Ensures data is visible when the page first opens)
    demo.load(refresh_owner_dashboard, outputs=[data_table, sentiment_plot])
    demo.load(
        fn=update_dropdown, 
        inputs=category_input, 
        outputs=[product_input, review_input]
    )

# --- Launch the Application ---
if __name__ == "__main__":
    # Use share=True if you want to create a public link
    demo.launch()