acecalisto3 commited on
Commit
f7e0713
·
verified ·
1 Parent(s): 03e859c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -2
app.py CHANGED
@@ -469,6 +469,14 @@ with gr.Blocks(css=css) as demo:
469
  current_item_idx += 1
470
 
471
 
 
 
 
 
 
 
 
 
472
  @search_button.click(inputs=search_bar, outputs=button_groups + buttons + [generated_texts_state])
473
  def search_dataset_from_search_button(search_query):
474
  yield from _search_datasets(search_query)
@@ -538,7 +546,7 @@ with gr.Blocks(css=css) as demo:
538
  def show_dataset_from_button(search_query, *buttons_values, i):
539
  dataset_name, tags = buttons_values[2 * i : 2 * i + 2]
540
  yield from _show_dataset(search_query, dataset_name, tags)
541
-
542
  for i, (dataset_name_button, tags_button) in enumerate(batched(buttons, 2)):
543
  dataset_name_button.click(partial(show_dataset_from_button, i=i), inputs=show_dataset_inputs, outputs=show_dataset_outputs, js=scroll_to_top_js)
544
  tags_button.click(partial(show_dataset_from_button, i=i), inputs=show_dataset_inputs, outputs=show_dataset_outputs, js=scroll_to_top_js)
@@ -642,5 +650,37 @@ with gr.Blocks(css=css) as demo:
642
  yield {search_page: gr.Column(visible=True)}
643
 
644
 
645
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
646
 
 
 
469
  current_item_idx += 1
470
 
471
 
472
+ def upload_file(file):
473
+ # Upload file to Hugging Face Hub
474
+ # Replace with your own file upload logic
475
+ # For example, you can use the `huggingface_hub` library to upload the file
476
+ # See: https://huggingface.co/docs/hub/uploading
477
+ pass
478
+
479
+
480
  @search_button.click(inputs=search_bar, outputs=button_groups + buttons + [generated_texts_state])
481
  def search_dataset_from_search_button(search_query):
482
  yield from _search_datasets(search_query)
 
546
  def show_dataset_from_button(search_query, *buttons_values, i):
547
  dataset_name, tags = buttons_values[2 * i : 2 * i + 2]
548
  yield from _show_dataset(search_query, dataset_name, tags)
549
+
550
  for i, (dataset_name_button, tags_button) in enumerate(batched(buttons, 2)):
551
  dataset_name_button.click(partial(show_dataset_from_button, i=i), inputs=show_dataset_inputs, outputs=show_dataset_outputs, js=scroll_to_top_js)
552
  tags_button.click(partial(show_dataset_from_button, i=i), inputs=show_dataset_inputs, outputs=show_dataset_outputs, js=scroll_to_top_js)
 
650
  yield {search_page: gr.Column(visible=True)}
651
 
652
 
653
+ @demo.upload(inputs=[search_bar, dataset_title, dataset_content, dataset_dataframe, select_namespace_dropdown, visibility_radio], outputs=[save_dataset_button, open_dataset_message])
654
+ def upload_dataset(search_query, dataset_name, dataset_content, df, namespace, visability):
655
+ dataset_name, tags = dataset_name.strip("# ").split("\ntags:", 1)
656
+ dataset_name, tags = dataset_name.strip(), tags.strip()
657
+ csv_header, preview_df = parse_preview_df(dataset_content)
658
+ # Remove dummy "id" columns
659
+ for column_name, values in preview_df.to_dict(orient="series").items():
660
+ try:
661
+ if [int(v) for v in values] == list(range(len(preview_df))):
662
+ preview_df = preview_df.drop(columns=column_name)
663
+ if [int(v) for v in values] == list(range(1, len(preview_df) + 1)):
664
+ preview_df = preview_df.drop(columns=column_name)
665
+ except Exception:
666
+ pass
667
+ columns = list(preview_df)
668
+ output: list[Optional[dict]] = [None] * NUM_ROWS
669
+ output[:len(preview_df)] = [{"idx": i, **x} for i, x in enumerate(preview_df.to_dict(orient="records"))]
670
+ yield {
671
+ save_dataset_button: gr.Button(f"💾 Save Dataset {namespace}/{dataset_name}" + (" (private)" if visability != "public" else ""), interactive=False),
672
+ open_dataset_message: gr.Markdown(f"Uploading dataset {dataset_name}...")
673
+ }
674
+ token = oauth_token.token if oauth_token else save_dataset_hf_token
675
+ repo_id = f"{namespace}/{dataset_name}"
676
+ dataset_url = f"{URL}?q={search_query.replace(' ', '+')}&dataset={dataset_name.replace(' ', '+')}&tags={tags.replace(' ', '+')}"
677
+ create_repo(repo_id=repo_id, repo_type="dataset", private=visability!="public", exist_ok=True, token=token)
678
+ df.to_csv(f"hf://datasets/{repo_id}/data.csv", storage_options={"token": token}, index=False)
679
+ DatasetCard(DATASET_CARD_CONTENT.format(title=title, content=content, url=URL, dataset_url=dataset_url, model_id=model_id, search_query=search_query)).push_to_hub(repo_id=repo_id, repo_type="dataset", token=token)
680
+ gr.Info(f"✅ Dataset saved at {repo_id}")
681
+ additional_message = "PS: You can also save datasets under your account in the Settings ;)"
682
+ yield {open_dataset_message: gr.Markdown(f"# 🎉 Yay ! Your dataset has been saved to [{repo_id}](https://huggingface.co/datasets/{repo_id}) !\n\nDataset link: [https://huggingface.co/datasets/{repo_id}](https://huggingface.co/datasets/{repo_id})\n\n{additional_message}", visible=True)}
683
+ print(f"Saved {dataset_name}!")
684
+
685
 
686
+ demo.launch()