prgrmc commited on
Commit
3a5837a
·
1 Parent(s): 01c8c48

add timestamped logging, comment not used functions on helper

Browse files
Files changed (2) hide show
  1. helper.py +64 -50
  2. main.py +0 -2
helper.py CHANGED
@@ -13,15 +13,29 @@ from huggingface_hub import login
13
 
14
  from transformers import AutoTokenizer, AutoModelForCausalLM
15
  import logging
 
 
16
  import psutil
17
  from typing import Dict, Any, Optional, Tuple
18
 
19
- # Add model caching and optimization
20
- from functools import lru_cache
21
- import torch.nn as nn
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Configure logging
24
- logging.basicConfig(level=logging.INFO)
25
  logger = logging.getLogger(__name__)
26
 
27
 
@@ -338,7 +352,7 @@ def get_game_state(inventory: Dict = None) -> Dict[str, Any]:
338
  character = world["kingdoms"]["Valdor"]["towns"]["Ravenhurst"]["npcs"][
339
  "Elara Brightshield"
340
  ]
341
- print(f"character in get_game_state: {character}")
342
 
343
  game_state = {
344
  "name": world["name"],
@@ -366,7 +380,7 @@ def get_game_state(inventory: Dict = None) -> Dict[str, Any]:
366
  "reputation": {"Valdor": 0, "Ravenhurst": 0},
367
  }
368
 
369
- # print(f"game_state in get_game_state: {game_state}")
370
 
371
  # Extract required data with fallbacks
372
  return game_state
@@ -791,7 +805,7 @@ Inventory: {json.dumps(game_state['inventory'])}"""
791
  # # Check for None response
792
  # if not model_output or not isinstance(model_output, list):
793
  # logger.error(f"Invalid model output: {model_output}")
794
- # print(f"Invalid model output: {model_output}")
795
  # return "You look around carefully."
796
 
797
  # if not model_output[0] or not isinstance(model_output[0], dict):
@@ -804,10 +818,10 @@ Inventory: {json.dumps(game_state['inventory'])}"""
804
  # logger.error("Empty response from model")
805
  # return "You look around carefully."
806
 
807
- # print(f"Full response in run_action: {full_response}")
808
 
809
  # response = extract_response_after_action(full_response, message)
810
- # print(f"Extracted response in run_action: {response}")
811
 
812
  # # Convert to second person
813
  # response = response.replace("Elara", "You")
@@ -837,7 +851,7 @@ Inventory: {json.dumps(game_state['inventory'])}"""
837
 
838
  response = completion.choices[0].message.content
839
 
840
- print(f"Generated response Inference API: {response}")
841
 
842
  if not response:
843
  return "You look around carefully."
@@ -849,12 +863,12 @@ Inventory: {json.dumps(game_state['inventory'])}"""
849
 
850
  # # Perform safety check before returning
851
  # safe = is_safe(response)
852
- # print(f"\nSafety Check Result: {'SAFE' if safe else 'UNSAFE'}")
853
  # logger.info(f"Safety check result: {'SAFE' if safe else 'UNSAFE'}")
854
 
855
  # if not safe:
856
  # logging.warning("Unsafe content detected - blocking response")
857
- # print("Unsafe content detected - Response blocked")
858
  # return "This response was blocked for safety reasons."
859
 
860
  # if safe:
