remove data persist feature from app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from datasets import load_dataset, Dataset
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
# Define the dataset name and split
|
| 6 |
DATASET_NAME = "Thang/wikides"
|
| 7 |
SPLIT = "train"
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
try:
|
| 11 |
-
dataset = load_dataset(DATASET_NAME, split=SPLIT)
|
| 12 |
-
except Exception as e:
|
| 13 |
-
# If the dataset fails to load, raise a more descriptive error.
|
| 14 |
-
# This will cause the Hugging Face Space to fail during startup,
|
| 15 |
-
# which is the desired behavior for a critical dependency.
|
| 16 |
-
raise RuntimeError(f"Failed to load dataset '{DATASET_NAME}': {e}")
|
| 17 |
-
|
| 18 |
-
# Function to fetch data with a limit
|
| 19 |
def get_data_with_limit(limit: int = 50):
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
The limit defaults to 50 if not provided or if it's invalid.
|
| 23 |
"""
|
| 24 |
# Type and value checking for the limit parameter
|
| 25 |
if not isinstance(limit, int) or limit < 1:
|
| 26 |
limit = 50
|
| 27 |
-
|
| 28 |
-
# Ensure the limit doesn't exceed the dataset size
|
| 29 |
-
num_rows = len(dataset)
|
| 30 |
-
if limit > num_rows:
|
| 31 |
-
limit = num_rows
|
| 32 |
|
| 33 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Slice the dataset to get the required number of rows and convert to a dictionary
|
| 35 |
return dataset.select(range(limit)).to_dict()
|
| 36 |
except Exception as e:
|
| 37 |
-
# Return a structured error response if something goes wrong
|
| 38 |
return {"error": "Internal Server Error", "message": str(e)}
|
| 39 |
|
| 40 |
# Create the Gradio interface
|
|
@@ -47,7 +42,7 @@ demo = gr.Interface(
|
|
| 47 |
gr.JSON(label="Data")
|
| 48 |
],
|
| 49 |
title="WikiDES Data API",
|
| 50 |
-
description=f"A simple API to access data from the **{DATASET_NAME}** dataset
|
| 51 |
examples=[
|
| 52 |
[10],
|
| 53 |
[25],
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from datasets import load_dataset, Dataset
|
|
|
|
| 3 |
|
| 4 |
# Define the dataset name and split
|
| 5 |
DATASET_NAME = "Thang/wikides"
|
| 6 |
SPLIT = "train"
|
| 7 |
|
| 8 |
+
# Function to fetch data with a limit on demand
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def get_data_with_limit(limit: int = 50):
|
| 10 |
"""
|
| 11 |
+
Loads and fetches data from the dataset with a specified limit on each call.
|
| 12 |
The limit defaults to 50 if not provided or if it's invalid.
|
| 13 |
"""
|
| 14 |
# Type and value checking for the limit parameter
|
| 15 |
if not isinstance(limit, int) or limit < 1:
|
| 16 |
limit = 50
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
try:
|
| 19 |
+
# Load the dataset on demand.
|
| 20 |
+
# This will download the data each time if not in cache,
|
| 21 |
+
# but prevents it from being stored on the disk permanently.
|
| 22 |
+
dataset = load_dataset(DATASET_NAME, split=SPLIT)
|
| 23 |
+
|
| 24 |
+
# Ensure the limit doesn't exceed the dataset size
|
| 25 |
+
num_rows = len(dataset)
|
| 26 |
+
if limit > num_rows:
|
| 27 |
+
limit = num_rows
|
| 28 |
+
|
| 29 |
# Slice the dataset to get the required number of rows and convert to a dictionary
|
| 30 |
return dataset.select(range(limit)).to_dict()
|
| 31 |
except Exception as e:
|
| 32 |
+
# Return a structured error response if something goes wrong
|
| 33 |
return {"error": "Internal Server Error", "message": str(e)}
|
| 34 |
|
| 35 |
# Create the Gradio interface
|
|
|
|
| 42 |
gr.JSON(label="Data")
|
| 43 |
],
|
| 44 |
title="WikiDES Data API",
|
| 45 |
+
description=f"A simple API to access data from the **{DATASET_NAME}** dataset. The dataset is loaded on-demand for each request. The default limit is 50.",
|
| 46 |
examples=[
|
| 47 |
[10],
|
| 48 |
[25],
|