Reaperxxxx commited on
Commit
2831f7f
·
verified ·
1 Parent(s): 5d25a25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -17
app.py CHANGED
@@ -5,37 +5,33 @@ import torch
5
  # =========================
6
  # 1️⃣ Load model & tokenizer
7
  # =========================
8
- model_name = "Qwen/Qwen2.5-0.5B-Instruct" # Change to 1.5B, 3B, 7B if needed
9
 
10
- # Use the fast tokenizer for speed
11
  tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
12
 
13
- # Load model
14
  model = AutoModelForCausalLM.from_pretrained(
15
  model_name,
16
- device_map="auto", # Automatically choose GPU if available
17
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
18
- load_in_8bit=False # Change to True if GPU memory is limited
19
  )
20
 
21
- # Optional: PyTorch 2.x compile for speed
22
  if torch.__version__.startswith("2"):
23
  model = torch.compile(model)
24
 
25
  # =========================
26
  # 2️⃣ Hardcoded system prompt
27
  # =========================
28
- SYSTEM_PROMPT = "You are a friendly AI assistant named Magda that gives helpful and polite answers."
29
 
30
  # =========================
31
- # 3️⃣ Chat function
32
  # =========================
33
  def chat(user_prompt: str):
34
- """
35
- user_prompt: string message from user
36
- """
37
  messages = [
38
- {"role": "system", "content": SYSTEM_PROMPT}, # Hardcoded system
39
  {"role": "user", "content": user_prompt}
40
  ]
41
 
@@ -46,18 +42,18 @@ def chat(user_prompt: str):
46
  add_generation_prompt=True
47
  )
48
 
49
- # Encode input
50
  inputs = tokenizer([text], return_tensors="pt").to(model.device)
51
 
52
- # Generate response (fast settings)
53
  outputs = model.generate(
54
  **inputs,
55
  max_new_tokens=128, # smaller = faster
56
  do_sample=False, # deterministic = faster
57
- num_beams=1 # no beam search = faster
58
  )
59
 
60
- # Decode output
61
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
62
  return response
63
 
 
5
  # =========================
6
  # 1️⃣ Load model & tokenizer
7
  # =========================
8
+ model_name = "Qwen/Qwen2.5-0.5B-Instruct"
9
 
10
+ # Fast tokenizer for speed
11
  tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
12
 
13
+ # Load model with correct dtype
14
  model = AutoModelForCausalLM.from_pretrained(
15
  model_name,
16
+ device_map="auto", # Uses GPU if available, else CPU
17
+ dtype=torch.float32 # CPU inference works better with float32
 
18
  )
19
 
20
+ # Optional PyTorch 2.x compile (speeds up CPU inference)
21
  if torch.__version__.startswith("2"):
22
  model = torch.compile(model)
23
 
24
  # =========================
25
  # 2️⃣ Hardcoded system prompt
26
  # =========================
27
+ SYSTEM_PROMPT = "You are a friendly AI assistant that gives helpful and polite answers."
28
 
29
  # =========================
30
+ # 3️⃣ Optimized chat function
31
  # =========================
32
  def chat(user_prompt: str):
 
 
 
33
  messages = [
34
+ {"role": "system", "content": SYSTEM_PROMPT},
35
  {"role": "user", "content": user_prompt}
36
  ]
37
 
 
42
  add_generation_prompt=True
43
  )
44
 
45
+ # Encode input once
46
  inputs = tokenizer([text], return_tensors="pt").to(model.device)
47
 
48
+ # Faster generation settings
49
  outputs = model.generate(
50
  **inputs,
51
  max_new_tokens=128, # smaller = faster
52
  do_sample=False, # deterministic = faster
53
+ num_beams=1 # no beam search
54
  )
55
 
56
+ # Decode only the first sequence
57
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
58
  return response
59