Spaces:
Build error
Build error
Ffftdtd5dtft
commited on
Commit
•
2b3f1b9
1
Parent(s):
d0333e5
Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,11 @@ import matplotlib.pyplot as plt
|
|
7 |
import matplotlib.animation as animation
|
8 |
import time
|
9 |
from tqdm import tqdm
|
10 |
-
from transformers import AutoTokenizer,
|
11 |
from diffusers import DiffusionPipeline
|
12 |
from huggingface_hub import login, HfApi, Repository
|
13 |
from dotenv import load_dotenv
|
|
|
14 |
|
15 |
# Cargar variables de entorno
|
16 |
load_dotenv()
|
@@ -83,9 +84,54 @@ def push_to_hub(local_dir, repo_name):
|
|
83 |
|
84 |
def load_model(model_name):
|
85 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
86 |
-
model =
|
87 |
return tokenizer, model
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
def main():
|
90 |
while True:
|
91 |
try:
|
@@ -149,65 +195,25 @@ def main():
|
|
149 |
|
150 |
# Definir los argumentos de entrenamiento
|
151 |
training_args = TrainingArguments(
|
152 |
-
output_dir="outputs/unified_model",
|
153 |
-
evaluation_strategy="epoch",
|
154 |
-
learning_rate=9e-4,
|
155 |
per_device_train_batch_size=2,
|
156 |
per_device_eval_batch_size=16,
|
157 |
-
num_train_epochs=1,
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
)
|
162 |
|
163 |
# Definir el optimizador
|
164 |
-
optimizer = AdamW(unified_model.parameters(), lr=
|
165 |
|
|
|
166 |
train_losses = []
|
167 |
eval_losses = []
|
168 |
|
169 |
-
|
170 |
-
model.train()
|
171 |
-
epoch = 0
|
172 |
-
total_steps = len(train_loader)
|
173 |
-
for step, batch in enumerate(train_loader):
|
174 |
-
start_time = time.time()
|
175 |
-
input_ids = [batch[f"input_ids_{name}"].to("cpu") for name in tokenizers.keys()]
|
176 |
-
attention_mask = [batch[f"attention_mask_{name}"].to("cpu") for name in tokenizers.keys()]
|
177 |
-
labels = batch["label"].to("cpu")
|
178 |
-
optimizer.zero_grad()
|
179 |
-
outputs = model(input_ids)
|
180 |
-
loss = nn.CrossEntropyLoss()(outputs, labels)
|
181 |
-
loss.backward()
|
182 |
-
optimizer.step()
|
183 |
-
|
184 |
-
elapsed_time = time.time() - start_time
|
185 |
-
estimated_total_time = total_steps * (elapsed_time / (step + 1))
|
186 |
-
estimated_remaining_time = estimated_total_time - elapsed_time
|
187 |
-
|
188 |
-
if step % args.logging_steps == 0:
|
189 |
-
train_losses.append(loss.item())
|
190 |
-
print(f"Step {step}/{total_steps}, Loss: {loss.item()}, Estimated remaining time: {estimated_remaining_time:.2f} seconds")
|
191 |
-
|
192 |
-
epoch += 1
|
193 |
-
model.eval()
|
194 |
-
eval_loss = 0
|
195 |
-
with torch.no_grad():
|
196 |
-
for batch in eval_loader:
|
197 |
-
input_ids = [batch[f"input_ids_{name}"].to("cpu") for name in tokenizers.keys()]
|
198 |
-
attention_mask = [batch[f"attention_mask_{name}"].to("cpu") for name in tokenizers.keys()]
|
199 |
-
labels = batch["label"].to("cpu")
|
200 |
-
outputs = model(input_ids)
|
201 |
-
loss = nn.CrossEntropyLoss()(outputs, labels)
|
202 |
-
eval_loss += loss.item()
|
203 |
-
|
204 |
-
eval_loss /= len(eval_loader)
|
205 |
-
eval_losses.append(eval_loss)
|
206 |
-
print(f"Epoch {epoch}/{args.num_train_epochs}, Evaluation Loss: {eval_loss}")
|
207 |
-
|
208 |
train(unified_model, train_loader, eval_loader, training_args)
|
209 |
|
210 |
-
# Visualizar pérdidas
|
211 |
fig, ax = plt.subplots()
|
212 |
ax.set_xlabel("Epochs")
|
213 |
ax.set_ylabel("Loss")
|
@@ -241,6 +247,10 @@ def main():
|
|
241 |
# Subir el modelo y el tokenizador a Hugging Face
|
242 |
push_to_hub(local_dir, repo_name="Ffftdtd5dtft/my_model")
|
243 |
|
|
|
|
|
|
|
|
|
244 |
break
|
245 |
except Exception as e:
|
246 |
print(f"Error: {e}")
|
|
|
7 |
import matplotlib.animation as animation
|
8 |
import time
|
9 |
from tqdm import tqdm
|
10 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments
|
11 |
from diffusers import DiffusionPipeline
|
12 |
from huggingface_hub import login, HfApi, Repository
|
13 |
from dotenv import load_dotenv
|
14 |
+
import gradio as gr
|
15 |
|
16 |
# Cargar variables de entorno
|
17 |
load_dotenv()
|
|
|
84 |
|
85 |
def load_model(model_name):
|
86 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
87 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
88 |
return tokenizer, model
|
89 |
|
90 |
+
def train(model, train_loader, eval_loader, args):
|
91 |
+
model.train()
|
92 |
+
epoch = 0
|
93 |
+
total_steps = len(train_loader)
|
94 |
+
for step, batch in enumerate(train_loader):
|
95 |
+
start_time = time.time()
|
96 |
+
input_ids = [batch[f"input_ids_{name}"].to("cpu") for name in tokenizers.keys()]
|
97 |
+
attention_mask = [batch[f"attention_mask_{name}"].to("cpu") for name in tokenizers.keys()]
|
98 |
+
labels = batch["label"].to("cpu")
|
99 |
+
optimizer.zero_grad()
|
100 |
+
outputs = model(input_ids)
|
101 |
+
loss = nn.CrossEntropyLoss()(outputs, labels)
|
102 |
+
loss.backward()
|
103 |
+
optimizer.step()
|
104 |
+
|
105 |
+
elapsed_time = time.time() - start_time
|
106 |
+
estimated_total_time = total_steps * (elapsed_time / (step + 1))
|
107 |
+
estimated_remaining_time = estimated_total_time - elapsed_time
|
108 |
+
|
109 |
+
if step % args.logging_steps == 0:
|
110 |
+
train_losses.append(loss.item())
|
111 |
+
print(f"Step {step}/{total_steps}, Loss: {loss.item()}, Estimated remaining time: {estimated_remaining_time:.2f} seconds")
|
112 |
+
|
113 |
+
epoch += 1
|
114 |
+
model.eval()
|
115 |
+
eval_loss = 0
|
116 |
+
with torch.no_grad():
|
117 |
+
for batch in eval_loader:
|
118 |
+
input_ids = [batch[f"input_ids_{name}"].to("cpu") for name in tokenizers.keys()]
|
119 |
+
attention_mask = [batch[f"attention_mask_{name}"].to("cpu") for name in tokenizers.keys()]
|
120 |
+
labels = batch["label"].to("cpu")
|
121 |
+
outputs = model(input_ids)
|
122 |
+
loss = nn.CrossEntropyLoss()(outputs, labels)
|
123 |
+
eval_loss += loss.item()
|
124 |
+
|
125 |
+
eval_loss /= len(eval_loader)
|
126 |
+
eval_losses.append(eval_loss)
|
127 |
+
print(f"Epoch {epoch}/{args.num_train_epochs}, Evaluation Loss: {eval_loss}")
|
128 |
+
|
129 |
+
def gradio_interface(input_text):
|
130 |
+
# Define the Gradio interface function
|
131 |
+
tokenized_inputs = {name: tokenizer.encode(input_text, return_tensors="pt") for name, tokenizer in tokenizers.items()}
|
132 |
+
model_output = unified_model(tokenized_inputs)
|
133 |
+
return model_output
|
134 |
+
|
135 |
def main():
|
136 |
while True:
|
137 |
try:
|
|
|
195 |
|
196 |
# Definir los argumentos de entrenamiento
|
197 |
training_args = TrainingArguments(
|
|
|
|
|
|
|
198 |
per_device_train_batch_size=2,
|
199 |
per_device_eval_batch_size=16,
|
200 |
+
num_train_epochs=1,
|
201 |
+
logging_steps=10,
|
202 |
+
save_steps=10,
|
203 |
+
evaluation_strategy="steps"
|
204 |
)
|
205 |
|
206 |
# Definir el optimizador
|
207 |
+
optimizer = AdamW(unified_model.parameters(), lr=5e-5)
|
208 |
|
209 |
+
# Listas para almacenar las pérdidas
|
210 |
train_losses = []
|
211 |
eval_losses = []
|
212 |
|
213 |
+
# Entrenar el modelo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
train(unified_model, train_loader, eval_loader, training_args)
|
215 |
|
216 |
+
# Visualizar pérdidas
|
217 |
fig, ax = plt.subplots()
|
218 |
ax.set_xlabel("Epochs")
|
219 |
ax.set_ylabel("Loss")
|
|
|
247 |
# Subir el modelo y el tokenizador a Hugging Face
|
248 |
push_to_hub(local_dir, repo_name="Ffftdtd5dtft/my_model")
|
249 |
|
250 |
+
# Configurar y lanzar la interfaz Gradio
|
251 |
+
interface = gr.Interface(fn=gradio_interface, inputs="text", outputs="text")
|
252 |
+
interface.launch()
|
253 |
+
|
254 |
break
|
255 |
except Exception as e:
|
256 |
print(f"Error: {e}")
|