Spaces:
Sleeping
Sleeping
File size: 1,647 Bytes
eadf2c1 04d9958 386f746 04d9958 eadf2c1 04d9958 eadf2c1 04d9958 eadf2c1 04d9958 eadf2c1 04d9958 eadf2c1 04d9958 eadf2c1 04d9958 eadf2c1 04d9958 eadf2c1 |
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 |
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)
|