@@ -878,7 +892,7 @@ Inventory: {json.dumps(game_state['inventory'])}"""
878
  if inventory_update:
879
  response += inventory_update
880
 
881
- print(f"Final response in run_action: {response}")
882
  # Validate response
883
  return response if response else "You look around carefully."
884
 
@@ -1320,7 +1334,7 @@ def is_safe(message: str) -> bool:
1320
 
1321
  # # result = safety_tokenizer.decode(output[0], skip_special_tokens=True)
1322
  # result = get_safety_response(prompt)
1323
- # print(f"Raw safety check result: {result}")
1324
 
1325
  # # # Extract response after prompt
1326
  # # if "[/INST]" in result:
@@ -1328,13 +1342,13 @@ def is_safe(message: str) -> bool:
1328
 
1329
  # # # Clean response
1330
  # # result = result.lower().strip()
1331
- # # print(f"Cleaned safety check result: {result}")
1332
  # # words = [word for word in result.split() if word in ["safe", "unsafe"]]
1333
 
1334
  # # # Take first valid response word
1335
  # # is_safe = words[0] == "safe" if words else False
1336
 
1337
- # # print("Final Safety check result:", is_safe)
1338
 
1339
  # is_safe = "safe" in result.lower().split()
1340
 
@@ -1348,39 +1362,39 @@ def is_safe(message: str) -> bool:
1348
  # return False
1349
 
1350
 
1351
- def detect_inventory_changes(game_state, output):
1352
- inventory = game_state["inventory"]
1353
- messages = [
1354
- {"role": "system", "content": system_prompt},
1355
- {"role": "user", "content": f"Current Inventory: {str(inventory)}"},
1356
- {"role": "user", "content": f"Recent Story: {output}"},
1357
- {"role": "user", "content": "Inventory Updates"},
1358
- ]
1359
-
1360
- input_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
1361
- model_output = generator(input_text, num_return_sequences=1, temperature=0.0)
1362
- response = model_output[0]["generated_text"]
1363
- result = json.loads(response)
1364
- return result["itemUpdates"]
1365
-
1366
-
1367
- def update_inventory(inventory, item_updates):
1368
- update_msg = ""
1369
- for update in item_updates:
1370
- name = update["name"]
1371
- change_amount = update["change_amount"]
1372
- if change_amount > 0:
1373
- if name not in inventory:
1374
- inventory[name] = change_amount
1375
- else:
1376
- inventory[name] += change_amount
1377
- update_msg += f"\nInventory: {name} +{change_amount}"
1378
- elif name in inventory and change_amount < 0:
1379
- inventory[name] += change_amount
1380
- update_msg += f"\nInventory: {name} {change_amount}"
1381
- if name in inventory and inventory[name] < 0:
1382
- del inventory[name]
1383
- return update_msg
1384
 
1385
 
1386
  logging.info("Finished helper function")
 
13
 
14
  from transformers import AutoTokenizer, AutoModelForCausalLM
15
  import logging
16
+ import sys
17
+ from datetime import datetime
18
  import psutil
19
  from typing import Dict, Any, Optional, Tuple
20
 
21
+ # # Add model caching and optimization
22
+ # from functools import lru_cache
23
+ # import torch.nn as nn
24
+
25
+
26
+ # Custom tprint function with timestamp
27
+ def tprint(*args, **kwargs):
28
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
29
+ print(f"[{timestamp}] [{sys._getframe().f_back.f_lineno}]", *args, **kwargs)
30
+
31
+
32
+ # Configure logging with timestamp and line numbers
33
+ logging.basicConfig(
34
+ level=logging.INFO,
35
+ format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s",
36
+ datefmt="%Y-%m-%d %H:%M:%S",
37
+ )
38
 
 
 
39
  logger = logging.getLogger(__name__)
40
 
41
 
 
352
  character = world["kingdoms"]["Valdor"]["towns"]["Ravenhurst"]["npcs"][
353
  "Elara Brightshield"
354
  ]
355
+ tprint(f"character in get_game_state: {character}")
356
 
357
  game_state = {
358
  "name": world["name"],
 
380
  "reputation": {"Valdor": 0, "Ravenhurst": 0},
381
  }
382
 
383
+ # tprint(f"game_state in get_game_state: {game_state}")
384
 
385
  # Extract required data with fallbacks
386
  return game_state
 
805
  # # Check for None response
806
  # if not model_output or not isinstance(model_output, list):
807
  # logger.error(f"Invalid model output: {model_output}")
808
+ # tprint(f"Invalid model output: {model_output}")
809
  # return "You look around carefully."
810
 
811
  # if not model_output[0] or not isinstance(model_output[0], dict):
 
818
  # logger.error("Empty response from model")
819
  # return "You look around carefully."
820
 
821
+ # tprint(f"Full response in run_action: {full_response}")
822
 
823
  # response = extract_response_after_action(full_response, message)
824
+ # tprint(f"Extracted response in run_action: {response}")
825
 
826
  # # Convert to second person
827
  # response = response.replace("Elara", "You")
 
851
 
852
  response = completion.choices[0].message.content
853
 
854
+ tprint(f"Generated response Inference API: {response}")
855
 
856
  if not response:
857
  return "You look around carefully."
 
863
 
864
  # # Perform safety check before returning
865
  # safe = is_safe(response)
866
+ # tprint(f"\nSafety Check Result: {'SAFE' if safe else 'UNSAFE'}")
867
  # logger.info(f"Safety check result: {'SAFE' if safe else 'UNSAFE'}")
868
 
869
  # if not safe:
870
  # logging.warning("Unsafe content detected - blocking response")
871
+ # tprint("Unsafe content detected - Response blocked")
872
  # return "This response was blocked for safety reasons."
873
 
874
  # if safe:
 
892
  if inventory_update:
893
  response += inventory_update
894
 
895
+ tprint(f"Final response in run_action: {response}")
896
  # Validate response
897
  return response if response else "You look around carefully."
898
 
 
1334
 
1335
  # # result = safety_tokenizer.decode(output[0], skip_special_tokens=True)
1336
  # result = get_safety_response(prompt)
1337
+ # tprint(f"Raw safety check result: {result}")
1338
 
1339
  # # # Extract response after prompt
1340
  # # if "[/INST]" in result:
 
1342
 
1343
  # # # Clean response
1344
  # # result = result.lower().strip()
1345
+ # # tprint(f"Cleaned safety check result: {result}")
1346
  # # words = [word for word in result.split() if word in ["safe", "unsafe"]]
1347
 
1348
  # # # Take first valid response word
1349
  # # is_safe = words[0] == "safe" if words else False
1350
 
1351
+ # # tprint("Final Safety check result:", is_safe)
1352
 
1353
  # is_safe = "safe" in result.lower().split()
1354
 
 
1362
  # return False
1363
 
1364
 
1365
+ # def detect_inventory_changes(game_state, output):
1366
+ # inventory = game_state["inventory"]
1367
+ # messages = [
1368
+ # {"role": "system", "content": system_prompt},
1369
+ # {"role": "user", "content": f"Current Inventory: {str(inventory)}"},
1370
+ # {"role": "user", "content": f"Recent Story: {output}"},
1371
+ # {"role": "user", "content": "Inventory Updates"},
1372
+ # ]
1373
+
1374
+ # input_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
1375
+ # model_output = generator(input_text, num_return_sequences=1, temperature=0.0)
1376
+ # response = model_output[0]["generated_text"]
1377
+ # result = json.loads(response)
1378
+ # return result["itemUpdates"]
1379
+
1380
+
1381
+ # def update_inventory(inventory, item_updates):
1382
+ # update_msg = ""
1383
+ # for update in item_updates:
1384
+ # name = update["name"]
1385
+ # change_amount = update["change_amount"]
1386
+ # if change_amount > 0:
1387
+ # if name not in inventory:
1388
+ # inventory[name] = change_amount
1389
+ # else:
1390
+ # inventory[name] += change_amount
1391
+ # update_msg += f"\nInventory: {name} +{change_amount}"
1392
+ # elif name in inventory and change_amount < 0:
1393
+ # inventory[name] += change_amount
1394
+ # update_msg += f"\nInventory: {name} {change_amount}"
1395
+ # if name in inventory and inventory[name] < 0:
1396
+ # del inventory[name]
1397
+ # return update_msg
1398
 
1399
 
1400
  logging.info("Finished helper function")
main.py CHANGED
@@ -9,8 +9,6 @@ from helper import (
9
  run_action,
10
  start_game,
11
  is_safe,
12
- detect_inventory_changes,
13
- update_inventory,
14
  )
15
 
16
  import pdb
 
9
  run_action,
10
  start_game,
11
  is_safe,
 
 
12
  )
13
 
14
  import pdb