Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| import pandas as pd | |
| import joblib | |
| # Load your model and feature list | |
| model = joblib.load("ar_overdue_model.joblib") | |
| feature_names = joblib.load("ar_model_features.joblib") | |
| def predict(company_code, document_type, amount, due_in_days): | |
| # Build the input DataFrame | |
| input_dict = { | |
| "company_code": company_code, | |
| "document_type": document_type, | |
| "amount": amount, | |
| "due_in_days": due_in_days | |
| } | |
| input_df = pd.DataFrame([input_dict]) | |
| # One-hot encode and align columns | |
| input_df = pd.get_dummies(input_df) | |
| for col in feature_names: | |
| if col not in input_df.columns: | |
| input_df[col] = 0 | |
| input_df = input_df[feature_names] | |
| # Predict | |
| proba = model.predict_proba(input_df)[0, 1] | |
| pred = model.predict(input_df)[0] | |
| return f"Overdue: {bool(pred)} (Probability: {proba:.2f})" | |
| # Define the Gradio interface | |
| iface = gr.Interface( | |
| fn=predict, | |
| inputs=[ | |
| gr.Dropdown(['CompanyA', 'CompanyB', 'CompanyC'], label="Company Code"), | |
| gr.Dropdown(['INV', 'CRN', 'DBN'], label="Document Type"), | |
| gr.Number(label="Amount"), | |
| gr.Number(label="Due In Days") | |
| ], | |
| outputs="text", | |
| title="AR Overdue Prediction", | |
| description="Enter invoice details to predict overdue probability." | |
| ) | |
| if __name__ == "__main__": | |
| # 1) Turn on the async queue so the /api/* routes get mounted | |
| iface = iface.queue() | |
| # 2) Read the HF Spaces port (default to 7860 locally) | |
| port = int(os.environ.get("PORT", 7860)) | |
| # 3) Launch on all interfaces | |
| iface.launch(server_name="0.0.0.0", server_port=port) | |