JJteam commited on
Commit
2247b00
1 Parent(s): 1aba1d6

adding logger

Browse files
Files changed (2) hide show
  1. MM-REACT/app.py +88 -19
  2. requirements.txt +1 -0
MM-REACT/app.py CHANGED
@@ -18,6 +18,12 @@ from langchain.llms import AzureOpenAI
18
  from langchain.utilities import ImunAPIWrapper, ImunMultiAPIWrapper
19
  from openai.error import AuthenticationError, InvalidRequestError, RateLimitError
20
  import argparse
 
 
 
 
 
 
21
 
22
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
23
  BUG_FOUND_MSG = "There is a bug in the application!"
@@ -29,17 +35,17 @@ MAX_TOKENS = 512
29
  AGRS = None
30
  #####################################
31
 
32
- # resets memory
33
- def reset_memory(history):
34
- # global memory
35
- # memory.clear()
36
- print ("clearning memory, loading langchain...")
37
- load_chain()
38
- history = []
39
- return history, history
40
 
41
  # load chain
42
- def load_chain(history):
43
  global ARGS
44
 
45
  if ARGS.openAIModel == 'openAIGPT35':
@@ -196,10 +202,16 @@ def load_chain(history):
196
  ]
197
 
198
  chain = initialize_agent(tools, llm, agent="conversational-assistant", verbose=True, memory=memory, return_intermediate_steps=True, max_iterations=4)
 
 
 
199
  print("langchain reloaded")
 
 
 
200
  history = []
201
- history.append(("Show me what you got!", "Hi Human, I am ready to serve!"))
202
- return history, history, chain, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.UploadButton.update(visible=True)
203
 
204
 
205
  # executes input typed by human
@@ -231,13 +243,19 @@ class ChatWrapper:
231
  self.lock = Lock()
232
 
233
  def __call__(
234
- self, inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain]
235
  ):
 
236
  """Execute the chat functionality."""
237
  self.lock.acquire()
238
  try:
239
  print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
240
  print("inp: " + inp)
 
 
 
 
 
241
  history = history or []
242
  # If chain is None, that is because no API key was provided.
243
  output = "Please paste your OpenAI key from openai.com to use this app. " + str(datetime.datetime.now())
@@ -262,15 +280,49 @@ class ChatWrapper:
262
  self.lock.release()
263
 
264
  print (history)
 
 
 
 
 
265
  return history, history, ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
 
267
  # upload image
268
- def add_image(state, chain, image):
269
  global ARGS
270
  state = state or []
271
 
272
  url_input_for_chain = "http://0.0.0.0:{}/file={}".format(ARGS.port, image.name)
273
-
274
  outputs = run_chain(chain, url_input_for_chain)
275
 
276
  ########################
@@ -286,6 +338,11 @@ def add_image(state, chain, image):
286
 
287
 
288
  print (state)
 
 
 
 
 
289
  return state, state
290
 
291
  # extract image url from response and process differently
@@ -337,8 +394,9 @@ def init_and_kick_off():
337
  llm_state = gr.State()
338
  history_state = gr.State()
339
  chain_state = gr.State()
 
340
 
341
- reset_btn = gr.Button(value="!!!CLICK to wake up the AI!!!", variant="secondary", elem_id="resetbtn").style(full_width=True)
342
 
343
  with gr.Row():
344
  chatbot = gr.Chatbot(elem_id="chatbot").style(height=620)
@@ -353,16 +411,27 @@ def init_and_kick_off():
353
  with gr.Column(scale=0.10, min_width=0):
354
  btn = gr.UploadButton("📁", file_types=["image"], visible=False).style(full_width=True)
355
 
356
- message.submit(chat, inputs=[message, history_state, chain_state],
 
 
 
 
 
 
 
 
 
 
 
357
  outputs=[chatbot, history_state, message])
358
 
359
- submit.click(chat, inputs=[message, history_state, chain_state],
360
  outputs=[chatbot, history_state, message])
361
 
362
- btn.upload(add_image, inputs=[history_state, chain_state, btn], outputs=[history_state, chatbot])
363
 
364
  # load the chain
365
- reset_btn.click(load_chain, inputs=[history_state], outputs=[chatbot, history_state, chain_state, message, submit, btn])
366
 
367
  # launch the app
368
  block.launch(server_name="0.0.0.0", server_port = ARGS.port)
 
18
  from langchain.utilities import ImunAPIWrapper, ImunMultiAPIWrapper
19
  from openai.error import AuthenticationError, InvalidRequestError, RateLimitError
20
  import argparse
21
+ import logging
22
+ from opencensus.ext.azure.log_exporter import AzureLogHandler
23
+ import uuid
24
+
25
+ logger = None
26
+
27
 
28
  OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
29
  BUG_FOUND_MSG = "There is a bug in the application!"
 
35
  AGRS = None
36
  #####################################
37
 
38
+
39
+ def get_logger():
40
+ global logger
41
+ if logger is None:
42
+ logger = logging.getLogger(__name__)
43
+ logger.addHandler(AzureLogHandler())
44
+ return logger
45
+
46
 
47
  # load chain
48
+ def load_chain(history, log_state):
49
  global ARGS
50
 
51
  if ARGS.openAIModel == 'openAIGPT35':
 
202
  ]
203
 
204
  chain = initialize_agent(tools, llm, agent="conversational-assistant", verbose=True, memory=memory, return_intermediate_steps=True, max_iterations=4)
205
+ log_state = log_state or ""
206
+ print ("log_state {}".format(log_state))
207
+ log_state = str(uuid.uuid1())
208
  print("langchain reloaded")
