Spaces:
Sleeping
Sleeping
Commit
·
aa9f0d3
1
Parent(s):
a44b2db
ok
Browse files- generative_inference.py +113 -16
- inference.py +12 -2
generative_inference.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import torch
|
2 |
import json
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
4 |
from peft import PeftModel
|
5 |
import openai
|
6 |
|
@@ -36,8 +37,14 @@ MODEL_OPTIONS = {
|
|
36 |
"model_id": "ft:gpt-3.5-turbo-0125:asma:gpt-3-5-turbo-absa:Bb6gmwkE"},
|
37 |
"GPT4o": {"base": "openai/gpt-4o",
|
38 |
"model_id": "ft:gpt-4o-mini-2024-07-18:asma:gpt4-finetune-absa:BazoEjnp"},
|
39 |
-
"
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
}
|
42 |
|
43 |
|
@@ -111,26 +118,94 @@ def infer_gpt_absa(text, model_key):
|
|
111 |
|
112 |
|
113 |
|
114 |
-
|
|
|
115 |
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
base_model = AutoModelForCausalLM.from_pretrained(
|
118 |
-
MODEL_OPTIONS["
|
119 |
device_map="auto",
|
120 |
torch_dtype=torch.float16,
|
121 |
trust_remote_code=True
|
122 |
)
|
123 |
tokenizer = AutoTokenizer.from_pretrained(
|
124 |
-
MODEL_OPTIONS["
|
125 |
trust_remote_code=True
|
126 |
)
|
127 |
-
model = PeftModel.from_pretrained(base_model, MODEL_OPTIONS["
|
128 |
|
129 |
-
cached_models["
|
130 |
return tokenizer, model
|
131 |
|
132 |
|
133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
def build_deepseek_prompt(review_text, output=""):
|
135 |
return f"""<|system|>
|
136 |
You are an advanced AI model specialized in extracting aspects and determining their sentiment polarity from customer reviews.
|
@@ -152,15 +227,30 @@ Instructions:
|
|
152 |
{output}""" # ✅ include the output here
|
153 |
|
154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
-
def infer_deepseek(
|
157 |
-
|
|
|
|
|
|
|
158 |
|
159 |
-
prompt = build_deepseek_prompt(
|
160 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to(model.device)
|
161 |
|
162 |
with torch.no_grad():
|
163 |
-
|
164 |
**inputs,
|
165 |
max_new_tokens=128,
|
166 |
do_sample=False,
|
@@ -168,9 +258,16 @@ def infer_deepseek(text):
|
|
168 |
pad_token_id=tokenizer.eos_token_id
|
169 |
)
|
170 |
|
171 |
-
decoded = tokenizer.decode(
|
|
|
|
|
|
|
|
|
172 |
try:
|
173 |
-
|
174 |
-
return parsed
|
175 |
except Exception as e:
|
176 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import torch
|
2 |
import json
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
from peft import PeftModel
|
6 |
import openai
|
7 |
|
|
|
37 |
"model_id": "ft:gpt-3.5-turbo-0125:asma:gpt-3-5-turbo-absa:Bb6gmwkE"},
|
38 |
"GPT4o": {"base": "openai/gpt-4o",
|
39 |
"model_id": "ft:gpt-4o-mini-2024-07-18:asma:gpt4-finetune-absa:BazoEjnp"},
|
40 |
+
"ALLaM": {
|
41 |
+
"base": "ALLaM-AI/ALLaM-7B-Instruct-preview",
|
42 |
+
"adapter": "asmashayea/allam-absa"
|
43 |
+
},
|
44 |
+
"DeepSeek": {
|
45 |
+
"base": "deepseek-ai/deepseek-llm-7b-chat",
|
46 |
+
"adapter": "asmashayea/deepseek-absa"
|
47 |
+
}
|
48 |
}
|
49 |
|
50 |
|
|
|
118 |
|
119 |
|
120 |
|
121 |
+
def infer_allam(review_text):
|
122 |
+
tokenizer, model = cached_models.get("ALLaM") or load_allam()
|
123 |
|
124 |
+
prompt = tokenizer.apply_chat_template(
|
125 |
+
[
|
126 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
127 |
+
{"role": "user", "content": review_text}
|
128 |
+
],
|
129 |
+
tokenize=False
|
130 |
+
)
|
131 |
+
|
132 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to(model.device)
|
133 |
+
|
134 |
+
with torch.no_grad():
|
135 |
+
outputs = model.generate(
|
136 |
+
**inputs,
|
137 |
+
max_new_tokens=128,
|
138 |
+
do_sample=False,
|
139 |
+
temperature=0.0,
|
140 |
+
pad_token_id=tokenizer.eos_token_id
|
141 |
+
)
|
142 |
+
|
143 |
+
decoded = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
|
144 |
+
try:
|
145 |
+
parsed = json.loads(decoded)
|
146 |
+
return parsed
|
147 |
+
except Exception as e:
|
148 |
+
return {"error": str(e), "raw": decoded}
|
149 |
+
|
150 |
+
|
151 |
+
def load_allam():
|
152 |
base_model = AutoModelForCausalLM.from_pretrained(
|
153 |
+
MODEL_OPTIONS["ALLaM"]["base"],
|
154 |
device_map="auto",
|
155 |
torch_dtype=torch.float16,
|
156 |
trust_remote_code=True
|
157 |
)
|
158 |
tokenizer = AutoTokenizer.from_pretrained(
|
159 |
+
MODEL_OPTIONS["ALLaM"]["adapter"],
|
160 |
trust_remote_code=True
|
161 |
)
|
162 |
+
model = PeftModel.from_pretrained(base_model, MODEL_OPTIONS["ALLaM"]["adapter"])
|
163 |
|
164 |
+
cached_models["ALLaM"] = (tokenizer, model)
|
165 |
return tokenizer, model
|
166 |
|
167 |
|
168 |
|
169 |
+
|
170 |
+
|
171 |
+
def load_allam():
|
172 |
+
base = AutoModelForCausalLM.from_pretrained(
|
173 |
+
MODEL_OPTIONS["ALLaM"]["base"],
|
174 |
+
torch_dtype=torch.float16,
|
175 |
+
trust_remote_code=True
|
176 |
+
)
|
177 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
178 |
+
MODEL_OPTIONS["ALLaM"]["adapter"], trust_remote_code=True
|
179 |
+
)
|
180 |
+
model = PeftModel.from_pretrained(base, MODEL_OPTIONS["ALLaM"]["adapter"])
|
181 |
+
cached_models["ALLaM"] = (tokenizer, model)
|
182 |
+
return tokenizer, model
|
183 |
+
|
184 |
+
def infer_allam(review):
|
185 |
+
if "ALLaM" not in cached_models:
|
186 |
+
tokenizer, model = load_allam()
|
187 |
+
else:
|
188 |
+
tokenizer, model = cached_models["ALLaM"]
|
189 |
+
|
190 |
+
prompt = tokenizer.apply_chat_template([
|
191 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
192 |
+
{"role": "user", "content": review}
|
193 |
+
], tokenize=False)
|
194 |
+
|
195 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
196 |
+
|
197 |
+
with torch.no_grad():
|
198 |
+
output = model.generate(**inputs, max_new_tokens=256)
|
199 |
+
decoded = tokenizer.decode(output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
200 |
+
|
201 |
+
try:
|
202 |
+
return json.loads(decoded)
|
203 |
+
except:
|
204 |
+
return decoded
|
205 |
+
|
206 |
+
|
207 |
+
|
208 |
+
|
209 |
def build_deepseek_prompt(review_text, output=""):
|
210 |
return f"""<|system|>
|
211 |
You are an advanced AI model specialized in extracting aspects and determining their sentiment polarity from customer reviews.
|
|
|
227 |
{output}""" # ✅ include the output here
|
228 |
|
229 |
|
230 |
+
def load_deepseek():
|
231 |
+
base = AutoModelForCausalLM.from_pretrained(
|
232 |
+
MODEL_OPTIONS["DeepSeek"]["base"],
|
233 |
+
torch_dtype=torch.float16,
|
234 |
+
trust_remote_code=True
|
235 |
+
)
|
236 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
237 |
+
MODEL_OPTIONS["DeepSeek"]["adapter"], trust_remote_code=True
|
238 |
+
)
|
239 |
+
model = PeftModel.from_pretrained(base, MODEL_OPTIONS["DeepSeek"]["adapter"])
|
240 |
+
cached_models["DeepSeek"] = (tokenizer, model)
|
241 |
+
return tokenizer, model
|
242 |
|
243 |
+
def infer_deepseek(review):
|
244 |
+
if "DeepSeek" not in cached_models:
|
245 |
+
tokenizer, model = load_deepseek()
|
246 |
+
else:
|
247 |
+
tokenizer, model = cached_models["DeepSeek"]
|
248 |
|
249 |
+
prompt = build_deepseek_prompt(review)
|
250 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to(model.device)
|
251 |
|
252 |
with torch.no_grad():
|
253 |
+
output = model.generate(
|
254 |
**inputs,
|
255 |
max_new_tokens=128,
|
256 |
do_sample=False,
|
|
|
258 |
pad_token_id=tokenizer.eos_token_id
|
259 |
)
|
260 |
|
261 |
+
decoded = tokenizer.decode(
|
262 |
+
output[0][inputs["input_ids"].shape[1]:],
|
263 |
+
skip_special_tokens=True
|
264 |
+
).strip()
|
265 |
+
|
266 |
try:
|
267 |
+
return json.loads(decoded)
|
|
|
268 |
except Exception as e:
|
269 |
+
print(f"❌ DeepSeek JSON parse error: {e}")
|
270 |
+
return decoded
|
271 |
+
|
272 |
+
|
273 |
+
|
inference.py
CHANGED
@@ -3,7 +3,7 @@ import json
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModel, AutoConfig
|
4 |
from peft import LoraConfig, get_peft_model, PeftModel
|
5 |
from modeling_bilstm_crf import BERT_BiLSTM_CRF
|
6 |
-
from generative_inference import infer_t5_bart, infer_gpt_absa, infer_deepseek
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
|
9 |
# Define supported models and their adapter IDs
|
@@ -24,7 +24,14 @@ MODEL_OPTIONS = {
|
|
24 |
"model_id": "ft:gpt-3.5-turbo-0125:asma:gpt-3-5-turbo-absa:Bb6gmwkE"},
|
25 |
"GPT4o": {"base": "openai/gpt-4o",
|
26 |
"model_id": "ft:gpt-4o-mini-2024-07-18:asma:gpt4-finetune-absa:BazoEjnp"},
|
27 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
}
|
29 |
|
30 |
|
@@ -142,5 +149,8 @@ def predict_absa(text, model_choice):
|
|
142 |
|
143 |
elif model_choice == "DeepSeek":
|
144 |
return infer_deepseek(text)
|
|
|
|
|
|
|
145 |
|
146 |
return decoded
|
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModel, AutoConfig
|
4 |
from peft import LoraConfig, get_peft_model, PeftModel
|
5 |
from modeling_bilstm_crf import BERT_BiLSTM_CRF
|
6 |
+
from generative_inference import infer_t5_bart, infer_gpt_absa, infer_deepseek, infer_allam
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
|
9 |
# Define supported models and their adapter IDs
|
|
|
24 |
"model_id": "ft:gpt-3.5-turbo-0125:asma:gpt-3-5-turbo-absa:Bb6gmwkE"},
|
25 |
"GPT4o": {"base": "openai/gpt-4o",
|
26 |
"model_id": "ft:gpt-4o-mini-2024-07-18:asma:gpt4-finetune-absa:BazoEjnp"},
|
27 |
+
"ALLaM": {
|
28 |
+
"base": "ALLaM-AI/ALLaM-7B-Instruct-preview",
|
29 |
+
"adapter": "asmashayea/allam-absa"
|
30 |
+
},
|
31 |
+
"DeepSeek": {
|
32 |
+
"base": "deepseek-ai/deepseek-llm-7b-chat",
|
33 |
+
"adapter": "asmashayea/deepseek-absa"
|
34 |
+
}
|
35 |
}
|
36 |
|
37 |
|
|
|
149 |
|
150 |
elif model_choice == "DeepSeek":
|
151 |
return infer_deepseek(text)
|
152 |
+
|
153 |
+
elif model_choice == "ALLaM":
|
154 |
+
return infer_allam(text)
|
155 |
|
156 |
return decoded
|