Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,50 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
from typing import Dict, Any
|
4 |
from data_loader import read_dask_data, read_polars_data, read_another_dask_data
|
5 |
|
6 |
-
#
|
|
|
7 |
def load_and_process_data(dataset_choice: str, num_rows: int) -> Dict[str, Any]:
|
8 |
try:
|
9 |
-
#
|
10 |
dataset_mapping = {
|
11 |
"Dask Data": read_dask_data,
|
12 |
"Polars Data": read_polars_data,
|
13 |
"Another Dask Data": read_another_dask_data
|
14 |
}
|
15 |
|
16 |
-
# Fetch the
|
17 |
data_loader = dataset_mapping.get(dataset_choice)
|
18 |
if not data_loader:
|
|
|
19 |
return {"error": "Invalid dataset choice."}
|
20 |
|
21 |
-
#
|
22 |
data = data_loader()
|
|
|
|
|
23 |
processed_data = data.head(num_rows)
|
24 |
|
|
|
25 |
return {
|
26 |
-
"processed_data": processed_data.to_dict()
|
27 |
}
|
28 |
except Exception as e:
|
29 |
-
#
|
|
|
30 |
print(f"Error processing data: {str(e)}")
|
31 |
return {"error": "Unable to process data. Please check the logs for details."}
|
32 |
|
33 |
-
#
|
|
|
34 |
def create_interface():
|
35 |
-
#
|
36 |
dataset_choice = gr.components.Dropdown(
|
37 |
choices=["Dask Data", "Polars Data", "Another Dask Data"],
|
38 |
label="Select Dataset"
|
@@ -41,7 +53,7 @@ def create_interface():
|
|
41 |
minimum=1, maximum=100, value=5, label="Number of Rows to Display"
|
42 |
)
|
43 |
|
44 |
-
#
|
45 |
with gr.Blocks() as demo:
|
46 |
gr.Markdown("# Enhanced Dataset Loader Demo")
|
47 |
gr.Markdown("Interact with various datasets and select the amount of data to display.")
|
@@ -51,8 +63,13 @@ def create_interface():
|
|
51 |
processed_data_output = gr.JSON(label="Processed Data")
|
52 |
processed_data_output.render()
|
53 |
|
|
|
54 |
demo.add(dataset_choice, num_rows, processed_data_output, load_and_process_data)
|
|
|
|
|
55 |
demo.launch()
|
56 |
|
|
|
57 |
if __name__ == "__main__":
|
58 |
create_interface()
|
|
|
|
1 |
+
```python
|
2 |
+
# Import the necessary libraries:
|
3 |
+
# - `gradio` is a library for creating interactive web interfaces
|
4 |
+
# - `typing` provides type annotations for Python
|
5 |
+
# - `data_loader` is a custom module that contains functions to read different types of data
|
6 |
+
|
7 |
import gradio as gr
|
8 |
from typing import Dict, Any
|
9 |
from data_loader import read_dask_data, read_polars_data, read_another_dask_data
|
10 |
|
11 |
+
# Define a function called `load_and_process_data` that takes a dataset choice and the number of rows to display
|
12 |
+
# This function is responsible for loading and processing the data based on the user's input
|
13 |
def load_and_process_data(dataset_choice: str, num_rows: int) -> Dict[str, Any]:
|
14 |
try:
|
15 |
+
# Create a mapping of dataset choices to their corresponding data loading functions
|
16 |
dataset_mapping = {
|
17 |
"Dask Data": read_dask_data,
|
18 |
"Polars Data": read_polars_data,
|
19 |
"Another Dask Data": read_another_dask_data
|
20 |
}
|
21 |
|
22 |
+
# Fetch the appropriate data loading function based on the user's dataset choice
|
23 |
data_loader = dataset_mapping.get(dataset_choice)
|
24 |
if not data_loader:
|
25 |
+
# If the dataset choice is invalid, return an error message
|
26 |
return {"error": "Invalid dataset choice."}
|
27 |
|
28 |
+
# Load the data using the selected data loading function
|
29 |
data = data_loader()
|
30 |
+
|
31 |
+
# Process the data to show the specified number of rows
|
32 |
processed_data = data.head(num_rows)
|
33 |
|
34 |
+
# Convert the processed data to a dictionary for JSON serialization
|
35 |
return {
|
36 |
+
"processed_data": processed_data.to_dict()
|
37 |
}
|
38 |
except Exception as e:
|
39 |
+
# If an exception occurs during data processing, log the error
|
40 |
+
# and return an error message
|
41 |
print(f"Error processing data: {str(e)}")
|
42 |
return {"error": "Unable to process data. Please check the logs for details."}
|
43 |
|
44 |
+
# Define a function called `create_interface` that creates a Gradio interface
|
45 |
+
# The interface allows the user to select a dataset and the number of rows to display
|
46 |
def create_interface():
|
47 |
+
# Define the input components for the interface
|
48 |
dataset_choice = gr.components.Dropdown(
|
49 |
choices=["Dask Data", "Polars Data", "Another Dask Data"],
|
50 |
label="Select Dataset"
|
|
|
53 |
minimum=1, maximum=100, value=5, label="Number of Rows to Display"
|
54 |
)
|
55 |
|
56 |
+
# Define the layout of the Gradio interface
|
57 |
with gr.Blocks() as demo:
|
58 |
gr.Markdown("# Enhanced Dataset Loader Demo")
|
59 |
gr.Markdown("Interact with various datasets and select the amount of data to display.")
|
|
|
63 |
processed_data_output = gr.JSON(label="Processed Data")
|
64 |
processed_data_output.render()
|
65 |
|
66 |
+
# Add the input components and the data processing function to the interface
|
67 |
demo.add(dataset_choice, num_rows, processed_data_output, load_and_process_data)
|
68 |
+
|
69 |
+
# Launch the Gradio interface
|
70 |
demo.launch()
|
71 |
|
72 |
+
# Execute the `create_interface` function when the script is run
|
73 |
if __name__ == "__main__":
|
74 |
create_interface()
|
75 |
+
```
|