hallisky commited on
Commit
63b741b
·
1 Parent(s): c9649d5

Fix sliders, still has A100 issue with multiple PEFT

Browse files
Files changed (1) hide show
  1. app.py +31 -14
app.py CHANGED
@@ -3,7 +3,6 @@ import gradio as gr
3
  import torch
4
  from peft import PeftModel, PeftConfig
5
  from transformers import AutoModelForCausalLM, AutoTokenizer
6
- from IPython import embed
7
 
8
  import json
9
  from datetime import datetime
@@ -25,13 +24,19 @@ def convert_data_to_format(text):
25
 
26
  MODEL_PATHS = {
27
  "length_more": "hallisky/lora-length-long-llama-3-8b",
28
- "length_less": "hallisky/lora-length-long-llama-3-8b",
29
  "function_more": "hallisky/lora-function-more-llama-3-8b",
30
  "function_less": "hallisky/lora-function-less-llama-3-8b",
31
  "grade_more": "hallisky/lora-grade-highschool-llama-3-8b",
32
  "grade_less": "hallisky/lora-grade-elementary-llama-3-8b",
 
 
 
 
 
 
33
  }
34
- FIRST_MODEL = list(MODEL_PATHS.keys())[0]
35
 
36
  DESCRIPTION = """\
37
  # Authorship Obfuscation
@@ -57,9 +62,9 @@ if torch.cuda.is_available():
57
  # Load in the first model
58
  model = PeftModel.from_pretrained(base_model, MODEL_PATHS[FIRST_MODEL], adapter_name=FIRST_MODEL).to(device)
59
  # Load in the rest of the models
60
- for cur_adapter in MODEL_PATHS.keys():
61
- if cur_adapter != FIRST_MODEL:
62
- model.load_adapter(MODEL_PATHS[cur_adapter], adapter_name=cur_adapter)
63
 
64
  model.eval()
65
 
@@ -96,6 +101,19 @@ def greet(input_text, length, function_words, grade_level, sarcasm, formality, v
96
  global latest_obfuscation, user_id
97
  current_time = datetime.now().isoformat()
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  response = (
100
  f"Hello!\n"
101
  f"Input Text: {input_text}\n"
@@ -112,14 +130,13 @@ def greet(input_text, length, function_words, grade_level, sarcasm, formality, v
112
  )
113
 
114
  converted_text = convert_data_to_format(input_text)
115
- response = converted_text
116
- with torch.no_grad():
117
- outputs = model.generate(
118
- input_ids=tokenizer(converted_text, return_tensors="pt").input_ids.to(device),
119
- max_length=100,
120
- num_return_sequences=1,
121
- )
122
- # response = tokenizer.decode(outputs[0], skip_special_tokens=True)
123
 
124
  # Save the new obfuscation result and reset feedback
125
  latest_obfuscation = {
 
3
  import torch
4
  from peft import PeftModel, PeftConfig
5
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
6
 
7
  import json
8
  from datetime import datetime
 
24
 
25
  MODEL_PATHS = {
26
  "length_more": "hallisky/lora-length-long-llama-3-8b",
27
+ "length_less": "hallisky/lora-length-short-llama-3-8b",
28
  "function_more": "hallisky/lora-function-more-llama-3-8b",
29
  "function_less": "hallisky/lora-function-less-llama-3-8b",
30
  "grade_more": "hallisky/lora-grade-highschool-llama-3-8b",
31
  "grade_less": "hallisky/lora-grade-elementary-llama-3-8b",
32
+ "formality_more": "hallisky/lora-formality-formal-llama-3-8b",
33
+ "formality_less": "hallisky/lora-formality-informal-llama-3-8b",
34
+ "sarcasm_more": "hallisky/lora-sarcasm-more-llama-3-8b",
35
+ "sarcasm_less": "hallisky/lora-sarcasm-less-llama-3-8b",
36
+ "voice_passive": "hallisky/lora-voice-passive-llama-3-8b",
37
+ "voice_active": "hallisky/lora-voice-active-llama-3-8b",
38
  }
39
+ FIRST_MODEL = list(MODEL_PATHS.keys())[5]
40
 
41
  DESCRIPTION = """\
42
  # Authorship Obfuscation
 
62
  # Load in the first model
63
  model = PeftModel.from_pretrained(base_model, MODEL_PATHS[FIRST_MODEL], adapter_name=FIRST_MODEL).to(device)
64
  # Load in the rest of the models
65
+ # for cur_adapter in MODEL_PATHS.keys():
66
+ # if cur_adapter != FIRST_MODEL:
67
+ # model.load_adapter(MODEL_PATHS[cur_adapter], adapter_name=cur_adapter)
68
 
69
  model.eval()
70
 
 
101
  global latest_obfuscation, user_id
102
  current_time = datetime.now().isoformat()
103
 
104
+ sliders_dict = {}
105
+ cur_keys = []
106
+ cur_keys.append(("length_more" if length > 0 else (None if length == 0 else "length_less"), length))
107
+ cur_keys.append(("function_more" if function_words > 0 else (None if function_words == 0 else "function_less"), function_words))
108
+ cur_keys.append(("grade_more" if grade_level > 0 else (None if grade_level == 0 else "grade_less"), grade_level))
109
+ cur_keys.append(("sarcasm_more" if sarcasm > 0 else (None if sarcasm == 0 else "sarcasm_less"), sarcasm))
110
+ cur_keys.append(("formality_more" if formality > 0 else (None if formality == 0 else "formality_less"), formality))
111
+ cur_keys.append(("voice_active" if voice > 0 else (None if voice == 0 else "voice_passive"), voice))
112
+
113
+ for cur_key in cur_keys:
114
+ if cur_key[0] is not None:
115
+ sliders_dict[cur_key[0]] = cur_key[1]
116
+
117
  response = (
118
  f"Hello!\n"
119
  f"Input Text: {input_text}\n"
 
130
  )
131
 
132
  converted_text = convert_data_to_format(input_text)
133
+
134
+ # Convert the list of strings in data to a list of model inputs
135
+ inputs = tokenizer(converted_text, return_tensors="pt", max_length=2048, truncation=True).to(device)
136
+ input_length = inputs.input_ids.shape[1]
137
+ with torch.no_grad():
138
+ outputs = model.generate(**inputs, max_length=100, top_p = 0.95)
139
+ response = tokenizer.decode(outputs[0, input_length:], skip_special_tokens=True)
 
140
 
141
  # Save the new obfuscation result and reset feedback
142
  latest_obfuscation = {