Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import model | |
| import os | |
| FEATURES = [ | |
| {"name": "Administrative", "desc": "Number of administrative pages visited", "type": "number", "min": 0, "max": 30}, | |
| {"name": "Administrative_Duration", "desc": "Total time spent on administrative pages (seconds)", "type": "number", "min": 0, "max": 40000}, | |
| {"name": "Informational", "desc": "Number of informational pages visited", "type": "number", "min": 0, "max": 24}, | |
| {"name": "Informational_Duration", "desc": "Total time spent on informational pages (seconds)", "type": "number", "min": 0, "max": 25480}, | |
| {"name": "ProductRelated", "desc": "Number of product-related pages visited", "type": "number", "min": 0, "max": 705}, | |
| {"name": "ProductRelated_Duration", "desc": "Total time spent on product-related pages (seconds)", "type": "number", "min": 0, "max": 63973}, | |
| {"name": "BounceRates", "desc": "Bounce rate value (0-1)", "type": "number", "step": 0.001, "min": 0, "max": 1}, | |
| {"name": "ExitRates", "desc": "Exit rate value (0-1)", "type": "number", "step": 0.001, "min": 0, "max": 1}, | |
| {"name": "PageValues", "desc": "Page value (0-361)", "type": "number", "step": 0.01, "min": 0, "max": 361}, | |
| {"name": "SpecialDay", "desc": "Special day proximity (0-1)", "type": "number", "step": 0.01, "min": 0, "max": 1}, | |
| {"name": "Month", "desc": "Month of the visit", "type": "select", "options": ["Jan", "Feb", "Mar", "Apr", "May", "June", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]}, | |
| {"name": "OperatingSystems", "desc": "Operating system (1-8)", "type": "number", "min": 1, "max": 8}, | |
| {"name": "Browser", "desc": "Browser (1-13)", "type": "number", "min": 1, "max": 13}, | |
| {"name": "Region", "desc": "Region (1-9)", "type": "number", "min": 1, "max": 9}, | |
| {"name": "TrafficType", "desc": "Traffic type (1-20)", "type": "number", "min": 1, "max": 20}, | |
| {"name": "VisitorType", "desc": "Type of visitor", "type": "select", "options": ["New_Visitor", "Returning_Visitor", "Other"]}, | |
| {"name": "Weekend", "desc": "Is weekend?", "type": "select", "options": ["True", "False"]}, | |
| ] | |
| # 1. Main Tab: Show metrics and images | |
| def main_tab(): | |
| metrics = """ | |
| **Model Metrics:** | |
| - Accuracy: 0.8619 | |
| - Precision: 0.5335 | |
| - Recall: 0.5497 | |
| - F1 Score: 0.5415 | |
| - ROC AUC Score: 0.7330 | |
| """ | |
| img_dir = "imgs" | |
| img_files = [ | |
| ("Classification Metrics", os.path.join(img_dir, "classification_metrics_bar_plot.png")), | |
| ("Confusion Matrix", os.path.join(img_dir, "confusion_matrix.png")), | |
| ("ROC Curve", os.path.join(img_dir, "roc_curve.png")), | |
| ] | |
| # Only return label and path for existing files | |
| img_comps = [(label, img) for label, img in img_files if os.path.exists(img)] | |
| return metrics, img_comps | |
| # 2. Dataset Tab: Show info and column descriptions | |
| def dataset_tab(): | |
| dataset_info = """ | |
| **Dataset:** [Online Shoppers Purchasing Intention Dataset](https://archive.ics.uci.edu/dataset/468/online+shoppers+purchasing+intention+dataset) | |
| - 18,000+ sessions, 17 features + target | |
| - Binary classification: Revenue (True/False) | |
| """ | |
| columns_html = """ | |
| <table> | |
| <thead> | |
| <tr> | |
| <th>Column</th> | |
| <th>Description</th> | |
| <th>Range/Options</th> | |
| </tr> | |
| </thead> | |
| <tbody> | |
| """ | |
| for f in FEATURES: | |
| rng = f.get("options", f"{f.get('min', '')} - {f.get('max', '')}") | |
| if isinstance(rng, list): | |
| rng = ", ".join(str(x) for x in rng) | |
| columns_html += f"<tr><td>{f['name']}</td><td>{f['desc']}</td><td>{rng}</td></tr>\n" | |
| columns_html += "</tbody></table>" | |
| return gr.Markdown(dataset_info), gr.HTML(columns_html) | |
| # 3. Model Tab: Prediction UI | |
| def predict_gradio(*inputs): | |
| row = {} | |
| for i, f in enumerate(FEATURES): | |
| row[f["name"]] = inputs[i] | |
| pred, proba = model.predict_from_row(row) | |
| return f"Prediction: {pred}\nProbabilities: {proba}" | |
| inputs = [] | |
| example_row = { | |
| 'Administrative': 3, | |
| 'Administrative_Duration': 80.0, | |
| 'Informational': 0, | |
| 'Informational_Duration': 0.0, | |
| 'ProductRelated': 20, | |
| 'ProductRelated_Duration': 500.0, | |
| 'BounceRates': 0.02, | |
| 'ExitRates': 0.04, | |
| 'PageValues': 0.0, | |
| 'SpecialDay': 0.0, | |
| 'Month': 'Nov', | |
| 'OperatingSystems': 2, | |
| 'Browser': 2, | |
| 'Region': 1, | |
| 'TrafficType': 2, | |
| 'VisitorType': 'Returning_Visitor', | |
| 'Weekend': 'False', | |
| } | |
| for f in FEATURES: | |
| label = f"{f['name']} ({f['desc']})" | |
| default = example_row.get(f['name']) | |
| if f["type"] == "select": | |
| inputs.append(gr.Dropdown(choices=f["options"], label=label, value=default)) | |
| else: | |
| step = f.get("step", 1) | |
| inputs.append(gr.Number(label=label, minimum=f["min"], maximum=f["max"], step=step, value=default)) | |
| model_tab = gr.Interface( | |
| fn=predict_gradio, | |
| inputs=inputs, | |
| outputs="text", | |
| title="Model Prediction", | |
| description="Fill in all fields. Ranges and descriptions are shown for each input.", | |
| ) | |
| with gr.Blocks() as demo: | |
| with gr.Tab("Main"): | |
| metrics, imgs = main_tab() | |
| gr.Markdown(metrics) | |
| for label, img_path in imgs: | |
| gr.Image(value=img_path, label=label) | |
| with gr.Tab("Dataset"): | |
| dataset_info, columns_md = dataset_tab() | |
| # gr.Markdown(dataset_info.value) | |
| # gr.Markdown(columns_md.value) | |
| with gr.Tab("Model"): | |
| model_tab.render() | |
| demo.launch() | |