DylanZimmer commited on
Commit
5ebc9ea
·
1 Parent(s): 20ed78d

reWritten

Browse files
Files changed (1) hide show
  1. app.py +16 -41
app.py CHANGED
@@ -6,52 +6,29 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
6
  # Load model and tokenizer
7
  model_name = "HuggingFaceTB/SmolLM3-3B"
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
10
 
11
- @spaces.GPU
12
- def chat_with_smollm3(message, history, system_prompt="", enable_thinking=True, temperature=0.6, top_p=0.95, max_tokens=32768):
13
- """
14
- Chat with SmolLM3-3B model with full feature support
15
- """
16
- # Prepare messages
17
  messages = []
18
-
19
- # Add system prompt if provided
20
  if system_prompt.strip():
21
- # Handle thinking mode flags in system prompt
22
- if enable_thinking and "/no_think" not in system_prompt:
23
- if "/think" not in system_prompt:
24
- system_prompt += "/think"
25
- elif not enable_thinking and "/think" not in system_prompt:
26
- if "/no_think" not in system_prompt:
27
- system_prompt += "/no_think"
28
  messages.append({"role": "system", "content": system_prompt})
29
- else:
30
- # Use enable_thinking parameter if no system prompt
31
- if not enable_thinking:
32
- messages.append({"role": "system", "content": "/no_think"})
33
-
34
- # Add conversation history
35
  for human_msg, assistant_msg in history:
36
  messages.append({"role": "user", "content": human_msg})
37
  if assistant_msg:
38
  messages.append({"role": "assistant", "content": assistant_msg})
39
-
40
- # Add current message
41
  messages.append({"role": "user", "content": message})
42
-
43
- # Apply chat template
44
  text = tokenizer.apply_chat_template(
45
  messages,
46
  tokenize=False,
47
- add_generation_prompt=True,
48
- enable_thinking=enable_thinking if not system_prompt.strip() else None
49
  )
50
-
51
- # Tokenize input
52
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
53
-
54
- # Generate response
55
  with torch.no_grad():
56
  generated_ids = model.generate(
57
  **model_inputs,
@@ -61,22 +38,20 @@ def chat_with_smollm3(message, history, system_prompt="", enable_thinking=True,
61
  do_sample=True,
62
  pad_token_id=tokenizer.eos_token_id
63
  )
64
-
65
- # Decode response
66
  output_ids = generated_ids[0][len(model_inputs.input_ids[0]):]
67
  response = tokenizer.decode(output_ids, skip_special_tokens=True)
68
 
69
  return response
70
 
 
 
71
  demo = gr.ChatInterface(
72
- fn=chat_with_smollm3,
 
73
  additional_inputs=[
74
- gr.Textbox(label="System Prompt", value=""),
75
- gr.Checkbox(label="Enable Thinking", value=True),
76
- gr.Slider(0, 1, value=0.6, step=0.01, label="Temperature"),
77
- gr.Slider(0, 1, value=0.95, step=0.01, label="Top P"),
78
- gr.Number(value=32768, label="Max Tokens")
79
- ]
80
  )
81
 
82
  demo.launch()
 
6
  # Load model and tokenizer
7
  model_name = "HuggingFaceTB/SmolLM3-3B"
8
  tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")\
10
 
11
+ def chat_fxn_caller(message, history, system_prompt="", temperature=0.6, top_p=0.95, max_tokens=32768):
 
 
 
 
 
12
  messages = []
13
+
 
14
  if system_prompt.strip():
 
 
 
 
 
 
 
15
  messages.append({"role": "system", "content": system_prompt})
16
+
 
 
 
 
 
17
  for human_msg, assistant_msg in history:
18
  messages.append({"role": "user", "content": human_msg})
19
  if assistant_msg:
20
  messages.append({"role": "assistant", "content": assistant_msg})
21
+
 
22
  messages.append({"role": "user", "content": message})
23
+
 
24
  text = tokenizer.apply_chat_template(
25
  messages,
26
  tokenize=False,
27
+ add_generation_prompt=True #SmolLm3 specific, tells model give next response
 
28
  )
29
+
 
30
  model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
31
+
 
32
  with torch.no_grad():
33
  generated_ids = model.generate(
34
  **model_inputs,
 
38
  do_sample=True,
39
  pad_token_id=tokenizer.eos_token_id
40
  )
41
+
 
42
  output_ids = generated_ids[0][len(model_inputs.input_ids[0]):]
43
  response = tokenizer.decode(output_ids, skip_special_tokens=True)
44
 
45
  return response
46
 
47
+ prompt = "Be a good chatbox"
48
+
49
  demo = gr.ChatInterface(
50
+ chat_fxn_caller,
51
+ type="messages",
52
  additional_inputs=[
53
+ gr.Textbox(prompt, label="System Prompt"),
54
+ ],
 
 
 
 
55
  )
56
 
57
  demo.launch()