Futuresony commited on
Commit
927446b
·
verified ·
1 Parent(s): 97f10b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -10
app.py CHANGED
@@ -7,19 +7,32 @@ from peft import PeftModel # For loading adapter files
7
  BASE_MODEL_PATH = "unsloth/Llama-3.2-3B-Instruct" # Replace with your base model path
8
  ADAPTER_PATH = "Futuresony/future_ai_12_10_2024.gguf/adapter" # Your Hugging Face repo
9
 
 
 
 
 
 
 
 
10
  # Load base model and tokenizer
11
  print("Loading base model and tokenizer...")
12
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_PATH)
13
- model = AutoModelForCausalLM.from_pretrained(BASE_MODEL_PATH, torch_dtype=torch.float16, device_map="auto")
14
 
15
- # Load adapter files using PEFT
 
 
 
 
 
 
 
16
  print("Loading adapter...")
17
  model = PeftModel.from_pretrained(model, ADAPTER_PATH)
18
 
19
  # Set model to evaluation mode
20
  model.eval()
21
 
22
- # Generate responses using the model
23
  def respond(
24
  message,
25
  history: list[tuple[str, str]],
@@ -28,7 +41,6 @@ def respond(
28
  temperature,
29
  top_p,
30
  ):
31
- # Format chat messages
32
  messages = [{"role": "system", "content": system_message}]
33
  for val in history:
34
  if val[0]:
@@ -38,10 +50,8 @@ def respond(
38
 
39
  messages.append({"role": "user", "content": message})
40
 
41
- # Concatenate messages as input text
42
  input_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
43
-
44
- # Tokenize input text
45
  inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
46
 
47
  # Generate response
@@ -51,12 +61,10 @@ def respond(
51
  top_p=top_p,
52
  do_sample=True,
53
  )
54
-
55
  output_ids = model.generate(**inputs, generation_config=generation_config)
56
  response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
57
 
58
- return response.split("assistant:")[-1].strip() # Extract assistant response
59
-
60
 
61
  # Gradio Interface
62
  demo = gr.ChatInterface(
@@ -71,3 +79,4 @@ demo = gr.ChatInterface(
71
 
72
  if __name__ == "__main__":
73
  demo.launch()
 
 
7
  BASE_MODEL_PATH = "unsloth/Llama-3.2-3B-Instruct" # Replace with your base model path
8
  ADAPTER_PATH = "Futuresony/future_ai_12_10_2024.gguf/adapter" # Your Hugging Face repo
9
 
10
+ # Function to clean rope_scaling in model config
11
+ def clean_rope_scaling(config):
12
+ if "rope_scaling" in config:
13
+ valid_rope_scaling = {"type": "linear", "factor": config["rope_scaling"].get("factor", 1.0)}
14
+ config["rope_scaling"] = valid_rope_scaling
15
+ return config
16
+
17
  # Load base model and tokenizer
18
  print("Loading base model and tokenizer...")
19
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_PATH)
 
20
 
21
+ # Load and clean the model config
22
+ config = LlamaConfig.from_pretrained(BASE_MODEL_PATH)
23
+ clean_config = clean_rope_scaling(config.to_dict())
24
+
25
+ # Load model with cleaned config
26
+ model = AutoModelForCausalLM.from_pretrained(BASE_MODEL_PATH, config=clean_config, torch_dtype=torch.float16, device_map="auto")
27
+
28
+ # Load adapter using PEFT
29
  print("Loading adapter...")
30
  model = PeftModel.from_pretrained(model, ADAPTER_PATH)
31
 
32
  # Set model to evaluation mode
33
  model.eval()
34
 
35
+ # Function to generate responses
36
  def respond(
37
  message,
38
  history: list[tuple[str, str]],
 
41
  temperature,
42
  top_p,
43
  ):
 
44
  messages = [{"role": "system", "content": system_message}]
45
  for val in history:
46
  if val[0]:
 
50
 
51
  messages.append({"role": "user", "content": message})
52
 
53
+ # Prepare input
54
  input_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
 
 
55
  inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
56
 
57
  # Generate response
 
61
  top_p=top_p,
62
  do_sample=True,
63
  )
 
64
  output_ids = model.generate(**inputs, generation_config=generation_config)
65
  response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
66
 
67
+ return response.split("assistant:")[-1].strip()
 
68
 
69
  # Gradio Interface
70
  demo = gr.ChatInterface(
 
79
 
80
  if __name__ == "__main__":
81
  demo.launch()
82
+