Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
|
|
|
|
| 5 |
model = joblib.load("ar_overdue_model.joblib")
|
| 6 |
feature_names = joblib.load("ar_model_features.joblib")
|
| 7 |
|
| 8 |
def predict(company_code, document_type, amount, due_in_days):
|
|
|
|
| 9 |
input_dict = {
|
| 10 |
"company_code": company_code,
|
| 11 |
"document_type": document_type,
|
|
@@ -13,25 +16,35 @@ def predict(company_code, document_type, amount, due_in_days):
|
|
| 13 |
"due_in_days": due_in_days
|
| 14 |
}
|
| 15 |
input_df = pd.DataFrame([input_dict])
|
|
|
|
| 16 |
input_df = pd.get_dummies(input_df)
|
| 17 |
for col in feature_names:
|
| 18 |
if col not in input_df.columns:
|
| 19 |
input_df[col] = 0
|
| 20 |
input_df = input_df[feature_names]
|
| 21 |
-
|
|
|
|
| 22 |
pred = model.predict(input_df)[0]
|
| 23 |
return f"Overdue: {bool(pred)} (Probability: {proba:.2f})"
|
| 24 |
|
|
|
|
| 25 |
iface = gr.Interface(
|
| 26 |
fn=predict,
|
| 27 |
inputs=[
|
| 28 |
gr.Dropdown(['CompanyA', 'CompanyB', 'CompanyC'], label="Company Code"),
|
| 29 |
-
gr.Dropdown(['INV', 'CRN', 'DBN'],
|
| 30 |
gr.Number(label="Amount"),
|
| 31 |
gr.Number(label="Due In Days")
|
| 32 |
],
|
| 33 |
-
outputs="text"
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import joblib
|
| 5 |
|
| 6 |
+
# Load your model and feature list
|
| 7 |
model = joblib.load("ar_overdue_model.joblib")
|
| 8 |
feature_names = joblib.load("ar_model_features.joblib")
|
| 9 |
|
| 10 |
def predict(company_code, document_type, amount, due_in_days):
|
| 11 |
+
# Build the input DataFrame
|
| 12 |
input_dict = {
|
| 13 |
"company_code": company_code,
|
| 14 |
"document_type": document_type,
|
|
|
|
| 16 |
"due_in_days": due_in_days
|
| 17 |
}
|
| 18 |
input_df = pd.DataFrame([input_dict])
|
| 19 |
+
# One-hot encode and align columns
|
| 20 |
input_df = pd.get_dummies(input_df)
|
| 21 |
for col in feature_names:
|
| 22 |
if col not in input_df.columns:
|
| 23 |
input_df[col] = 0
|
| 24 |
input_df = input_df[feature_names]
|
| 25 |
+
# Predict
|
| 26 |
+
proba = model.predict_proba(input_df)[0, 1]
|
| 27 |
pred = model.predict(input_df)[0]
|
| 28 |
return f"Overdue: {bool(pred)} (Probability: {proba:.2f})"
|
| 29 |
|
| 30 |
+
# Define the Gradio interface
|
| 31 |
iface = gr.Interface(
|
| 32 |
fn=predict,
|
| 33 |
inputs=[
|
| 34 |
gr.Dropdown(['CompanyA', 'CompanyB', 'CompanyC'], label="Company Code"),
|
| 35 |
+
gr.Dropdown(['INV', 'CRN', 'DBN'], label="Document Type"),
|
| 36 |
gr.Number(label="Amount"),
|
| 37 |
gr.Number(label="Due In Days")
|
| 38 |
],
|
| 39 |
+
outputs="text",
|
| 40 |
+
title="AR Overdue Prediction",
|
| 41 |
+
description="Enter invoice details to predict overdue probability."
|
| 42 |
)
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
+
# 1) Turn on the async queue so the /api/* routes get mounted
|
| 46 |
+
iface = iface.queue()
|
| 47 |
+
# 2) Read the HF Spaces port (default to 7860 locally)
|
| 48 |
+
port = int(os.environ.get("PORT", 7860))
|
| 49 |
+
# 3) Launch on all interfaces
|
| 50 |
+
iface.launch(server_name="0.0.0.0", server_port=port)
|