File size: 3,572 Bytes
9d2dbc7
 
 
 
 
b3d1867
 
 
 
9d2dbc7
 
b3d1867
9d2dbc7
 
 
 
 
b3d1867
9d2dbc7
 
 
3324f1e
b75f4af
9d2dbc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3d1867
4a0f178
b3d1867
 
b198cf7
 
b3d1867
 
 
 
 
 
 
 
 
 
 
 
 
b198cf7
 
 
 
9d2dbc7
 
8065555
 
 
 
 
 
 
0574c71
8065555
 
 
 
 
 
 
0574c71
8065555
 
 
 
 
 
9d2dbc7
 
 
60bd4b0
9d2dbc7
5cf6ed9
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import json
import shutil
import gradio as gr
from datasets import load_dataset
from huggingface_hub import upload_file
from io import StringIO
import pandas as pd
import datetime

HF_TOKEN = os.environ.get("HF_TOKEN", None)
DIALOGUES_DATASET = "ArmelRandy/MT_dialogues"

def load_data():
    dataset = load_dataset("ArmelR/oasst1_guanaco_english", use_auth_token=HF_TOKEN)
    return dataset

    
samples = load_data()
splits = list(samples.keys())
languages = ["Wolof"]
print(f"current directory {os.getcwd()}")
print(f"total path {os.path.dirname(os.path.realpath(__file__))}")

custom_css = """
#banner-image {
    display: block;
    margin-left: auto;
    margin-right: auto;
}
#chat-message {
    font-size: 14px;
    min-height: 300px;
}
"""
def caller_split(s):
    return 0, samples[s][0]["prompt"], samples[s][0]["completion"]

def identity(index, split):
    ds = samples[split][index]
    return ds["prompt"], ds["completion"]
    
def save(index, language, split, prompt, completion):
    buffer = StringIO()
    now = datetime.datetime.now()
    timestamp = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")
    file_name = f"prompts_{timestamp}.jsonl"
    if len(prompt) != 0 and len(completion) != 0 :
        print("Saving ...")
        data = {"prompt": prompt, "completion": completion, "language": language, "index": index}
        pd.DataFrame([data]).to_json(buffer, orient="records", lines=True)
        # Push to Hub
        upload_file(
            path_in_repo=f"{now.date()}/{now.hour}/{file_name}",
            path_or_fileobj=buffer.getvalue().encode(),
            repo_id=DIALOGUES_DATASET,
            token=HF_TOKEN,
            repo_type="dataset",
        )
    
        # Clean and rerun
        buffer.close()
        next_index = min(1+index, len(samples[split])-1)
        return next_index, samples[split][next_index]["prompt"], samples[split][next_index]["completion"], "", ""
    else :
        return index, samples[split][index]["prompt"], samples[split][index]["completion"], "", ""

with gr.Blocks(analytics_enabled=False, css=custom_css) as demo:
    gr.HTML("""<h1 align="center">MT💫</h1>""")
    # gr.Markdown("""""")
    with gr.Blocks():
        with gr.Row() : 
            split = gr.Dropdown(choices=splits, label="Dataset split", value=splits[0])
        with gr.Row() :
            index_example = gr.Slider(minimum=0, maximum=10000, step=1, value=0, interactive=True, info=f"Index of the chosen instruction-output pair.")
        with gr.Row() :
            with gr.Column():
                prompt = gr.Textbox(label="prompt")
            with gr.Column():
                completion = gr.Code(label="Completion")
    with gr.Blocks():
        with gr.Row() :
            language = gr.Dropdown(choices=languages, label="Translation language", value=languages[0])
        with gr.Row() :
            with gr.Column() :
                translated_prompt = gr.Textbox(label="Translated prompt")
            with gr.Column() :
                translated_completion = gr.Textbox(label="Translated completion")
        with gr.Row() :
            button = gr.Button(value="Submit")
    
    split.change(caller_split, inputs=[split], outputs=[index_example, prompt, completion])
    index_example.release(identity, inputs=[index_example, split], outputs=[prompt, completion])
    button.click(save, inputs=[index_example, language, split, translated_prompt, translated_completion], outputs=[index_example, prompt, completion, translated_prompt, translated_completion])
    
demo.launch(debug=True)