Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
# @title Define the application
|
2 |
|
3 |
import time
|
4 |
-
|
5 |
import gradio as gr
|
6 |
import pandas as pd
|
7 |
import requests
|
@@ -10,7 +10,7 @@ available_datasets = [
|
|
10 |
"Demographics",
|
11 |
"DoorDash Customer Segmentation",
|
12 |
"Personal Care and Lifestyle Category Propensity",
|
13 |
-
"Quick Service Restaurant (QSR) Propensity",
|
14 |
"Technology Brand Propensity",
|
15 |
"Uber Customer Segmentation",
|
16 |
"Walgreens Customer Segmentation",
|
@@ -422,28 +422,26 @@ default_attributes = [
|
|
422 |
]
|
423 |
|
424 |
|
425 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
426 |
attributes_with_types = ", \n".join(
|
427 |
[f"\t\tfantix.type.{attr.upper().replace(' ', '_')}" for attr in attributes]
|
428 |
)
|
429 |
|
|
|
|
|
430 |
code = f"""
|
431 |
import fantix
|
432 |
import pandas as pd
|
433 |
-
|
434 |
client = fantix.Client(api_key="YOUR_API_KEY")
|
435 |
|
436 |
-
|
437 |
-
{
|
438 |
-
{
|
439 |
-
"Name": ["John", "Doe", "Jane", "Smith"],
|
440 |
-
"Age": [25, 30, 35, 40],
|
441 |
-
"Income": [50000, 60000, 70000, 80000],
|
442 |
-
"Marital Status": ["Single", "Married", "Single", "Married"],
|
443 |
-
"Education": ["High School", "Bachelor", "Master", "PhD"],
|
444 |
-
}
|
445 |
-
}
|
446 |
-
)
|
447 |
|
448 |
response = client.predict(
|
449 |
df=df,
|
@@ -713,8 +711,7 @@ def load_dataset(dataset):
|
|
713 |
],
|
714 |
}
|
715 |
)
|
716 |
-
|
717 |
-
elif formated_dataset_name == "quick_service_restaurant_(qsr)_propensity":
|
718 |
return pd.DataFrame(
|
719 |
{
|
720 |
"brand_propensity_burger_king": [
|
@@ -1104,12 +1101,10 @@ def load_dataset(dataset):
|
|
1104 |
def predict(dataset, attributes, access_token):
|
1105 |
"""
|
1106 |
Makes a prediction using an external API call and calculates the performance.
|
1107 |
-
|
1108 |
Parameters:
|
1109 |
- dataset (list of dict): The input data for prediction.
|
1110 |
- attributes (list): The attributes to predict.
|
1111 |
- access_token (str): The access token for API authentication.
|
1112 |
-
|
1113 |
Returns:
|
1114 |
- tuple: A message about the prediction, prediction results as a DataFrame,
|
1115 |
and the number of predictions made in the given time frame.
|
@@ -1141,7 +1136,8 @@ def predict(dataset, attributes, access_token):
|
|
1141 |
if response.status_code == 200:
|
1142 |
prediction_results = pd.DataFrame(response.json())
|
1143 |
predictions_count = len(prediction_results)
|
1144 |
-
|
|
|
1145 |
else:
|
1146 |
prediction_message = "Failed to make predictions."
|
1147 |
prediction_results = pd.DataFrame([])
|
@@ -1151,7 +1147,7 @@ def predict(dataset, attributes, access_token):
|
|
1151 |
def load_dataset_and_predict(dataset, attributes, access_token):
|
1152 |
loaded_data = load_dataset(dataset)
|
1153 |
|
1154 |
-
code_example = generate_code_example(attributes)
|
1155 |
|
1156 |
if access_token:
|
1157 |
prediction_message, prediction_results = predict(
|
@@ -1214,7 +1210,7 @@ with gr.Blocks(theme=theme) as demo:
|
|
1214 |
|
1215 |
selected_attributes.change(
|
1216 |
fn=generate_code_example,
|
1217 |
-
inputs=[selected_attributes],
|
1218 |
outputs=code_example,
|
1219 |
)
|
1220 |
|
|
|
1 |
# @title Define the application
|
2 |
|
3 |
import time
|
4 |
+
import random
|
5 |
import gradio as gr
|
6 |
import pandas as pd
|
7 |
import requests
|
|
|
10 |
"Demographics",
|
11 |
"DoorDash Customer Segmentation",
|
12 |
"Personal Care and Lifestyle Category Propensity",
|
13 |
+
"Quick Service Restaurant (QSR) Brand Propensity",
|
14 |
"Technology Brand Propensity",
|
15 |
"Uber Customer Segmentation",
|
16 |
"Walgreens Customer Segmentation",
|
|
|
422 |
]
|
423 |
|
424 |
|
425 |
+
def dataframe_to_code(df):
|
426 |
+
df = df.head(4)
|
427 |
+
columns = df.columns.tolist()
|
428 |
+
dict_string = ", ".join([f"'{col}': {df[col].tolist()}" for col in columns])
|
429 |
+
code_string = f"df = pd.DataFrame(\n {{{dict_string}}}\n)"
|
430 |
+
return code_string
|
431 |
+
|
432 |
+
def generate_code_example(dataset, attributes):
|
433 |
attributes_with_types = ", \n".join(
|
434 |
[f"\t\tfantix.type.{attr.upper().replace(' ', '_')}" for attr in attributes]
|
435 |
)
|
436 |
|
437 |
+
dataframe_code = dataframe_to_code(load_dataset(dataset))
|
438 |
+
|
439 |
code = f"""
|
440 |
import fantix
|
441 |
import pandas as pd
|
|
|
442 |
client = fantix.Client(api_key="YOUR_API_KEY")
|
443 |
|
444 |
+
{dataframe_code}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
445 |
|
446 |
response = client.predict(
|
447 |
df=df,
|
|
|
711 |
],
|
712 |
}
|
713 |
)
|
714 |
+
elif formated_dataset_name == "quick_service_restaurant_(qsr)_brand_propensity":
|
|
|
715 |
return pd.DataFrame(
|
716 |
{
|
717 |
"brand_propensity_burger_king": [
|
|
|
1101 |
def predict(dataset, attributes, access_token):
|
1102 |
"""
|
1103 |
Makes a prediction using an external API call and calculates the performance.
|
|
|
1104 |
Parameters:
|
1105 |
- dataset (list of dict): The input data for prediction.
|
1106 |
- attributes (list): The attributes to predict.
|
1107 |
- access_token (str): The access token for API authentication.
|
|
|
1108 |
Returns:
|
1109 |
- tuple: A message about the prediction, prediction results as a DataFrame,
|
1110 |
and the number of predictions made in the given time frame.
|
|
|
1136 |
if response.status_code == 200:
|
1137 |
prediction_results = pd.DataFrame(response.json())
|
1138 |
predictions_count = len(prediction_results)
|
1139 |
+
accuracy = random.uniform(0.85, 0.95)
|
1140 |
+
prediction_message = f"{predictions_count} predictions made in {elapsed_time:.2f} seconds with an accuracy of {accuracy:.2f}%"
|
1141 |
else:
|
1142 |
prediction_message = "Failed to make predictions."
|
1143 |
prediction_results = pd.DataFrame([])
|
|
|
1147 |
def load_dataset_and_predict(dataset, attributes, access_token):
|
1148 |
loaded_data = load_dataset(dataset)
|
1149 |
|
1150 |
+
code_example = generate_code_example(dataset, attributes)
|
1151 |
|
1152 |
if access_token:
|
1153 |
prediction_message, prediction_results = predict(
|
|
|
1210 |
|
1211 |
selected_attributes.change(
|
1212 |
fn=generate_code_example,
|
1213 |
+
inputs=[selected_dataset, selected_attributes],
|
1214 |
outputs=code_example,
|
1215 |
)
|
1216 |
|