awacke1 commited on
Commit
a66cfba
·
verified ·
1 Parent(s): f567a91

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Dict
2
+ import httpx
3
+ import gradio as gr
4
+ import pandas as pd
5
+ import json
6
+
7
+ async def get_splits(dataset_name: str) -> Dict[str, List[Dict]]:
8
+ URL = f"https://datasets-server.huggingface.co/splits?dataset={dataset_name}"
9
+ async with httpx.AsyncClient() as session:
10
+ response = await session.get(URL)
11
+ return response.json()
12
+
13
+ async def get_valid_datasets() -> List[str]:
14
+ URL = f"https://huggingface.co/api/datasets"
15
+ async with httpx.AsyncClient() as session:
16
+ response = await session.get(URL)
17
+ try:
18
+ datasets = [dataset["id"] for dataset in response.json()]
19
+ except (KeyError, json.JSONDecodeError):
20
+ datasets = [] # Set a default value if the response is not in the expected format
21
+ return datasets
22
+
23
+ async def get_first_rows(dataset: str, config: str, split: str) -> Dict[str, Dict[str, List[Dict]]]:
24
+ URL = f"https://datasets-server.huggingface.co/first-rows?dataset={dataset}&config={config}&split={split}"
25
+ async with httpx.AsyncClient() as session:
26
+ response = await session.get(URL)
27
+ print(URL)
28
+ gr.Markdown(URL)
29
+ return response.json()
30
+
31
+ def get_df_from_rows(api_output):
32
+ dfFromSort = pd.DataFrame([row["row"] for row in api_output["rows"]])
33
+ try:
34
+ dfFromSort.sort_values(by=1, axis=1, ascending=True, inplace=False, kind='mergesort', na_position='last', ignore_index=False, key=None)
35
+ except:
36
+ print("Exception sorting due to keyerror?")
37
+ return dfFromSort
38
+
39
+ async def update_configs(dataset_name: str):
40
+ splits = await get_splits(dataset_name)
41
+ all_configs = sorted(set([s["config"] for s in splits["splits"]]))
42
+ return (gr.Dropdown.update(choices=all_configs, value=all_configs[0]),
43
+ splits)
44
+
45
+ async def update_splits(config_name: str, state: gr.State):
46
+ splits_for_config = sorted(set([s["split"] for s in state["splits"] if s["config"] == config_name]))
47
+ dataset_name = state["splits"][0]["dataset"]
48
+ dataset = await update_dataset(splits_for_config[0], config_name, dataset_name)
49
+ return (gr.Dropdown.update(choices=splits_for_config, value=splits_for_config[0]), dataset)
50
+
51
+ async def update_dataset(split_name: str, config_name: str, dataset_name: str):
52
+ rows = await get_first_rows(dataset_name, config_name, split_name)
53
+ df = get_df_from_rows(rows)
54
+ return df
55
+
56
+ # Guido von Roissum: https://www.youtube.com/watch?v=-DVyjdw4t9I
57
+ async def update_URL(dataset: str, config: str, split: str) -> str:
58
+ URL = f"https://datasets-server.huggingface.co/first-rows?dataset={dataset}&config={config}&split={split}"
59
+ URL = f"https://huggingface.co/datasets/{split}"
60
+ return (URL)
61
+
62
+ async def openurl(URL: str) -> str:
63
+ html = f"<a href={URL} target=_blank>{URL}</a>"
64
+ return (html)
65
+
66
+ with gr.Blocks() as demo:
67
+ gr.Markdown("<h1><center>🥫Datasetter📊 Datasets Analyzer and Transformer</center></h1>")
68
+ gr.Markdown("""<div align="center">Curated Datasets: <a href = "https://www.kaggle.com/datasets">Kaggle</a>. <a href="https://www.nlm.nih.gov/research/umls/index.html">NLM UMLS</a>. <a href="https://loinc.org/downloads/">LOINC</a>. <a href="https://www.cms.gov/medicare/icd-10/2022-icd-10-cm">ICD10 Diagnosis</a>. <a href="https://icd.who.int/dev11/downloads">ICD11</a>. <a href="https://paperswithcode.com/datasets?q=medical&v=lst&o=newest">Papers,Code,Datasets for SOTA in Medicine</a>. <a href="https://paperswithcode.com/datasets?q=mental&v=lst&o=newest">Mental</a>. <a href="https://paperswithcode.com/datasets?q=behavior&v=lst&o=newest">Behavior</a>. <a href="https://www.cms.gov/medicare-coverage-database/downloads/downloads.aspx">CMS Downloads</a>. <a href="https://www.cms.gov/medicare/fraud-and-abuse/physicianselfreferral/list_of_codes">CMS CPT and HCPCS Procedures and Services</a> """)
69
+
70
+ splits_data = gr.State()
71
+
72
+ with gr.Row():
73
+ dataset_name = gr.Dropdown(label="Dataset", interactive=True)
74
+ config = gr.Dropdown(label="Subset", interactive=True)
75
+ split = gr.Dropdown(label="Split", interactive=True)
76
+
77
+ with gr.Row():
78
+ #filterleft = gr.Textbox(label="First Column Filter",placeholder="Filter Column 1")
79
+ URLcenter = gr.Textbox(label="Dataset URL", placeholder="URL")
80
+ btn = gr.Button("Use Dataset")
81
+ #URLoutput = gr.Textbox(label="Output",placeholder="URL Output")
82
+ #URLoutput = gr.HTML(label="Output",placeholder="URL Output")
83
+ URLoutput = gr.HTML(label="Output")
84
+
85
+ with gr.Row():
86
+ dataset = gr.DataFrame(wrap=True, interactive=True)
87
+
88
+ demo.load(get_valid_datasets, inputs=None, outputs=[dataset_name])
89
+
90
+ dataset_name.change(update_configs, inputs=[dataset_name], outputs=[config, splits_data])
91
+ config.change(update_splits, inputs=[config, splits_data], outputs=[split, dataset])
92
+ split.change(update_dataset, inputs=[split, config, dataset_name], outputs=[dataset])
93
+
94
+ dataset_name.change(update_URL, inputs=[split, config, dataset_name], outputs=[URLcenter])
95
+
96
+ btn.click(openurl, [URLcenter], URLoutput)
97
+
98
+ demo.launch(debug=True)