File size: 3,361 Bytes
d48bb37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
from datasets import load_dataset, Dataset, DatasetDict
import os

def train_and_deploy(write_token, repo_name, license_text):
    # トークンを環境変数に設定
    os.environ['HF_WRITE_TOKEN'] = write_token
    
    # ライセンスファイルを作成
    with open("LICENSE", "w") as f:
        f.write(license_text)
    
    # モデルとトークナイザーの読み込み
    model_name = "EleutherAI/pythia-14m"  # トレーニング対象のモデル
    model = AutoModelForCausalLM.from_pretrained(model_name)
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    
    # FBK-MT/mosel データセットの読み込み
    dataset = load_dataset("FBK-MT/mosel")
    
    # データセットのキーを確認
    print(f"Dataset keys: {dataset.keys()}")
    if "train" not in dataset:
        raise KeyError("The dataset does not contain a 'train' split.")
    if "test" not in dataset:
        raise KeyError("The dataset does not contain a 'test' split.")
    
    # データセットの最初のエントリのキーを確認
    print(f"Sample keys in 'train' split: {dataset['train'][0].keys()}")
    
    # データセットのトークン化
    def tokenize_function(examples):
        try:
            texts = examples['text']
            return tokenizer(texts, padding="max_length", truncation=True, max_length=128)
        except KeyError as e:
            print(f"KeyError: {e}")
            print(f"Available keys: {examples.keys()}")
            raise
    
    tokenized_datasets = dataset.map(tokenize_function, batched=True)
    
    # トレーニング設定
    training_args = TrainingArguments(
        output_dir="./results",
        per_device_train_batch_size=8,
        per_device_eval_batch_size=8,
        evaluation_strategy="epoch",
        save_strategy="epoch",
        logging_dir="./logs",
        logging_steps=10,
        num_train_epochs=3,  # トレーニングエポック数
        push_to_hub=True,  # Hugging Face Hubにプッシュ
        hub_token=write_token,
        hub_model_id=repo_name  # ユーザーが入力したリポジトリ名
    )
    
    # Trainerの設定
    trainer = Trainer(
        model=model,
        args=training_args,
        train_dataset=tokenized_datasets["train"],
        eval_dataset=tokenized_datasets["test"],
    )
    
    # トレーニング実行
    trainer.train()
    
    # モデルをHugging Face Hubにプッシュ
    trainer.push_to_hub()

    return f"モデルが'{repo_name}'リポジトリにデプロイされました!"

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("### pythia トレーニングとデプロイ")
    token_input = gr.Textbox(label="Hugging Face Write Token", placeholder="トークンを入力してください...")
    repo_input = gr.Textbox(label="リポジトリ名", placeholder="デプロイするリポジトリ名を入力してください...")
    license_input = gr.Textbox(label="ライセンス", placeholder="ライセンス情報を入力してください...")
    output = gr.Textbox(label="出力")
    train_button = gr.Button("デプロイ")

    train_button.click(fn=train_and_deploy, inputs=[token_input, repo_input, license_input], outputs=output)

demo.launch()