update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from datasets import load_dataset, Dataset
|
| 3 |
-
from huggingface_hub import hf_hub_download
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
# Define the dataset name and
|
| 7 |
-
DATASET_NAME = "wikides"
|
| 8 |
-
|
| 9 |
-
# For a real application, you'd use a known, public Hugging Face dataset.
|
| 10 |
-
# The user might need to upload a wikides file.
|
| 11 |
-
# Let's assume a dummy local file for the sake of the API demonstration.
|
| 12 |
-
# In a real-world scenario, you would use `load_dataset("wikides", split="train")`
|
| 13 |
-
# However, the search results indicate that the dataset is not a simple direct load.
|
| 14 |
-
# So we'll use a simplified example to demonstrate the API functionality.
|
| 15 |
-
# Assuming a local dummy CSV file named 'wikides_data.csv' exists.
|
| 16 |
-
# We'll create a dummy file for the demonstration.
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
if not os.path.exists("wikides_data.csv"):
|
| 20 |
-
with open("wikides_data.csv", "w") as f:
|
| 21 |
-
f.write("id,title,text\n")
|
| 22 |
-
for i in range(100):
|
| 23 |
-
f.write(f"{i},Title {i},This is the text for article {i} from WikiDES.\n")
|
| 24 |
-
|
| 25 |
-
# Load the dataset from the local file
|
| 26 |
-
# In a real scenario you would have the actual dataset files.
|
| 27 |
try:
|
| 28 |
-
dataset = load_dataset(
|
| 29 |
except Exception as e:
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Function to fetch data with a limit
|
| 33 |
def get_data_with_limit(limit: int = 50):
|
|
@@ -35,6 +21,7 @@ def get_data_with_limit(limit: int = 50):
|
|
| 35 |
Fetches data from the dataset with a specified limit.
|
| 36 |
The limit defaults to 50 if not provided or if it's invalid.
|
| 37 |
"""
|
|
|
|
| 38 |
if not isinstance(limit, int) or limit < 1:
|
| 39 |
limit = 50
|
| 40 |
|
|
@@ -44,16 +31,13 @@ def get_data_with_limit(limit: int = 50):
|
|
| 44 |
limit = num_rows
|
| 45 |
|
| 46 |
try:
|
| 47 |
-
# Slice the dataset to get the required number of rows
|
| 48 |
return dataset.select(range(limit)).to_dict()
|
| 49 |
except Exception as e:
|
| 50 |
-
# Return a structured error response
|
| 51 |
return {"error": "Internal Server Error", "message": str(e)}
|
| 52 |
|
| 53 |
# Create the Gradio interface
|
| 54 |
-
# We use `gr.Interface` to define the API endpoint.
|
| 55 |
-
# The `inputs` and `outputs` define the schema.
|
| 56 |
-
# We set `api_name="data"` to expose the function at the `/data` endpoint.
|
| 57 |
demo = gr.Interface(
|
| 58 |
fn=get_data_with_limit,
|
| 59 |
inputs=[
|
|
@@ -63,7 +47,7 @@ demo = gr.Interface(
|
|
| 63 |
gr.JSON(label="Data")
|
| 64 |
],
|
| 65 |
title="WikiDES Data API",
|
| 66 |
-
description="A simple API to access data from the
|
| 67 |
examples=[
|
| 68 |
[10],
|
| 69 |
[25],
|
|
|
|
| 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 |
+
# Load the actual dataset from Hugging Face Hub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
|
|
|
| 21 |
Fetches data from the dataset with a specified limit.
|
| 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 |
|
|
|
|
| 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 during data retrieval
|
| 38 |
return {"error": "Internal Server Error", "message": str(e)}
|
| 39 |
|
| 40 |
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=get_data_with_limit,
|
| 43 |
inputs=[
|
|
|
|
| 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 with a configurable limit. The default limit is 50.",
|
| 51 |
examples=[
|
| 52 |
[10],
|
| 53 |
[25],
|