from typing import List, Dict import httpx import gradio as gr import pandas as pd import json async def get_valid_datasets() -> Dict[str, List[str]]: URL = f"https://huggingface.co/api/datasets" async with httpx.AsyncClient() as session: response = await session.get(URL) try: datasets = [dataset["id"] for dataset in response.json()] except (KeyError, json.JSONDecodeError): datasets = [] # Set a default value if the response is not in the expected format return gr.Dropdown.update(choices=datasets, value="awacke1/ChatbotMemory.csv") async def get_splits(dataset_name: str) -> Dict[str, List[Dict]]: URL = f"https://datasets-server.huggingface.co/splits?dataset={dataset_name}" async with httpx.AsyncClient() as session: response = await session.get(URL) return response.json() async def get_valid_datasets_old() -> Dict[str, List[str]]: URL = f"https://datasets-server.huggingface.co/valid" async with httpx.AsyncClient() as session: response = await session.get(URL) datasets = response.json()["valid"] return gr.Dropdown.update(choices=datasets, value="awacke1/ChatbotMemory.csv") # The one to watch: https://huggingface.co/rungalileo # rungalileo/medical_transcription_40 async def get_valid_datasets_old2() -> Dict[str, List[str]]: URL = f"https://datasets-server.huggingface.co/valid" async with httpx.AsyncClient() as session: response = await session.get(URL) try: datasets = response.json()["valid"] except (KeyError, json.JSONDecodeError): datasets = [] # Set a default value if the response is not in the expected format return gr.Dropdown.update(choices=datasets, value="awacke1/ChatbotMemory.csv") async def get_first_rows(dataset: str, config: str, split: str) -> Dict[str, Dict[str, List[Dict]]]: URL = f"https://datasets-server.huggingface.co/first-rows?dataset={dataset}&config={config}&split={split}" async with httpx.AsyncClient() as session: response = await session.get(URL) print(URL) gr.Markdown(URL) return response.json() def get_df_from_rows(api_output): dfFromSort = pd.DataFrame([row["row"] for row in api_output["rows"]]) try: dfFromSort.sort_values(by=1, axis=1, ascending=True, inplace=False, kind='mergesort', na_position='last', ignore_index=False, key=None) except: print("Exception sorting due to keyerror?") return dfFromSort async def update_configs(dataset_name: str): splits = await get_splits(dataset_name) all_configs = sorted(set([s["config"] for s in splits["splits"]])) return (gr.Dropdown.update(choices=all_configs, value=all_configs[0]), splits) async def update_splits(config_name: str, state: gr.State): splits_for_config = sorted(set([s["split"] for s in state["splits"] if s["config"] == config_name])) dataset_name = state["splits"][0]["dataset"] dataset = await update_dataset(splits_for_config[0], config_name, dataset_name) return (gr.Dropdown.update(choices=splits_for_config, value=splits_for_config[0]), dataset) async def update_dataset(split_name: str, config_name: str, dataset_name: str): rows = await get_first_rows(dataset_name, config_name, split_name) df = get_df_from_rows(rows) return df # Guido von Roissum: https://www.youtube.com/watch?v=-DVyjdw4t9I async def update_URL(dataset: str, config: str, split: str) -> str: URL = f"https://datasets-server.huggingface.co/first-rows?dataset={dataset}&config={config}&split={split}" URL = f"https://huggingface.co/datasets/{split}" return (URL) async def openurl(URL: str) -> str: html = f"{URL}" return (html) with gr.Blocks() as demo: gr.Markdown("