looker01202 commited on
Commit
331b624
·
1 Parent(s): e6e9116

Gemini changes added 8

Browse files
Files changed (2) hide show
  1. app.py +41 -1
  2. granite3.3_2b_chat_template.jinja +14 -12
app.py CHANGED
@@ -52,6 +52,42 @@ def load_model():
52
 
53
  tokenizer, model, model_name = load_model()
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  # --- Start: Print Loaded Chat Template ---
56
  print("--- Tokenizer's Loaded Chat Template ---")
57
  # Check if the attribute exists before printing
@@ -168,11 +204,15 @@ def chat(message, history, hotel_id):
168
  for role, content in history_tuples:
169
  messages.append({"role": role, "content": content})
170
 
 
 
 
171
  # Apply the template
172
  input_text = tokenizer.apply_chat_template(
173
  messages,
174
  tokenize=False,
175
- add_generation_prompt=True
 
176
  )
177
 
178
  print("--- Granite Templated Input ---")
 
52
 
53
  tokenizer, model, model_name = load_model()
54
 
55
+
56
+ # --- Start: Apply CORRECTED Chat Template from File (if Granite) ---
57
+ if "granite" in model_name.lower():
58
+ template_filename = "granite3.3_2b_chat_template.jinja" # Use the new filename
59
+ applied_template = False
60
+ try:
61
+ # Assuming the template file is in the same directory as app.py (project root)
62
+ print(f"Attempting to load corrected chat template from: {template_filename}")
63
+ with open(template_filename, "r", encoding="utf-8") as f:
64
+ custom_chat_template_content = f.read()
65
+
66
+ # Assign the loaded template content
67
+ tokenizer.chat_template = custom_chat_template_content
68
+ applied_template = True
69
+ print(f"✅ Loaded and applied corrected chat template from: {template_filename}")
70
+
71
+ except FileNotFoundError:
72
+ print(f"⚠️ WARNING: Corrected template file '{template_filename}' not found.")
73
+ except Exception as e:
74
+ print(f"❌ ERROR reading corrected template file '{template_filename}': {e}")
75
+
76
+ # Fallback / Verification print
77
+ if not applied_template:
78
+ print("Falling back to tokenizer's default built-in template (which might be incorrect).")
79
+ print("--- Final Chat Template Being Used ---")
80
+ if hasattr(tokenizer, 'chat_template') and tokenizer.chat_template:
81
+ print(tokenizer.chat_template) # Print the template actually being used
82
+ else:
83
+ print("Tokenizer does not have a chat_template attribute or it is empty.")
84
+ print("------------------------------------")
85
+
86
+ else:
87
+ print("Model is not Granite, using default chat template.")
88
+ # --- End: Apply CORRECTED Chat Template from File ---
89
+
90
+
91
  # --- Start: Print Loaded Chat Template ---
92
  print("--- Tokenizer's Loaded Chat Template ---")
93
  # Check if the attribute exists before printing
 
204
  for role, content in history_tuples:
205
  messages.append({"role": role, "content": content})
206
 
207
+ # Set meta data (annotations)which influences the bot behaviour in the controls json
208
+ controls = {"length":"short","originality": "abstractive"}
209
+
210
  # Apply the template
211
  input_text = tokenizer.apply_chat_template(
212
  messages,
213
  tokenize=False,
214
+ add_generation_prompt=True,
215
+ controls=controls
216
  )
217
 
218
  print("--- Granite Templated Input ---")
granite3.3_2b_chat_template.jinja CHANGED
@@ -50,15 +50,17 @@ Finally, after the response is written, include a numbered list of sentences fro
50
  {{- '<|end_of_text|>
51
  ' }}
52
  {%- endfor %}
53
- {%- endif %}
54
- {%- for message in loop_messages %}
55
- {{- '<|start_of_role|>' + message['role'] + '<|end_of_role|>' + message['content'] + '<|end_of_text|>
56
- ' }}
57
- {%- if loop.last and add_generation_prompt %}
58
- {{- '<|start_of_role|>assistant' }}
59
- {%- if controls %}
60
- {{- ' ' + controls | tojson()}}
61
- {%- endif %}
62
- {{- '<|end_of_role|>' }}
63
- {%- endif %}
64
- {%- endfor %}
 
 
 
50
  {{- '<|end_of_text|>
51
  ' }}
52
  {%- endfor %}
53
+ {%- endif %}
54
+ {# Find this loop near the end of the template file #}
55
+ {%- for message in loop_messages %}
56
+ {%- if message['role'] != 'document' %} {# <<< ADD THIS LINE #}
57
+ {{- '<|start_of_role|>' + message['role'] + '<|end_of_role|>' + message['content'] + '<|end_of_text|>\n' }}
58
+ {%- if loop.last and add_generation_prompt %}
59
+ {{- '<|start_of_role|>assistant' }}
60
+ {%- if controls %}
61
+ {{- ' ' + controls | tojson()}}
62
+ {%- endif %}
63
+ {{- '&lt;|end_of_role|&gt;' }}
64
+ {%- endif %}
65
+ {%- endif %} {# <<< ADD THIS LINE #}
66
+ {%- endfor %}