209
+ # eproperties = {'custom_dimensions': {'key_1': 'value_1', 'key_2': 'value_2'}}
210
+ properties = {'custom_dimensions': {'session': log_state}}
211
+ get_logger().warning("langchain reloaded", extra=properties)
212
  history = []
213
+ history.append(("Show me what you got!", "Hi Human, Please upload an image to get started!"))
214
+ return history, history, chain, log_state, gr.Textbox.update(visible=True), gr.Button.update(visible=True), gr.UploadButton.update(visible=True)
215
 
216
 
217
  # executes input typed by human
 
243
  self.lock = Lock()
244
 
245
  def __call__(
246
+ self, inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain], log_state
247
  ):
248
+
249
  """Execute the chat functionality."""
250
  self.lock.acquire()
251
  try:
252
  print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
253
  print("inp: " + inp)
254
+
255
+ properties = {'custom_dimensions': {'session': log_state}}
256
+ get_logger().warning("inp: " + inp, extra=properties)
257
+
258
+
259
  history = history or []
260
  # If chain is None, that is because no API key was provided.
261
  output = "Please paste your OpenAI key from openai.com to use this app. " + str(datetime.datetime.now())
 
280
  self.lock.release()
281
 
282
  print (history)
283
+ properties = {'custom_dimensions': {'session': log_state}}
284
+ if outputs is None:
285
+ outputs = ""
286
+ get_logger().warning(str(json.dumps(outputs)), extra=properties)
287
+
288
  return history, history, ""
289
+
290
+ def add_image_with_path(state, chain, imagepath, log_state):
291
+ global ARGS
292
+ state = state or []
293
+
294
+ url_input_for_chain = "http://0.0.0.0:{}/file={}".format(ARGS.port, imagepath)
295
+
296
+ outputs = run_chain(chain, url_input_for_chain)
297
+
298
+ ########################
299
+ # multi line response handling
300
+ outputs = process_chain_output(outputs)
301
+
302
+ for i, output in enumerate(outputs):
303
+ if i==0:
304
+ # state.append((f"![](/file={imagepath})", output))
305
+ state.append(((imagepath,), output))
306
+ else:
307
+ state.append((None, output))
308
+
309
+
310
+ print (state)
311
+ properties = {'custom_dimensions': {'session': log_state}}
312
+ get_logger().warning("url_input_for_chain: " + url_input_for_chain, extra=properties)
313
+ if outputs is None:
314
+ outputs = ""
315
+ get_logger().warning(str(json.dumps(outputs)), extra=properties)
316
+ return state, state
317
+
318
 
319
  # upload image
320
+ def add_image(state, chain, image, log_state):
321
  global ARGS
322
  state = state or []
323
 
324
  url_input_for_chain = "http://0.0.0.0:{}/file={}".format(ARGS.port, image.name)
325
+
326
  outputs = run_chain(chain, url_input_for_chain)
327
 
328
  ########################
 
338
 
339
 
340
  print (state)
341
+ properties = {'custom_dimensions': {'session': log_state}}
342
+ get_logger().warning("url_input_for_chain: " + url_input_for_chain, extra=properties)
343
+ if outputs is None:
344
+ outputs = ""
345
+ get_logger().warning(str(json.dumps(outputs)), extra=properties)
346
  return state, state
347
 
348
  # extract image url from response and process differently
 
394
  llm_state = gr.State()
395
  history_state = gr.State()
396
  chain_state = gr.State()
397
+ log_state = gr.State()
398
 
399
+ reset_btn = gr.Button(value="!!!CLICK to wake up MM-REACT!!!", variant="secondary", elem_id="resetbtn").style(full_width=True)
400
 
401
  with gr.Row():
402
  chatbot = gr.Chatbot(elem_id="chatbot").style(height=620)
 
411
  with gr.Column(scale=0.10, min_width=0):
412
  btn = gr.UploadButton("📁", file_types=["image"], visible=False).style(full_width=True)
413
 
414
+ # with gr.Row():
415
+ # with gr.Column():
416
+ # example1Image = gr.Image("images/money.png", interactive=False).style(height=100, width=100)
417
+ # with gr.Column():
418
+ # example1ImagePath = gr.Text("images/money.png", interactive=False, visible=False)
419
+ # with gr.Column():
420
+ # example1ImageButton = gr.Button(value="Try it", variant="secondary").style(full_width=True)
421
+ # example1ImageButton.click(add_image_with_path, inputs=[history_state, chain_state, example1ImagePath],
422
+ # outputs=[history_state, chatbot])
423
+
424
+
425
+ message.submit(chat, inputs=[message, history_state, chain_state, log_state],
426
  outputs=[chatbot, history_state, message])
427
 
428
+ submit.click(chat, inputs=[message, history_state, chain_state, log_state],
429
  outputs=[chatbot, history_state, message])
430
 
431
+ btn.upload(add_image, inputs=[history_state, chain_state, btn, log_state], outputs=[history_state, chatbot])
432
 
433
  # load the chain
434
+ reset_btn.click(load_chain, inputs=[history_state, log_state], outputs=[chatbot, history_state, chain_state, log_state, message, submit, btn])
435
 
436
  # launch the app
437
  block.launch(server_name="0.0.0.0", server_port = ARGS.port)
requirements.txt CHANGED
@@ -1,3 +1,4 @@
 
1
  imagesize==1.4.1
2
  gradio==3.21.0
3
  openai==0.26.4
 
1
+ opencensus
2
  imagesize==1.4.1
3
  gradio==3.21.0
4
  openai==0.26.4