Spaces:
Sleeping
Sleeping
“Fabrizio
commited on
Commit
·
50b83fc
1
Parent(s):
8ceff96
feat: Initialize Gradio interface with dataset selection, prediction, and authentication
Browse files- README.md +1 -1
- app.py +157 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Demographic 33k Alpha Ui
|
3 |
-
emoji:
|
4 |
colorFrom: pink
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
|
|
1 |
---
|
2 |
title: Demographic 33k Alpha Ui
|
3 |
+
emoji: 🤖
|
4 |
colorFrom: pink
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
available_datasets = [
|
5 |
+
"Demographic",
|
6 |
+
"Health",
|
7 |
+
"Financial",
|
8 |
+
"Social",
|
9 |
+
"Economic",
|
10 |
+
"Political",
|
11 |
+
]
|
12 |
+
|
13 |
+
default_dataset = "Demographic"
|
14 |
+
|
15 |
+
available_attributes = [
|
16 |
+
"Income",
|
17 |
+
"Age",
|
18 |
+
"Marital Status",
|
19 |
+
"Education",
|
20 |
+
]
|
21 |
+
|
22 |
+
default_attributes = ["Income", "Age"]
|
23 |
+
|
24 |
+
|
25 |
+
def load_dataset(dataset):
|
26 |
+
"""
|
27 |
+
Load and return data based on the 'dataset' parameter.
|
28 |
+
|
29 |
+
Parameters:
|
30 |
+
- dataset (str): The name of the dataset to load. Currently, this parameter is not used as the function returns a static DataFrame.
|
31 |
+
|
32 |
+
Returns:
|
33 |
+
- pd.DataFrame: A pandas DataFrame containing the loaded data.
|
34 |
+
"""
|
35 |
+
return pd.DataFrame(
|
36 |
+
{
|
37 |
+
"Name": ["John", "Doe", "Jane", "Smith"],
|
38 |
+
"Age": [25, 30, 35, 40],
|
39 |
+
"Income": [50000, 60000, 70000, 80000],
|
40 |
+
"Marital Status": ["Single", "Married", "Single", "Married"],
|
41 |
+
"Education": ["High School", "Bachelor", "Master", "PhD"],
|
42 |
+
}
|
43 |
+
)
|
44 |
+
|
45 |
+
|
46 |
+
def predict(dataset, attributes, access_token):
|
47 |
+
"""
|
48 |
+
Simulates predictions based on the provided dataset and attributes. Requires an access token for authentication.
|
49 |
+
|
50 |
+
Parameters:
|
51 |
+
- dataset (str): The name of the dataset on which to base predictions. This parameter is currently not used as the function returns a static prediction.
|
52 |
+
- attributes (list of str): The attributes selected for prediction. This parameter is currently not used as the function returns a static prediction.
|
53 |
+
- access_token (str): The access token required for authentication to make predictions.
|
54 |
+
|
55 |
+
Returns:
|
56 |
+
- tuple: A tuple containing a prediction message (str) and a pandas DataFrame with prediction results.
|
57 |
+
|
58 |
+
Raises:
|
59 |
+
- gr.Error: If the access token is not provided, an error is raised indicating that an access token is required.
|
60 |
+
"""
|
61 |
+
if not access_token:
|
62 |
+
raise gr.Error(
|
63 |
+
"Access token missing or invalid. Please ensure you have a valid access token."
|
64 |
+
)
|
65 |
+
|
66 |
+
prediction_message = "200 predictions made in 2.5 seconds."
|
67 |
+
prediction_results = pd.DataFrame(
|
68 |
+
{
|
69 |
+
"Name": ["John", "Doe", "Jane", "Smith"],
|
70 |
+
"Predicted Value": [25000, 30000, 35000, 40000],
|
71 |
+
}
|
72 |
+
)
|
73 |
+
return prediction_message, prediction_results
|
74 |
+
|
75 |
+
|
76 |
+
def load_dataset_and_predict(dataset, attributes, access_token):
|
77 |
+
"""
|
78 |
+
Combines data loading and prediction into a single step, requiring an access token for the prediction part. Uses the 'load_dataset' and 'predict' functions to load the data and generate predictions.
|
79 |
+
|
80 |
+
Parameters:
|
81 |
+
- dataset (str): The name of the dataset to load and predict on.
|
82 |
+
- attributes (list of str): The attributes selected for making predictions.
|
83 |
+
- access_token (str): The access token required for authentication to make predictions.
|
84 |
+
|
85 |
+
Returns:
|
86 |
+
- tuple: A tuple containing the loaded pandas DataFrame, a prediction message (str), and a pandas DataFrame with prediction results.
|
87 |
+
|
88 |
+
Note:
|
89 |
+
- If the access token is not provided, the prediction part is skipped, and the prediction message and results will be returned as empty or None.
|
90 |
+
"""
|
91 |
+
loaded_data = load_dataset(dataset)
|
92 |
+
|
93 |
+
if access_token:
|
94 |
+
prediction_message, prediction_results = predict(
|
95 |
+
dataset, attributes, access_token
|
96 |
+
)
|
97 |
+
else:
|
98 |
+
prediction_message = "No access token provided, prediction skipped."
|
99 |
+
prediction_results = None
|
100 |
+
|
101 |
+
return loaded_data, prediction_message, prediction_results
|
102 |
+
|
103 |
+
|
104 |
+
interface_theme = gr.themes.Default()
|
105 |
+
|
106 |
+
with gr.Blocks(theme=interface_theme) as demo:
|
107 |
+
gr.Markdown("### Authenticate")
|
108 |
+
access_token = gr.Textbox(
|
109 |
+
type="password",
|
110 |
+
label="Access Token",
|
111 |
+
placeholder="Enter your access token here.",
|
112 |
+
)
|
113 |
+
|
114 |
+
with gr.Row():
|
115 |
+
with gr.Column():
|
116 |
+
gr.Markdown("### Select Dataset and Attributes")
|
117 |
+
|
118 |
+
selected_dataset = gr.Dropdown(
|
119 |
+
choices=available_datasets,
|
120 |
+
label="Select Dataset",
|
121 |
+
value=default_dataset,
|
122 |
+
)
|
123 |
+
|
124 |
+
selected_attributes = gr.Dropdown(
|
125 |
+
choices=available_attributes,
|
126 |
+
label="Select Attributes",
|
127 |
+
info="You can select multiple attributes.",
|
128 |
+
multiselect=True,
|
129 |
+
value=default_attributes,
|
130 |
+
)
|
131 |
+
|
132 |
+
gr.Markdown("### Dataset Preview")
|
133 |
+
dataset_preview = gr.Dataframe()
|
134 |
+
|
135 |
+
predict_button = gr.Button("Predict Attributes")
|
136 |
+
|
137 |
+
gr.Markdown("### Prediction Results")
|
138 |
+
prediction_preview = gr.Dataframe()
|
139 |
+
prediction_label = gr.Markdown("")
|
140 |
+
|
141 |
+
selected_dataset.change(
|
142 |
+
fn=load_dataset, inputs=selected_dataset, outputs=dataset_preview
|
143 |
+
)
|
144 |
+
|
145 |
+
predict_button.click(
|
146 |
+
fn=predict,
|
147 |
+
inputs=[selected_dataset, selected_attributes, access_token],
|
148 |
+
outputs=[prediction_label, prediction_preview],
|
149 |
+
)
|
150 |
+
|
151 |
+
demo.load(
|
152 |
+
fn=load_dataset_and_predict,
|
153 |
+
inputs=[selected_dataset, selected_attributes, access_token],
|
154 |
+
outputs=[dataset_preview, prediction_label, prediction_preview],
|
155 |
+
)
|
156 |
+
|
157 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
requests==2.31.0
|
2 |
+
pandas==2.2.1
|