kasim90 commited on
Commit
39d5e72
·
verified ·
1 Parent(s): 11a9206

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -81
app.py CHANGED
@@ -3,107 +3,82 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments,
3
  from peft import LoraConfig, get_peft_model
4
  from datasets import load_dataset
5
  import gradio as gr
 
6
  import spaces
7
 
8
- #----------------------------
9
- import time
10
- import gradio as gr
11
 
12
- #--------------------
 
 
 
 
 
 
 
 
 
 
 
13
 
 
 
 
14
 
15
  @spaces.GPU
16
- def ana():
17
- # === 1️⃣ MODEL VE TOKENIZER YÜKLEME ===
18
- MODEL_NAME = "mistralai/Mistral-7B-v0.1" # Hugging Face model adı
19
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
20
-
21
- # === 2️⃣ CPU OPTİMİZASYONU ===
22
- torch_dtype = torch.float32 # CPU için uygun dtype
23
- model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch_dtype)
24
-
25
- # === 3️⃣ LoRA AYARLARI ===
26
- lora_config = LoraConfig(
27
- r=8,
28
- lora_alpha=32,
29
- lora_dropout=0.1,
30
- bias="none",
31
- target_modules=["q_proj", "v_proj"],
32
- )
33
- model = get_peft_model(model, lora_config)
34
-
35
- # === 4️⃣ VERİ SETİ ===
36
- dataset = load_dataset("oscar", "unshuffled_deduplicated_tr", trust_remote_code=True) # trust_remote_code=True
37
- subset = dataset["train"].shuffle(seed=42).select(range(10000)) # Küçük subset seçiyoruz (10.000 örnek)
38
-
39
- # === 5️⃣ TOKENLEŞTİRME FONKSİYONU ===
40
- @spaces.GPU
41
- def tokenize_function(examples):
42
- return tokenizer(examples["text"], truncation=True, max_length=512)
43
-
44
- tokenized_datasets = subset.map(tokenize_function, batched=True)
45
-
46
- # === 6️⃣ EĞİTİM AYARLARI ===
47
- # Eğitimde kaç adım olduğunu hesaplayalım
48
- train_size = len(tokenized_datasets) # 10,000 örnek
49
- batch_size = 1 # Batch size 1
50
- num_epochs = 1 # 1 epoch eğitimi
51
- max_steps = (train_size // batch_size) * num_epochs # max_steps hesapla
52
-
53
- training_args = TrainingArguments(
54
- output_dir="./mistral_lora",
55
- per_device_train_batch_size=1,
56
- gradient_accumulation_steps=16,
57
- learning_rate=5e-4,
58
- num_train_epochs=1,
59
- max_steps=max_steps, # Buraya max_steps parametresini ekliyoruz
60
- save_steps=500,
61
- save_total_limit=2,
62
- logging_dir="./logs",
63
- logging_steps=10,
64
- optim="adamw_torch",
65
- train_dataset=split_dataset["train"],
66
- eval_dataset=split_dataset["test"],
67
- no_cuda=True, # GPU kullanılmıyor
68
- )
69
- train_model()
70
 
 
 
 
 
 
71
 
72
- # === 7️⃣ MODEL EĞİTİMİ ===
73
  @spaces.GPU
74
  def train_model():
75
- trainer = Trainer(
76
- model=model,
77
- args=training_args,
78
- train_dataset=tokenized_datasets,
79
- )
80
  trainer.train()
81
 
82
- # Eğitimi başlat
83
- demo = gr.ChatInterface(
84
- slow_echo,
85
- type="messages",
86
- flagging_mode="manual",
87
- flagging_options=["Like", "Spam", "Inappropriate", "Other"],
88
- save_history=True,
89
- )
90
-
91
  @spaces.GPU
92
  def slow_echo(message, history):
93
- for i in range(len(message)):
94
- ana()
95
- time.sleep(0.05)
96
-
97
- yield "You typed: " + message[: i + 1]
98
 
99
  demo = gr.ChatInterface(
100
  slow_echo,
101
- type="messages",
102
  flagging_mode="manual",
103
  flagging_options=["Like", "Spam", "Inappropriate", "Other"],
104
  save_history=True,
105
  )
106
 
107
  if __name__ == "__main__":
108
- demo.launch(share=True)
109
-
 
3
  from peft import LoraConfig, get_peft_model
4
  from datasets import load_dataset
5
  import gradio as gr
6
+ import time
7
  import spaces
8
 
9
+ # === 1️⃣ MODEL VE TOKENIZER YÜKLEME ===
10
+ MODEL_NAME = "mistralai/Mistral-7B-v0.1"
11
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
12
 
13
+ torch_dtype = torch.float32
14
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch_dtype)
15
+
16
+ # === 2️⃣ LoRA AYARLARI ===
17
+ lora_config = LoraConfig(
18
+ r=8,
19
+ lora_alpha=32,
20
+ lora_dropout=0.1,
21
+ bias="none",
22
+ target_modules=["q_proj", "v_proj"],
23
+ )
24
+ model = get_peft_model(model, lora_config)
25
 
26
+ # === 3️⃣ VERİ SETİ ===
27
+ dataset = load_dataset("oscar", "unshuffled_deduplicated_tr", trust_remote_code=True)
28
+ subset = dataset["train"].shuffle(seed=42).select(range(10000))
29
 
30
  @spaces.GPU
31
+ def tokenize_function(examples):
32
+ return tokenizer(examples["text"], truncation=True, max_length=512)
33
+
34
+ tokenized_datasets = subset.map(tokenize_function, batched=True)
35
+
36
+ # === 4️⃣ EĞİTİM AYARLARI ===
37
+ batch_size = 1
38
+ num_epochs = 1
39
+ max_steps = (len(tokenized_datasets) // batch_size) * num_epochs
40
+
41
+ training_args = TrainingArguments(
42
+ output_dir="./mistral_lora",
43
+ per_device_train_batch_size=1,
44
+ gradient_accumulation_steps=16,
45
+ learning_rate=5e-4,
46
+ num_train_epochs=1,
47
+ max_steps=max_steps,
48
+ save_steps=500,
49
+ save_total_limit=2,
50
+ logging_dir="./logs",
51
+ logging_steps=10,
52
+ optim="adamw_torch",
53
+ no_cuda=True,
54
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ trainer = Trainer(
57
+ model=model,
58
+ args=training_args,
59
+ train_dataset=tokenized_datasets,
60
+ )
61
 
 
62
  @spaces.GPU
63
  def train_model():
 
 
 
 
 
64
  trainer.train()
65
 
66
+ # === 5️⃣ CHAT ARAYÜZÜ ===
 
 
 
 
 
 
 
 
67
  @spaces.GPU
68
  def slow_echo(message, history):
69
+ response = "Model henüz eğitilmedi. Lütfen eğitimi başlatın."
70
+ if model:
71
+ response = f"You typed: {message}"
72
+ return response
 
73
 
74
  demo = gr.ChatInterface(
75
  slow_echo,
76
+ type="text",
77
  flagging_mode="manual",
78
  flagging_options=["Like", "Spam", "Inappropriate", "Other"],
79
  save_history=True,
80
  )
81
 
82
  if __name__ == "__main__":
83
+ train_model()
84
+ demo.launch(share=True)