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