File size: 6,478 Bytes
71e55e7 eac2c48 014e235 71e55e7 cd24c9d 71e55e7 014e235 eac2c48 014e235 71e55e7 021c2c9 afa7e4f 021c2c9 afa7e4f 2ddfac0 71e55e7 2ddfac0 021c2c9 afa7e4f 021c2c9 afa7e4f 021c2c9 afa7e4f 021c2c9 afa7e4f 021c2c9 afa7e4f 021c2c9 afa7e4f 021c2c9 afa7e4f 021c2c9 afa7e4f 021c2c9 afa7e4f 021c2c9 afa7e4f 021c2c9 afa7e4f 2ddfac0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
from transformers import pipeline
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from peft import PeftModel, PeftConfig
import gradio as gr
import torch
base_model = "unsloth/Llama-3.2-3B-Instruct" # Replace with the correct base model
peft_model_path = "ivwhy/lora_model"
config = PeftConfig.from_pretrained(peft_model_path)
model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype=torch.bfloat16)
model = PeftModel.from_pretrained(model, peft_model_path)
tokenizer = AutoTokenizer.from_pretrained(base_model)
pipeline = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
device=-1, # CPU
)
chatbot = pipeline
message_list = []
response_list = []
def chat_function(message, history, system_prompt, max_new_tokens, temperature):
messages = [{"role":"system","content":system_prompt},
{"role":"user","content":message}]
prompt = pipeline.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,)
terminators = [
pipeline.tokenizer.eos_token_id,
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")]
outputs = pipeline(
prompt,
max_new_tokens = max_new_tokens,
eos_token_id = terminators,
do_sample = True,
temperature = 0.1,
top_p = 0.9,)
return outputs[0]["generated_text"][len(prompt):]
demo_chatbot = gr.ChatInterface(
chat_function,
textbox=gr.Textbox(placeholder="Enter message here", container=False, scale=7),
chatbot=gr.Chatbot(height=400),
additional_inputs=[
gr.Textbox("You are helpful AI", label="System Prompt"),
gr.Slider(500,4000, label="Max New Tokens"),
gr.Slider(0,1,label="Temperature")
])
demo_chatbot.launch()
''' =================================== OLD VERSION ==============================================
import torch
import transformers
import gradio as gr
from unsloth import FastLanguageModel
# Load the fine-tuned Unsloth model
max_seq_length = 2048 # Adjust based on your training
dtype = None # Auto-detect is fine for CPU
def load_model():
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="ivwhy/lora_model", # Your fine-tuned model path
max_seq_length=max_seq_length,
dtype=dtype,
load_in_4bit=True, # Keep 4-bit loading enabled
)
# Optional: Add special tokens for chat if needed
tokenizer.pad_token = tokenizer.eos_token
# Create the pipeline for CPU
pipeline = transformers.pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
device=-1 # Force CPU usage
)
return pipeline, tokenizer
# Load model globally
generation_pipeline, tokenizer = load_model()
def chat_function(message, history, system_prompt, max_new_tokens, temperature):
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": message}
]
# Apply chat template
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
# Define terminators
terminators = [
tokenizer.eos_token_id,
tokenizer.convert_tokens_to_ids("<|eot_id|>")
]
# Generate response
outputs = generation_pipeline(
prompt,
max_new_tokens=max_new_tokens,
eos_token_id=terminators,
do_sample=True,
temperature=temperature,
top_p=0.9,
)
# Extract and return just the generated text
return outputs[0]["generated_text"][len(prompt):]
# Create Gradio interface
demo = gr.ChatInterface(
chat_function,
textbox=gr.Textbox(placeholder="Enter message here", container=False, scale=7),
chatbot=gr.Chatbot(height=400),
additional_inputs=[
gr.Textbox("You are helpful AI", label="System Prompt"),
gr.Slider(minimum=1, maximum=4000, value=500, label="Max New Tokens"),
gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature")
]
)
if __name__ == "__main__":
demo.launch()
================================== OLD VER ==============================
import torch
import transformers
import gradio as gr
from unsloth import FastLanguageModel
# Load the fine-tuned Unsloth model
max_seq_length = 2048 # Adjust based on your training
dtype = None # None for auto detection
def load_model():
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="ivwhy/lora_model", # Your fine-tuned model path
max_seq_length=max_seq_length,
dtype=dtype,
load_in_4bit=True # Optional: load in 4-bit for efficiency
)
# Optional: Add special tokens for chat if needed
tokenizer.pad_token = tokenizer.eos_token
# Create the pipeline
pipeline = transformers.pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
device=0 if torch.cuda.is_available() else -1 # Use GPU if available
)
return pipeline, tokenizer
# Load model globally
generation_pipeline, tokenizer = load_model()
def chat_function(message, history, system_prompt, max_new_tokens, temperature):
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": message}
]
# Apply chat template
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
# Define terminators
terminators = [
tokenizer.eos_token_id,
tokenizer.convert_tokens_to_ids("<|eot_id|>")
]
# Generate response
outputs = generation_pipeline(
prompt,
max_new_tokens=max_new_tokens,
eos_token_id=terminators,
do_sample=True,
temperature=temperature,
top_p=0.9,
)
# Extract and return just the generated text
return outputs[0]["generated_text"][len(prompt):]
# Create Gradio interface
demo = gr.ChatInterface(
chat_function,
textbox=gr.Textbox(placeholder="Enter message here", container=False, scale=7),
chatbot=gr.Chatbot(height=400),
additional_inputs=[
gr.Textbox("You are helpful AI", label="System Prompt"),
gr.Slider(minimum=1, maximum=4000, value=500, label="Max New Tokens"),
gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature")
]
)
if __name__ == "__main__":
demo.launch()
''' |