Chris4K commited on
Commit
89880a6
·
verified ·
1 Parent(s): f033509

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -4
app.py CHANGED
@@ -39,7 +39,7 @@ class ToolLoader:
39
  tool = load_tool(tool_name)
40
  loaded_tools.append(tool)
41
  except Exception as e:
42
- print(f"Error loading tool '{tool_name}': {e}")
43
  return loaded_tools
44
 
45
  class CustomHfAgent(Agent):
@@ -64,12 +64,12 @@ class CustomHfAgent(Agent):
64
  response = requests.post(self.url_endpoint, json=inputs, headers=headers)
65
 
66
  if response.status_code == 429:
67
- print("Getting rate-limited, waiting a tiny bit before trying again.")
68
  time.sleep(1)
69
  return self._generate_one(prompt)
70
  elif response.status_code != 200:
71
  raise ValueError(f"Errors {inputs} {response.status_code}: {response.json()}")
72
- print(response)
73
  result = response.json()[0]["generated_text"]
74
  for stop_seq in stop:
75
  if result.endswith(stop_seq):
@@ -77,6 +77,11 @@ class CustomHfAgent(Agent):
77
  return result
78
 
79
  def handle_submission(user_message, selected_tools, url_endpoint):
 
 
 
 
 
80
  agent = CustomHfAgent(
81
  url_endpoint=url_endpoint,
82
  token=os.environ['HF_token'],
@@ -86,10 +91,18 @@ def handle_submission(user_message, selected_tools, url_endpoint):
86
 
87
  response = agent.run(user_message)
88
 
89
- print("Agent Response\n {}".format(response))
90
 
91
  return response
92
 
 
 
 
 
 
 
 
 
93
  # Define the tool names to load
94
  tool_names = [
95
  "Chris4K/random-character-tool",
@@ -167,6 +180,8 @@ elif tabs[1]:
167
  "https://api-inference.huggingface.co/models/gpt2"
168
  ])
169
 
 
 
170
 
171
  tool_checkboxes = [st.checkbox(f"{tool.name} --- {tool.description} ") for tool in tool_loader.tools]
172
 
 
39
  tool = load_tool(tool_name)
40
  loaded_tools.append(tool)
41
  except Exception as e:
42
+ log_response(f"Error loading tool '{tool_name}': {e}")
43
  return loaded_tools
44
 
45
  class CustomHfAgent(Agent):
 
64
  response = requests.post(self.url_endpoint, json=inputs, headers=headers)
65
 
66
  if response.status_code == 429:
67
+ log_response("Getting rate-limited, waiting a tiny bit before trying again.")
68
  time.sleep(1)
69
  return self._generate_one(prompt)
70
  elif response.status_code != 200:
71
  raise ValueError(f"Errors {inputs} {response.status_code}: {response.json()}")
72
+ log_response(response)
73
  result = response.json()[0]["generated_text"]
74
  for stop_seq in stop:
75
  if result.endswith(stop_seq):
 
77
  return result
78
 
79
  def handle_submission(user_message, selected_tools, url_endpoint):
80
+
81
+ log_response("User input \n {}".format(user_message))
82
+ log_response("selected_tools \n {}".format(selected_tools))
83
+ log_response("url_endpoint \n {}".format(url_endpoint))
84
+
85
  agent = CustomHfAgent(
86
  url_endpoint=url_endpoint,
87
  token=os.environ['HF_token'],
 
91
 
92
  response = agent.run(user_message)
93
 
94
+ log_response("Agent Response\n {}".format(response))
95
 
96
  return response
97
 
98
+
99
+ def log_response(response):
100
+ if log_enabled:
101
+ with st.chat_message("ai"):
102
+ st.markdown("Agent Response\n {}".format(response))
103
+ print(response)
104
+
105
+
106
  # Define the tool names to load
107
  tool_names = [
108
  "Chris4K/random-character-tool",
 
180
  "https://api-inference.huggingface.co/models/gpt2"
181
  ])
182
 
183
+ # Add a checkbox for enabling logging
184
+ log_enabled = st.checkbox("Enable Logging")
185
 
186
  tool_checkboxes = [st.checkbox(f"{tool.name} --- {tool.description} ") for tool in tool_loader.tools]
187