Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
import time
|
6 |
+
import base64
|
7 |
+
from io import StringIO
|
8 |
+
import getpass
|
9 |
+
import os
|
10 |
+
|
11 |
+
pd.set_option('display.max_columns', None)
|
12 |
+
pd.set_option('display.max_rows', None)
|
13 |
+
|
14 |
+
|
15 |
+
DATABRICKS_INSTANCE = os.getenv("DATABRICS_INSTANCE_")
|
16 |
+
ACCESS_TOKEN = os.getenv("ACCESS_TOKEN_")
|
17 |
+
JOB_ID = os.getenv("JOB_ID_")
|
18 |
+
|
19 |
+
|
20 |
+
def get_run_status(run_id):
|
21 |
+
url = f"{DATABRICKS_INSTANCE}/api/2.1/jobs/runs/get?run_id={run_id}"
|
22 |
+
headers = {
|
23 |
+
"Authorization": f"Bearer {ACCESS_TOKEN}"
|
24 |
+
}
|
25 |
+
response = requests.get(url, headers=headers)
|
26 |
+
return response.json()
|
27 |
+
|
28 |
+
def get_databricks_file(file_path):
|
29 |
+
# Construct the DBFS URL for the file
|
30 |
+
dbfs_url = f"{DATABRICKS_INSTANCE}/api/2.0/dbfs/read"
|
31 |
+
headers = {
|
32 |
+
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
33 |
+
"Content-Type": "application/json"
|
34 |
+
}
|
35 |
+
data = {
|
36 |
+
"path": file_path
|
37 |
+
}
|
38 |
+
response = requests.get(dbfs_url, headers=headers, json=data)
|
39 |
+
#response.raise_for_status() # Raise an exception for HTTP errors
|
40 |
+
content = response.json()
|
41 |
+
file_data = base64.b64decode(content['data'])
|
42 |
+
return file_data
|
43 |
+
|
44 |
+
def get_month_label(i):
|
45 |
+
return f'M{i+1}'
|
46 |
+
|
47 |
+
data_predictions_ = get_databricks_file("dbfs:/dbfs/FileStore/forecast_alleviatetax_predictions.csv")
|
48 |
+
df = pd.read_csv(StringIO(data_predictions_.decode('utf-8')))
|
49 |
+
df['revenue'] = (df['vintage_unique_cases'] / df['predicted_monthly_payment_rate']).round(2)
|
50 |
+
df['Month'] = [get_month_label(i) for i in range(len(df))]
|
51 |
+
|
52 |
+
def update_table(start_date, end_date, window):
|
53 |
+
#############################################################################################
|
54 |
+
try:
|
55 |
+
DATABRICKS_INSTANCE = os.getenv("DATABRICS_INSTANCE_")
|
56 |
+
ACCESS_TOKEN = os.getenv("ACCESS_TOKEN_")
|
57 |
+
JOB_ID = os.getenv("JOB_ID_")
|
58 |
+
|
59 |
+
# Parameters to pass to the job
|
60 |
+
params = {
|
61 |
+
"events_api_base_url": "https://beta.datascore.ai",
|
62 |
+
"month_train_starting": str(start_date),
|
63 |
+
"month_for_prediction": str(end_date),
|
64 |
+
"moving_average_period": str(window)
|
65 |
+
}
|
66 |
+
|
67 |
+
url = f"{DATABRICKS_INSTANCE}/api/2.1/jobs/run-now"
|
68 |
+
headers = {
|
69 |
+
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
70 |
+
"Content-Type": "application/json"
|
71 |
+
}
|
72 |
+
data = {
|
73 |
+
"job_id": JOB_ID,
|
74 |
+
"notebook_params": params # Pass parameters here
|
75 |
+
}
|
76 |
+
response = requests.post(url, headers=headers, data=json.dumps(data))
|
77 |
+
#return response.json()
|
78 |
+
# Run the Job
|
79 |
+
#run_info = post_run_job(JOB_ID, params)
|
80 |
+
run_info = response.json()
|
81 |
+
run_id = run_info['run_id']
|
82 |
+
#print(f"Run ID: {run_id}")
|
83 |
+
|
84 |
+
while True:
|
85 |
+
status = get_run_status(run_id)
|
86 |
+
state = status['state']['life_cycle_state']
|
87 |
+
if state in ['TERMINATED', 'SKIPPED', 'FAILED', 'ERROR']:
|
88 |
+
print(f"Job finished with state: {state}")
|
89 |
+
break
|
90 |
+
elif state in ['PROGRES']:
|
91 |
+
print(f"Job progress: {state}")
|
92 |
+
time.sleep(30)
|
93 |
+
|
94 |
+
print(f"Run ID: {run_id}")
|
95 |
+
data_payments = get_databricks_file("dbfs:/dbfs/FileStore/forecast_alleviatetax_payments.csv")
|
96 |
+
data_metrics = get_databricks_file("dbfs:/dbfs/FileStore/forecast_alleviatetax_metrics.csv")
|
97 |
+
data_predictions = get_databricks_file("dbfs:/dbfs/FileStore/forecast_alleviatetax_predictions.csv")
|
98 |
+
df_payments = pd.read_csv(StringIO(data_payments.decode('utf-8')))
|
99 |
+
new_columns = ['vintage', 'vintage_unique_cases'] + [f'M{col}' for col in df_payments.columns[2:]]
|
100 |
+
df_metrics = pd.read_csv(StringIO(data_metrics.decode('utf-8')))
|
101 |
+
df_predictions = pd.read_csv(StringIO(data_predictions.decode('utf-8')))
|
102 |
+
df_predictions['revenue'] = (df_predictions['vintage_unique_cases'] / df_predictions['predicted_monthly_payment_rate']).round(2)
|
103 |
+
df_predictions['Month'] = [get_month_label(i) for i in range(len(df_predictions))]
|
104 |
+
except Exception as e:
|
105 |
+
return {error: str(e)}
|
106 |
+
#############################################################################################
|
107 |
+
global df # Use global variable
|
108 |
+
#df = pd.read_csv('ok_csv.csv')
|
109 |
+
#df = df_payments.copy()
|
110 |
+
df = df_predictions.copy()
|
111 |
+
return df
|
112 |
+
|
113 |
+
|
114 |
+
def save_csv(file_name):
|
115 |
+
global df # Use global variable
|
116 |
+
if file_name:
|
117 |
+
df.to_csv(file_name, index=False)
|
118 |
+
return f"File '{file_name}' saved!"
|
119 |
+
else:
|
120 |
+
return "Please set file name!"
|
121 |
+
|
122 |
+
|
123 |
+
with gr.Blocks() as demo:
|
124 |
+
with gr.Row():
|
125 |
+
start_input = gr.Textbox(label="Start Date:", placeholder="Enter date (e.g., 2023-09-01)", max_lines=1)
|
126 |
+
prediction_input = gr.Text(label="Month Prediction:", placeholder="Enter date (e.g., 2024-10-31)", max_lines=1)
|
127 |
+
range_input = gr.Slider(1, 100, 6, label="Window / Moving Average Period")
|
128 |
+
|
129 |
+
table_ = gr.DataFrame(value=df, label="Predictions (consult 'revenue' column):")
|
130 |
+
btn_actualizar = gr.Button("Run Forecast")
|
131 |
+
|
132 |
+
with gr.Row():
|
133 |
+
file_name_input = gr.Textbox(label="CSV Name", placeholder="Predictions.csv")
|
134 |
+
btn_guardar = gr.Button("Save CSV")
|
135 |
+
|
136 |
+
btn_actualizar.click(fn=update_table, inputs=[start_input, prediction_input, range_input], outputs=table_)
|
137 |
+
btn_guardar.click(fn=save_csv, inputs=file_name_input, outputs=gr.Textbox(label="File Status:"))
|
138 |
+
|
139 |
+
demo.launch()
|