File size: 1,138 Bytes
3531f81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5629fd7
3531f81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import json
from datetime import datetime
from pathlib import Path
from uuid import uuid4

import gradio as gr

from huggingface_hub import CommitScheduler


JSON_DATASET_DIR = Path("json_dataset")
JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)

JSON_DATASET_PATH = JSON_DATASET_DIR / f"train-{uuid4()}.json"

scheduler = CommitScheduler(
    repo_id="example-space-to-dataset-json",
    repo_type="dataset",
    folder_path=JSON_DATASET_DIR,
    path_in_repo="data",
)


def greet(name: str) -> str:
    return "Hello " + name + "!"


def save_json(name: str, greetings: str) -> None:
    with scheduler.lock:
        with JSON_DATASET_PATH.open("a") as f:
            json.dump({"name": name, "greetings": greetings, "datetime": datetime.now().isoformat()}, f)
            f.write("\n")


def get_demo():
    with gr.Row():
        greet_name = gr.Textbox(label="Name")
        greet_output = gr.Textbox(label="Greetings")
    greet_btn = gr.Button("Greet")
    greet_btn.click(fn=greet, inputs=greet_name, outputs=greet_output).success(
        fn=save_json,
        inputs=[greet_name, greet_output],
        outputs=None,
    )