Kevin Fink
commited on
Commit
·
a20d3d9
1
Parent(s):
733b7e9
deve
Browse files
app.py
CHANGED
@@ -242,19 +242,20 @@ def fine_tune_model(model, dataset_name, hub_id, api_key, num_epochs, batch_size
|
|
242 |
except Exception as e:
|
243 |
return f"An error occurred: {str(e)}, TB: {traceback.format_exc()}"
|
244 |
return 'DONE!'#train_result
|
245 |
-
|
246 |
# Define Gradio interface
|
247 |
def predict(text):
|
248 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(
|
249 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
250 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
251 |
outputs = model(inputs)
|
252 |
predictions = outputs.logits.argmax(dim=-1)
|
253 |
return predictions.item()
|
254 |
-
|
255 |
|
256 |
@spaces.GPU(duration=120)
|
257 |
-
def run_train(dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad):
|
|
|
258 |
def initialize_weights(model):
|
259 |
for name, param in model.named_parameters():
|
260 |
if 'encoder.block.0.layer.0.DenseReluDense.wi.weight' in name: # Example layer
|
@@ -262,7 +263,7 @@ def run_train(dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad):
|
|
262 |
elif 'encoder.block.0.layer.0.DenseReluDense.wo.weight' in name: # Another example layer
|
263 |
torch.nn.init.kaiming_normal_(param.data) # Kaiming initialization
|
264 |
|
265 |
-
config = AutoConfig.from_pretrained("
|
266 |
model = AutoModelForSeq2SeqLM.from_config(config)
|
267 |
initialize_weights(model)
|
268 |
lora_config = LoraConfig(
|
@@ -272,10 +273,12 @@ def run_train(dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad):
|
|
272 |
bias="none" # Bias handling
|
273 |
)
|
274 |
#model = get_peft_model(model, lora_config)
|
275 |
-
result = fine_tune_model(model, dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad)
|
|
|
276 |
return result
|
277 |
# Create Gradio interface
|
278 |
-
try:
|
|
|
279 |
iface = gr.Interface(
|
280 |
fn=run_train,
|
281 |
inputs=[
|
@@ -293,7 +296,7 @@ try:
|
|
293 |
)
|
294 |
'''
|
295 |
iface = gr.Interface(
|
296 |
-
fn=
|
297 |
inputs=[
|
298 |
gr.Textbox(label="Query"),
|
299 |
],
|
@@ -301,7 +304,7 @@ try:
|
|
301 |
title="Fine-Tune Hugging Face Model",
|
302 |
description="This interface allows you to test a fine-tune Hugging Face model."
|
303 |
)
|
304 |
-
|
305 |
# Launch the interface
|
306 |
iface.launch()
|
307 |
except Exception as e:
|
|
|
242 |
except Exception as e:
|
243 |
return f"An error occurred: {str(e)}, TB: {traceback.format_exc()}"
|
244 |
return 'DONE!'#train_result
|
245 |
+
|
246 |
# Define Gradio interface
|
247 |
def predict(text):
|
248 |
+
model = AutoModelForSeq2SeqLM.from_pretrained('shorecode/t5-efficient-tiny-nh8-summarizer', num_labels=2)
|
249 |
+
tokenizer = AutoTokenizer.from_pretrained('shorecode/t5-efficient-tiny-nh8-summarizer')
|
250 |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
251 |
outputs = model(inputs)
|
252 |
predictions = outputs.logits.argmax(dim=-1)
|
253 |
return predictions.item()
|
254 |
+
|
255 |
|
256 |
@spaces.GPU(duration=120)
|
257 |
+
#def run_train(dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad):
|
258 |
+
def run_train(text):
|
259 |
def initialize_weights(model):
|
260 |
for name, param in model.named_parameters():
|
261 |
if 'encoder.block.0.layer.0.DenseReluDense.wi.weight' in name: # Example layer
|
|
|
263 |
elif 'encoder.block.0.layer.0.DenseReluDense.wo.weight' in name: # Another example layer
|
264 |
torch.nn.init.kaiming_normal_(param.data) # Kaiming initialization
|
265 |
|
266 |
+
config = AutoConfig.from_pretrained("shorecode/t5-efficient-tiny-nh8-summarizer")
|
267 |
model = AutoModelForSeq2SeqLM.from_config(config)
|
268 |
initialize_weights(model)
|
269 |
lora_config = LoraConfig(
|
|
|
273 |
bias="none" # Bias handling
|
274 |
)
|
275 |
#model = get_peft_model(model, lora_config)
|
276 |
+
#result = fine_tune_model(model, dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad)
|
277 |
+
result = predict(text)
|
278 |
return result
|
279 |
# Create Gradio interface
|
280 |
+
try:
|
281 |
+
'''
|
282 |
iface = gr.Interface(
|
283 |
fn=run_train,
|
284 |
inputs=[
|
|
|
296 |
)
|
297 |
'''
|
298 |
iface = gr.Interface(
|
299 |
+
fn=run_train,
|
300 |
inputs=[
|
301 |
gr.Textbox(label="Query"),
|
302 |
],
|
|
|
304 |
title="Fine-Tune Hugging Face Model",
|
305 |
description="This interface allows you to test a fine-tune Hugging Face model."
|
306 |
)
|
307 |
+
|
308 |
# Launch the interface
|
309 |
iface.launch()
|
310 |
except Exception as e:
|