import os import subprocess import gradio as gr # Ensure model is trained if not (os.path.exists("model/model.pkl") and os.path.exists("model/encoders.pkl")): print("Model files not found. Training model...") os.makedirs("model", exist_ok=True) subprocess.run(["python", "train.py"], check=True) # ✅ Import AFTER model is guaranteed to exist from predict import predict_transaction # Gradio UI def predict_ui(check_id, employee_id, total, discount_amount, item_count, time, terminal_id): if not employee_id or not time or not terminal_id: return "Please provide all required fields." return predict_transaction({ "check_id": check_id, "employee_id": employee_id.strip(), "total": total, "discount_amount": discount_amount, "item_count": item_count, "time": time.strip(), "terminal_id": terminal_id.strip() }) demo = gr.Interface( fn=predict_ui, inputs=[ gr.Number(label="Check ID"), gr.Text(label="Employee ID"), gr.Number(label="Total"), gr.Number(label="Discount Amount"), gr.Number(label="Item Count"), gr.Text(label="Time (HH:MM)"), gr.Text(label="Terminal ID"), ], outputs=gr.Text(label="Suspicious (1=True, 0=False)"), title="Suspicious Transaction Detector" ) demo.launch()