lamekemal commited on
Commit
513cce0
·
verified ·
1 Parent(s): 549a8f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -140
app.py CHANGED
@@ -1,148 +1,49 @@
 
1
  import torch
2
- from datasets import load_dataset
3
- from transformers import (
4
- AutoTokenizer,
5
- AutoModelForCausalLM,
6
- BitsAndBytesConfig,
7
- TrainingArguments,
8
- )
9
- from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
10
- from trl import SFTTrainer
11
-
12
- # 1. Configurations
13
- base_model = "mistralai/Mistral-7B-Instruct-v0.3"
14
- new_model_dir = "./mistral-7b-brvm-finetuned"
15
- output_dir = "./results"
16
-
17
- # 2. Device
18
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
19
- print(f"Utilisation du périphérique: {device}")
20
- if torch.cuda.is_available():
21
- print(f"GPU: {torch.cuda.get_device_name(0)} - "
22
- f"Mémoire: {torch.cuda.get_device_properties(0).total_memory / (1024**3):.2f} GB")
23
-
24
- # 3. Dataset
25
- dataset = load_dataset("lamekemal/brvm_finetune")
26
-
27
- # 4. Quantization
28
- bnb_config = BitsAndBytesConfig(
29
- load_in_4bit=True,
30
- bnb_4bit_quant_type="nf4",
31
- bnb_4bit_compute_dtype=torch.bfloat16,
32
- bnb_4bit_use_double_quant=False,
33
- )
34
-
35
- # 5. Charger modèle + tokenizer
36
- model = AutoModelForCausalLM.from_pretrained(
37
- base_model,
38
- quantization_config=bnb_config,
39
- device_map="auto",
40
- trust_remote_code=True,
41
- )
42
- model.config.use_cache = False
43
- model = prepare_model_for_kbit_training(model)
44
-
45
- tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True)
46
- if tokenizer.pad_token is None:
47
- tokenizer.pad_token = tokenizer.eos_token
48
- tokenizer.padding_side = "right"
49
-
50
- # ============================================
51
- # 10. Preprocessing (max_seq_length=512)
52
- # ============================================
53
- def tokenize_function(examples):
54
- texts = [
55
- f"Instruction: {instr}\nRéponse: {resp}"
56
- for instr, resp in zip(examples["instruction"], examples["response"])
57
- ]
58
- return tokenizer(
59
- texts,
60
- truncation=True,
61
- padding="max_length",
62
- max_length=512,
63
  )
 
 
64
 
65
- tokenized_datasets = dataset.map(tokenize_function, batched=True)
66
- # 6. LoRA config
67
- lora_config = LoraConfig(
68
- r=16,
69
- lora_alpha=32,
70
- target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
71
- "gate_proj", "up_proj", "down_proj"],
72
- lora_dropout=0.05,
73
- bias="none",
74
- task_type="CAUSAL_LM",
75
- )
76
- model = get_peft_model(model, lora_config)
77
-
78
- sft_config = SFTConfig(
79
- output_dir=output_dir,
80
- num_train_epochs=3,
81
- per_device_train_batch_size=2,
82
- gradient_accumulation_steps=4,
83
- optim="paged_adamw_32bit",
84
- save_steps=100,
85
- logging_steps=10,
86
- learning_rate=2e-4,
87
- fp16=False,
88
- bf16=torch.cuda.is_available(),
89
- max_grad_norm=0.3,
90
- warmup_ratio=0.03,
91
- group_by_length=True,
92
- lr_scheduler_type="cosine",
93
- report_to="tensorboard",
94
- evaluation_strategy="steps",
95
- eval_steps=100,
96
- save_total_limit=2,
97
- load_best_model_at_end=True,
98
- metric_for_best_model="eval_loss",
99
- max_seq_length=512,
100
- packing=False,
101
- )
102
-
103
- # ============================================
104
- # 12. TrainingArguments
105
- # ============================================
106
- use_bf16 = torch.cuda.is_available() and torch.cuda.is_bf16_supported()
107
 
108
- training_args = TrainingArguments(
109
- output_dir=output_dir,
110
- num_train_epochs=3,
111
- per_device_train_batch_size=4,
112
- gradient_accumulation_steps=2,
113
- optim="paged_adamw_8bit",
114
- save_steps=100,
115
- logging_steps=10,
116
- learning_rate=2e-4,
117
- #fp16=not use_bf16,
118
- bf16=True,
119
- max_grad_norm=0.3,
120
- warmup_ratio=0.03,
121
- group_by_length=True,
122
- lr_scheduler_type="cosine",
123
- report_to="tensorboard",
124
- eval_strategy="steps", # <-- corrige le nom
125
- eval_steps=100,
126
- save_total_limit=2,
127
- load_best_model_at_end=True,
128
- metric_for_best_model="eval_loss",
129
- )
130
 
131
- # ============================================
132
- # 13. Trainer
133
- # ============================================
134
- trainer = SFTTrainer(
135
- model=model,
136
- train_dataset=tokenized_datasets["train"],
137
- eval_dataset=tokenized_datasets["validation"],
138
- peft_config=lora_config,
139
- args=training_args
140
- )
141
 
142
- # 9. Fine-tuning
143
- trainer.train()
 
 
144
 
145
- # 10. Sauvegarde locale
146
- trainer.save_model(new_model_dir)
 
 
 
147
 
148
- print(f"✅ Modèle LoRA sauvegardé localement dans {new_model_dir}")
 
1
+ import gradio as gr
2
  import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
4
+ import os
5
+
6
+ MODEL_DIR = "./mistral-7b-brvm-finetuned"
7
+
8
+ # Fonction d’entraînement (appelle ton script de fine-tuning)
9
+ def train_model():
10
+ os.system("python finetune.py") # tu mets ton code d'entraînement dans finetune.py
11
+ return "✅ Entraînement terminé ! Le modèle est sauvegardé dans " + MODEL_DIR
12
+
13
+ # Chargement du modèle (fine-tuné si dispo, sinon base)
14
+ def load_model():
15
+ model_name = MODEL_DIR if os.path.exists(MODEL_DIR) else "mistralai/Mistral-7B-Instruct-v0.3"
16
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
17
+ model = AutoModelForCausalLM.from_pretrained(
18
+ model_name,
19
+ device_map="auto",
20
+ torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
21
+ trust_remote_code=True,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  )
23
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
24
+ return pipe
25
 
26
+ # On charge le pipeline une fois au démarrage
27
+ pipe = load_model()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ # Fonction de test du modèle
30
+ def chat(prompt):
31
+ outputs = pipe(prompt, max_new_tokens=200, do_sample=True, temperature=0.7, top_p=0.9)
32
+ return outputs[0]["generated_text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ # Interface Gradio
35
+ with gr.Blocks() as demo:
36
+ gr.Markdown("# 🐟 BRVM Finetuner (Mistral-7B)")
 
 
 
 
 
 
 
37
 
38
+ with gr.Tab("🚀 Entraînement"):
39
+ train_btn = gr.Button("Lancer l’entraînement")
40
+ train_output = gr.Textbox(label="Logs")
41
+ train_btn.click(fn=train_model, outputs=train_output)
42
 
43
+ with gr.Tab("💬 Tester le modèle"):
44
+ input_text = gr.Textbox(label="Votre question :", placeholder="Posez une question...")
45
+ output_text = gr.Textbox(label="Réponse du modèle")
46
+ submit_btn = gr.Button("Envoyer")
47
+ submit_btn.click(fn=chat, inputs=input_text, outputs=output_text)
48
 
49
+ demo.launch()