Kevin Fink commited on
Commit
ac28cc5
·
1 Parent(s): 599c53e
Files changed (2) hide show
  1. app.py +19 -3
  2. requirements.txt +1 -0
app.py CHANGED
@@ -6,6 +6,7 @@ from datasets import load_dataset, concatenate_datasets, load_from_disk
6
  import traceback
7
  from sklearn.metrics import accuracy_score
8
  import numpy as np
 
9
 
10
  import os
11
  from huggingface_hub import login
@@ -16,6 +17,7 @@ os.environ['HF_HOME'] = '/data/.huggingface'
16
  @spaces.GPU(duration=120)
17
  def fine_tune_model(model_name, dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad):
18
  try:
 
19
  def compute_metrics(eval_pred):
20
  logits, labels = eval_pred
21
  predictions = np.argmax(logits, axis=1)
@@ -34,6 +36,7 @@ def fine_tune_model(model_name, dataset_name, hub_id, api_key, num_epochs, batch
34
 
35
  # Load the model and tokenizer
36
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name.strip(), num_labels=2)
 
37
  #model = get_peft_model(model, lora_config)
38
 
39
 
@@ -91,7 +94,7 @@ def fine_tune_model(model_name, dataset_name, hub_id, api_key, num_epochs, batch
91
  model_inputs = tokenizer(
92
  examples['text'],
93
  max_length=max_length, # Set to None for dynamic padding
94
- padding='max_length', # Disable padding here, we will handle it later
95
  truncation=True,
96
  )
97
 
@@ -99,7 +102,7 @@ def fine_tune_model(model_name, dataset_name, hub_id, api_key, num_epochs, batch
99
  labels = tokenizer(
100
  examples['target'],
101
  max_length=max_length, # Set to None for dynamic padding
102
- padding='max_length', # Disable padding here, we will handle it later
103
  truncation=True,
104
  text_target=examples['target'] # Use text_target for target text
105
  )
@@ -132,10 +135,12 @@ def fine_tune_model(model_name, dataset_name, hub_id, api_key, num_epochs, batch
132
  '''
133
  # Define Gradio interface
134
  def predict(text):
 
 
135
  inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
136
  outputs = model(inputs)
137
  predictions = outputs.logits.argmax(dim=-1)
138
- return "Positive" if predictions.item() == 1 else "Negative"
139
  '''
140
  # Create Gradio interface
141
  try:
@@ -156,6 +161,17 @@ try:
156
  title="Fine-Tune Hugging Face Model",
157
  description="This interface allows you to fine-tune a Hugging Face model on a specified dataset."
158
  )
 
 
 
 
 
 
 
 
 
 
 
159
  # Launch the interface
160
  iface.launch()
161
  except Exception as e:
 
6
  import traceback
7
  from sklearn.metrics import accuracy_score
8
  import numpy as np
9
+ import torch
10
 
11
  import os
12
  from huggingface_hub import login
 
17
  @spaces.GPU(duration=120)
18
  def fine_tune_model(model_name, dataset_name, hub_id, api_key, num_epochs, batch_size, lr, grad):
19
  try:
20
+ torch.cuda.empty_cache()
21
  def compute_metrics(eval_pred):
22
  logits, labels = eval_pred
23
  predictions = np.argmax(logits, axis=1)
 
36
 
37
  # Load the model and tokenizer
38
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name.strip(), num_labels=2)
39
+ model.gradient_checkpointing_enable()
40
  #model = get_peft_model(model, lora_config)
41
 
42
 
 
94
  model_inputs = tokenizer(
95
  examples['text'],
96
  max_length=max_length, # Set to None for dynamic padding
97
+ padding=True, # Disable padding here, we will handle it later
98
  truncation=True,
99
  )
100
 
 
102
  labels = tokenizer(
103
  examples['target'],
104
  max_length=max_length, # Set to None for dynamic padding
105
+ padding=True, # Disable padding here, we will handle it later
106
  truncation=True,
107
  text_target=examples['target'] # Use text_target for target text
108
  )
 
135
  '''
136
  # Define Gradio interface
137
  def predict(text):
138
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name.strip(), num_labels=2)
139
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
140
  inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
141
  outputs = model(inputs)
142
  predictions = outputs.logits.argmax(dim=-1)
143
+ return predictions.item()
144
  '''
145
  # Create Gradio interface
146
  try:
 
161
  title="Fine-Tune Hugging Face Model",
162
  description="This interface allows you to fine-tune a Hugging Face model on a specified dataset."
163
  )
164
+ '''
165
+ iface = gr.Interface(
166
+ fn=predict,
167
+ inputs=[
168
+ gr.Textbox(label="Query"),
169
+ ],
170
+ outputs="text",
171
+ title="Fine-Tune Hugging Face Model",
172
+ description="This interface allows you to test a fine-tune Hugging Face model."
173
+ )
174
+ '''
175
  # Launch the interface
176
  iface.launch()
177
  except Exception as e:
requirements.txt CHANGED
@@ -5,3 +5,4 @@ peft
5
  huggingface_hub
6
  scikit-learn
7
  numpy
 
 
5
  huggingface_hub
6
  scikit-learn
7
  numpy
8
+ import torch