looker01202 commited on
Commit
daecd7b
Β·
1 Parent(s): 66ab75c

Gemini changes added 16

Browse files
Files changed (1) hide show
  1. app.py +39 -79
app.py CHANGED
@@ -220,94 +220,54 @@ def chat(message, history, hotel_id):
220
  print(input_text)
221
  print("-----------------------------")
222
 
223
- inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
 
 
 
224
 
 
225
  with torch.no_grad():
226
- # Using do_sample=False for more deterministic RAG based on context
227
- outputs = model.generate(inputs, max_new_tokens=1024, do_sample=False)
228
-
229
- decoded = tokenizer.decode(outputs[0], skip_special_tokens=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
- print("--- Granite Raw Output ---")
232
- print(decoded)
233
- print("--------------------------")
234
 
235
- # --- Start: UPDATED Response Parsing for Granite (v7 - Improved Regex & Fallback) ---
236
- try:
237
- assistant_marker = "<|start_of_role|>assistant"
238
- end_role_marker_literal = "<|end_of_role|>" # Use for fallback
239
- end_text_marker_literal = "<|end_of_text|>" # Use for fallback
240
-
241
- start_index = decoded.rfind(assistant_marker)
242
-
243
- if start_index != -1:
244
- search_area = decoded[start_index:]
245
-
246
- # Regex pattern v7: Focus on capturing group between role and text markers
247
- # - .*? : Match assistant start tag + controls non-greedily
248
- # - <\| : Match literal <|
249
- # - [^>]*? : Match any non-> characters non-greedily (lenient end_of_role)
250
- # - \|> : Match literal |>
251
- # - \s* : Match whitespace
252
- # - (.*?) : Capture content non-greedily (GROUP 1)
253
- # - <\| : Match literal <|
254
- # - [^>]*? : Match any non-> characters non-greedily (lenient end_of_text)
255
- # - \|> : Match literal |>
256
- pattern = re.compile(r".*?<\|[^>]*?role[^>]*?\|>\s*(.*?)<\|[^>]*?text[^>]*?\|>", re.DOTALL)
257
-
258
- match = pattern.search(search_area)
259
-
260
- if match:
261
- response = match.group(1).strip()
262
- if not response:
263
- response = "Sorry, I encountered an issue generating a response (empty)."
264
- else:
265
- # If the pattern still didn't match - IMPROVED FALLBACK
266
- print(f"❌ Error: Regex pattern v7 did not match structure. Search area started with: {repr(search_area[:150])}")
267
- try:
268
- # Fallback: Find the first end_role marker *after* the start_index
269
- end_role_fb_index = decoded.find(end_role_marker_literal, start_index)
270
- if end_role_fb_index != -1:
271
- # Find the first end_text marker *after* the end_role marker
272
- content_start_fb_index = end_role_fb_index + len(end_role_marker_literal)
273
- end_text_fb_index = decoded.find(end_text_marker_literal, content_start_fb_index)
274
-
275
- if end_text_fb_index != -1:
276
- # Extract between them
277
- potential_response = decoded[content_start_fb_index:end_text_fb_index].strip()
278
- if potential_response:
279
- print("⚠️ WARNING: Using improved fallback parsing due to regex failure.")
280
- response = potential_response
281
- else:
282
- raise ValueError("Improved fallback parsing yielded empty string.")
283
- else:
284
- raise ValueError("Improved fallback parsing failed: end_text marker not found after end_role marker.")
285
- else:
286
- raise ValueError("Improved fallback parsing failed: end_role marker not found after assistant start.")
287
-
288
- # --- ADD THIS DEBUG LINE ---
289
- print(f"DEBUG: Final response variable before UI append = {repr(response)}")
290
- # --- END ADD THIS DEBUG LINE ---
291
-
292
- except Exception as fallback_e:
293
- print(f"Improved fallback parsing also failed: {fallback_e}")
294
- response = "Sorry, I couldn't parse the model's response structure (regex & fallback failed)."
295
 
296
- else:
297
- print("❌ Error: Assistant start marker not found in decoded output.")
298
- response = "Sorry, I couldn't find the start of the assistant's response."
 
 
 
 
 
299
 
300
- except Exception as e:
301
- print(f"❌ Unexpected Error during response parsing: {e}")
302
- response = "Sorry, an unexpected error occurred while parsing the response."
303
- # --- End: UPDATED Response Parsing for Granite (v7 - Improved Regex & Fallback) ---
304
 
305
 
306
- # Add the final assistant reply to the UI history
307
- ui_history.append({"role": "assistant", "content": response})
308
 
309
- # Final yield with assistant reply
310
- yield ui_history, "" # Update chat, keep textbox cleared
311
 
312
 
313
  # --- Start: Dynamic Hotel ID Detection ---
 
220
  print(input_text)
221
  print("-----------------------------")
222
 
223
+ # --- Tokenize AND get input length/attention mask ---
224
+ inputs = tokenizer(input_text, return_tensors="pt").to(device) # Use tokenizer()
225
+ input_length = inputs.input_ids.shape[1] # Define input_length using input_ids
226
+ print(f"DEBUG: Input token length = {input_length}") # Keep this debug print
227
 
228
+ # --- Generate using input_ids and attention_mask ---
229
  with torch.no_grad():
230
+ outputs = model.generate(
231
+ inputs.input_ids, # Pass input_ids explicitly
232
+ attention_mask=inputs.attention_mask, # Pass attention_mask
233
+ max_new_tokens=1024,
234
+ do_sample=False
235
+ )
236
+
237
+ # --- Raw output shape printing (keep) ---
238
+ print("--- Granite Raw Output Tokens (Shape) ---")
239
+ print(outputs.shape)
240
+ print("-----------------------------------------")
241
+
242
+ # --- Start: NEW Decoding Strategy (like IBM example) ---
243
+ try:
244
+ # Get only the newly generated token IDs
245
+ new_token_ids = outputs[0][input_length:]
246
+ print(f"DEBUG: Number of new tokens generated = {len(new_token_ids)}") # Debug print
247
 
248
+ # Decode only the new tokens, skipping special tokens like <|end_of_text|>
249
+ response = tokenizer.decode(new_token_ids, skip_special_tokens=True).strip()
250
+ print(f"DEBUG: Decoded response (skip_special_tokens=True) = {repr(response)}") # Debug print
251
 
252
+ if not response:
253
+ response = "Sorry, I encountered an issue generating a response (empty)."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
 
255
+ except Exception as e:
256
+ print(f"❌ Unexpected Error during NEW decoding: {e}")
257
+ response = "Sorry, an unexpected error occurred during decoding."
258
+ # --- End: NEW Decoding Strategy ---
259
+
260
+ # --- ADD THIS DEBUG LINE (if not already present) ---
261
+ print(f"DEBUG: Final response variable before UI append = {repr(response)}")
262
+ # --- END ADD THIS DEBUG LINE ---
263
 
 
 
 
 
264
 
265
 
266
+ # Add the final assistant reply to the UI history
267
+ ui_history.append({"role": "assistant", "content": response})
268
 
269
+ # Final yield with assistant reply
270
+ yield ui_history, "" # Update chat, keep textbox cleared
271
 
272
 
273
  # --- Start: Dynamic Hotel ID Detection ---