Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,8 @@ import gradio as gr
|
|
2 |
import os, torch
|
3 |
from datasets import load_dataset
|
4 |
from huggingface_hub import HfApi, login
|
5 |
-
from peft import LoraConfig
|
|
|
6 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, TrainingArguments, pipeline
|
7 |
from trl import SFTTrainer, setup_chat_format
|
8 |
|
@@ -21,20 +22,46 @@ base_model_id = "codellama/CodeLlama-7b-hf" # "ibm-granite/granite-8b-code-instr
|
|
21 |
dataset = "b-mc2/sql-create-context"
|
22 |
|
23 |
def prompt_model(model_id, system_prompt, user_prompt, schema):
|
24 |
-
pipe = pipeline("text-generation",
|
25 |
-
model=model_id,
|
26 |
-
model_kwargs={"torch_dtype": torch.bfloat16},
|
27 |
-
device_map="auto",
|
28 |
-
max_new_tokens=1000)
|
29 |
-
messages = [
|
30 |
-
{"role": "system", "content": system_prompt.format(schema=schema)},
|
31 |
-
{"role": "user", "content": user_prompt},
|
32 |
-
{"role": "assistant", "content": ""}
|
33 |
-
]
|
34 |
-
output = pipe(messages)
|
35 |
-
result = output[0]["generated_text"][-1]["content"]
|
36 |
-
print(result)
|
37 |
-
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
def fine_tune_model(base_model_id, dataset):
|
40 |
#tokenizer = download_model(base_model_id)
|
|
|
2 |
import os, torch
|
3 |
from datasets import load_dataset
|
4 |
from huggingface_hub import HfApi, login
|
5 |
+
from peft import AutoPeftModelForCausalLM, LoraConfig
|
6 |
+
from random import randint
|
7 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, TrainingArguments, pipeline
|
8 |
from trl import SFTTrainer, setup_chat_format
|
9 |
|
|
|
22 |
dataset = "b-mc2/sql-create-context"
|
23 |
|
24 |
def prompt_model(model_id, system_prompt, user_prompt, schema):
|
25 |
+
# pipe = pipeline("text-generation",
|
26 |
+
# model=model_id,
|
27 |
+
# model_kwargs={"torch_dtype": torch.bfloat16},
|
28 |
+
# device_map="auto",
|
29 |
+
# max_new_tokens=1000)
|
30 |
+
# messages = [
|
31 |
+
# {"role": "system", "content": system_prompt.format(schema=schema)},
|
32 |
+
# {"role": "user", "content": user_prompt},
|
33 |
+
# {"role": "assistant", "content": ""}
|
34 |
+
# ]
|
35 |
+
# output = pipe(messages)
|
36 |
+
# result = output[0]["generated_text"][-1]["content"]
|
37 |
+
# print(result)
|
38 |
+
# return result
|
39 |
+
|
40 |
+
peft_model_id = "./code-llama-7b-text-to-sql"
|
41 |
+
# peft_model_id = args.output_dir
|
42 |
+
|
43 |
+
# Load Model with PEFT adapter
|
44 |
+
model = AutoPeftModelForCausalLM.from_pretrained(
|
45 |
+
peft_model_id,
|
46 |
+
device_map="auto",
|
47 |
+
torch_dtype=torch.float16
|
48 |
+
)
|
49 |
+
tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
|
50 |
+
# load into pipeline
|
51 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
52 |
+
|
53 |
+
###
|
54 |
+
|
55 |
+
eval_dataset = load_dataset("json", data_files="test_dataset.json", split="train")
|
56 |
+
rand_idx = randint(0, len(eval_dataset))
|
57 |
+
|
58 |
+
# Test on sample
|
59 |
+
prompt = pipe.tokenizer.apply_chat_template(eval_dataset[rand_idx]["messages"][:2], tokenize=False, add_generation_prompt=True)
|
60 |
+
outputs = pipe(prompt, max_new_tokens=256, do_sample=False, temperature=0.1, top_k=50, top_p=0.1, eos_token_id=pipe.tokenizer.eos_token_id, pad_token_id=pipe.tokenizer.pad_token_id)
|
61 |
+
|
62 |
+
print(f"Query:\n{eval_dataset[rand_idx]['messages'][1]['content']}")
|
63 |
+
print(f"Original Answer:\n{eval_dataset[rand_idx]['messages'][2]['content']}")
|
64 |
+
print(f"Generated Answer:\n{outputs[0]['generated_text'][len(prompt):].strip()}")
|
65 |
|
66 |
def fine_tune_model(base_model_id, dataset):
|
67 |
#tokenizer = download_model(base_model_id)
|