Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
demo
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
+
from warnings import filterwarnings
|
| 2 |
+
filterwarnings('ignore')
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import uuid
|
| 6 |
+
import joblib
|
| 7 |
+
import json
|
| 8 |
import gradio as gr
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from huggingface_hub import CommitScheduler
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
|
| 13 |
+
# Configure the logging functionality
|
| 14 |
+
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
| 15 |
+
log_folder = log_file.parent
|
| 16 |
+
|
| 17 |
+
repo_id = "operand-logs"
|
| 18 |
+
|
| 19 |
+
# Create a commit scheduler
|
| 20 |
+
scheduler = CommitScheduler(
|
| 21 |
+
repo_id=repo_id,
|
| 22 |
+
repo_type="dataset",
|
| 23 |
+
folder_path=log_folder,
|
| 24 |
+
path_in_repo="data",
|
| 25 |
+
every=2
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# # Load the saved model
|
| 29 |
+
# #insurance_charge_predictor = joblib.load('model.joblib')
|
| 30 |
+
|
| 31 |
+
# # Define the input features
|
| 32 |
+
|
| 33 |
+
# #numeric_features = ['age', 'bmi', 'children']
|
| 34 |
+
# #categorical_features = ['sex', 'smoker', 'region']
|
| 35 |
+
|
| 36 |
+
# age_input = gr.Number(label="Age")
|
| 37 |
+
# bmi_input = gr.Number(label="BMI")
|
| 38 |
+
# children_input = gr.Number(label="Children")
|
| 39 |
+
|
| 40 |
+
# # sex: ['female' 'male']
|
| 41 |
+
# # smoker: ['yes' 'no']
|
| 42 |
+
# # region: ['southwest' 'southeast' 'northwest' 'northeast']
|
| 43 |
+
|
| 44 |
+
# sex_input = gr.Dropdown(['female','male'],label='Sex')
|
| 45 |
+
# smoker_input = gr.Dropdown(['yes','no'],label='Smoker')
|
| 46 |
+
# region_input = gr.Dropdown(['southwest', 'southeast', 'northwest', 'northeast'],label='Region')
|
| 47 |
+
|
| 48 |
+
# model_output = gr.Label(label="charges")
|
| 49 |
+
|
| 50 |
+
# Define the predict function which will take features, convert to dataframe and make predictions using the saved model
|
| 51 |
+
# the functions runs when 'Submit' is clicked or when a API request is made
|
| 52 |
+
|
| 53 |
+
def dprocess(age, bmi, children, sex, smoker, region):
|
| 54 |
+
|
| 55 |
+
#Index(['age', 'sex', 'bmi', 'children', 'smoker', 'region'], dtype='object')
|
| 56 |
+
|
| 57 |
+
sample = {
|
| 58 |
+
'age': age,
|
| 59 |
+
'sex': sex,
|
| 60 |
+
'bmi': bmi,
|
| 61 |
+
'children': children,
|
| 62 |
+
'smoker': smoker,
|
| 63 |
+
'region': region
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
data_point = pd.DataFrame([sample])
|
| 67 |
+
|
| 68 |
+
prediction = insurance_charge_predictor.predict(data_point).tolist()
|
| 69 |
+
|
| 70 |
+
with scheduler.lock:
|
| 71 |
+
with log_file.open("a") as f:
|
| 72 |
+
f.write(json.dumps(
|
| 73 |
+
{
|
| 74 |
+
'age': age,
|
| 75 |
+
'sex': sex,
|
| 76 |
+
'bmi': bmi,
|
| 77 |
+
'children': children,
|
| 78 |
+
'smoker': smoker,
|
| 79 |
+
'region': region,
|
| 80 |
+
'prediction': prediction[0]
|
| 81 |
+
}
|
| 82 |
+
))
|
| 83 |
+
f.write("\n")
|
| 84 |
+
|
| 85 |
+
return prediction[0]
|
| 86 |
+
|
| 87 |
+
# Set-up the Gradio UI
|
| 88 |
+
textbox = gr.Textbox(label='Command:')
|
| 89 |
+
company = gr.Radio(label='Company:',
|
| 90 |
+
choices=["aws", "google", "IBM", "Meta", "msft"],
|
| 91 |
+
value="aws")
|
| 92 |
|
| 93 |
+
# Create Gradio interface
|
| 94 |
+
# For the inputs parameter of Interface provide [textbox,company] with outputs parameter of Interface provide prediction
|
| 95 |
+
demo = gr.Interface(fn=dprocess,
|
| 96 |
+
inputs=[textbox, company],
|
| 97 |
+
outputs="text",
|
| 98 |
+
title="operand data automation CLI",
|
| 99 |
+
description="",
|
| 100 |
+
theme=gr.themes.Soft())
|
| 101 |
|
| 102 |
+
demo.queue()
|
| 103 |
demo.launch()
|