Spaces:
Runtime error
Runtime error
Mehul Patel commited on
Commit ·
5487a42
1
Parent(s): 50528f8
improved logging
Browse files- app.py +7 -2
- custom_logging.py +45 -3
- rasa_socketio_client.py +10 -4
app.py
CHANGED
|
@@ -82,9 +82,13 @@
|
|
| 82 |
import gradio as gr
|
| 83 |
import asyncio
|
| 84 |
from rasa_socketio_client import RasaSocketIOClient
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
logger = get_logger(__name__)
|
| 88 |
rasa_io_url = "https://clairify.ai/socket.io"
|
| 89 |
theme = gr.themes.Default(primary_hue="cyan", secondary_hue="violet")
|
| 90 |
|
|
@@ -107,6 +111,7 @@ with gr.Blocks(theme=theme) as demo:
|
|
| 107 |
else:
|
| 108 |
new_entry = (None, message.get('text', ''))
|
| 109 |
chat_history.append(new_entry)
|
|
|
|
| 110 |
yield "", chat_history
|
| 111 |
|
| 112 |
async def handle_clear_button():
|
|
|
|
| 82 |
import gradio as gr
|
| 83 |
import asyncio
|
| 84 |
from rasa_socketio_client import RasaSocketIOClient
|
| 85 |
+
import logging
|
| 86 |
+
import custom_logging
|
| 87 |
+
|
| 88 |
+
# Obtain a logger for this module
|
| 89 |
+
logger = logging.getLogger("gradio_app")
|
| 90 |
+
logger.important(f"Logger handlers: {logger.handlers}")
|
| 91 |
|
|
|
|
| 92 |
rasa_io_url = "https://clairify.ai/socket.io"
|
| 93 |
theme = gr.themes.Default(primary_hue="cyan", secondary_hue="violet")
|
| 94 |
|
|
|
|
| 111 |
else:
|
| 112 |
new_entry = (None, message.get('text', ''))
|
| 113 |
chat_history.append(new_entry)
|
| 114 |
+
logger.important(f"Latest chat history: {chat_history}")
|
| 115 |
yield "", chat_history
|
| 116 |
|
| 117 |
async def handle_clear_button():
|
custom_logging.py
CHANGED
|
@@ -1,16 +1,58 @@
|
|
| 1 |
import logging
|
|
|
|
| 2 |
|
|
|
|
| 3 |
IMPORTANT_LEVEL_NUM = 25
|
| 4 |
logging.addLevelName(IMPORTANT_LEVEL_NUM, "IMPORTANT")
|
| 5 |
|
| 6 |
def important(self, message, *args, **kws):
|
| 7 |
if self.isEnabledFor(IMPORTANT_LEVEL_NUM):
|
|
|
|
| 8 |
self._log(IMPORTANT_LEVEL_NUM, message, args, **kws)
|
| 9 |
|
|
|
|
| 10 |
logging.Logger.important = important
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def get_logger(name):
|
| 15 |
-
return logging.getLogger(name)
|
| 16 |
|
|
|
|
| 1 |
import logging
|
| 2 |
+
import time
|
| 3 |
|
| 4 |
+
# Define a new logging level
|
| 5 |
IMPORTANT_LEVEL_NUM = 25
|
| 6 |
logging.addLevelName(IMPORTANT_LEVEL_NUM, "IMPORTANT")
|
| 7 |
|
| 8 |
def important(self, message, *args, **kws):
|
| 9 |
if self.isEnabledFor(IMPORTANT_LEVEL_NUM):
|
| 10 |
+
# Use _log directly with the custom level
|
| 11 |
self._log(IMPORTANT_LEVEL_NUM, message, args, **kws)
|
| 12 |
|
| 13 |
+
# Patch the Logger class with the new method
|
| 14 |
logging.Logger.important = important
|
| 15 |
|
| 16 |
+
class CustomFormatter(logging.Formatter):
|
| 17 |
+
def formatTime(self, record, datefmt=None):
|
| 18 |
+
# Custom time format: Human-readable + Unix time with milliseconds
|
| 19 |
+
local_time = time.strftime('%b-%d %I:%M:%S %p', time.localtime(record.created))
|
| 20 |
+
unix_time_with_milliseconds = f"{record.created:.3f}"
|
| 21 |
+
return f"{local_time} ({unix_time_with_milliseconds})"
|
| 22 |
+
|
| 23 |
+
# Initializing with a format that includes the logger name, level, and message
|
| 24 |
+
def __init__(self, fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s"):
|
| 25 |
+
super().__init__(fmt)
|
| 26 |
+
|
| 27 |
+
def configure_logging():
|
| 28 |
+
# Configure the root logger
|
| 29 |
+
root_logger = logging.getLogger()
|
| 30 |
+
root_logger.setLevel(IMPORTANT_LEVEL_NUM) # Only log IMPORTANT and above
|
| 31 |
+
|
| 32 |
+
# Clear existing handlers (if re-running this configuration in notebooks, etc.)
|
| 33 |
+
root_logger.handlers = []
|
| 34 |
+
|
| 35 |
+
# Create and set the formatter
|
| 36 |
+
formatter = CustomFormatter()
|
| 37 |
+
|
| 38 |
+
# Create a console handler using the formatter
|
| 39 |
+
console_handler = logging.StreamHandler()
|
| 40 |
+
console_handler.setFormatter(formatter)
|
| 41 |
+
console_handler.setLevel(IMPORTANT_LEVEL_NUM)
|
| 42 |
+
|
| 43 |
+
# Add the console handler to the root logger
|
| 44 |
+
root_logger.addHandler(console_handler)
|
| 45 |
+
|
| 46 |
+
# Disable propagation for all loggers created with getLogger()
|
| 47 |
+
root_logger.propagate = False
|
| 48 |
+
|
| 49 |
+
# Call the function to configure logging
|
| 50 |
+
configure_logging()
|
| 51 |
+
|
| 52 |
+
# Example usage
|
| 53 |
+
logger = logging.getLogger(__name__)
|
| 54 |
+
logger.important("This is an important message.")
|
| 55 |
+
logger.info("This info message should not appear.")
|
| 56 |
+
|
| 57 |
|
|
|
|
|
|
|
| 58 |
|
rasa_socketio_client.py
CHANGED
|
@@ -93,13 +93,16 @@
|
|
| 93 |
|
| 94 |
import asyncio
|
| 95 |
import socketio
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
|
| 100 |
class RasaSocketIOClient:
|
| 101 |
def __init__(self, uri, update_chat_history_callback=None):
|
| 102 |
-
self.sio = socketio.AsyncClient(logger=
|
| 103 |
self.uri = uri
|
| 104 |
self.message_queue = asyncio.Queue()
|
| 105 |
self.register_event_handlers()
|
|
@@ -129,18 +132,21 @@ class RasaSocketIOClient:
|
|
| 129 |
try:
|
| 130 |
await self.sio.emit('user_uttered', {'message': user_message})
|
| 131 |
logger.important(f"Message sent: {user_message}")
|
| 132 |
-
timeout =
|
| 133 |
while True:
|
| 134 |
try:
|
| 135 |
data = await asyncio.wait_for(self.message_queue.get(), timeout)
|
|
|
|
| 136 |
yield data
|
| 137 |
except asyncio.TimeoutError:
|
|
|
|
| 138 |
break
|
| 139 |
except Exception as e:
|
| 140 |
logger.error(f"Failed to send message: {e}")
|
| 141 |
|
| 142 |
async def disconnect(self):
|
| 143 |
try:
|
|
|
|
| 144 |
await self.sio.disconnect()
|
| 145 |
logger.important("Disconnected from the server.")
|
| 146 |
except Exception as e:
|
|
|
|
| 93 |
|
| 94 |
import asyncio
|
| 95 |
import socketio
|
| 96 |
+
import logging
|
| 97 |
+
import custom_logging
|
| 98 |
|
| 99 |
+
|
| 100 |
+
# Obtain a logger for this module
|
| 101 |
+
logger = logging.getLogger(__name__)
|
| 102 |
|
| 103 |
class RasaSocketIOClient:
|
| 104 |
def __init__(self, uri, update_chat_history_callback=None):
|
| 105 |
+
self.sio = socketio.AsyncClient(logger=False, engineio_logger=False)
|
| 106 |
self.uri = uri
|
| 107 |
self.message_queue = asyncio.Queue()
|
| 108 |
self.register_event_handlers()
|
|
|
|
| 132 |
try:
|
| 133 |
await self.sio.emit('user_uttered', {'message': user_message})
|
| 134 |
logger.important(f"Message sent: {user_message}")
|
| 135 |
+
timeout = 5.0 # Timeout in seconds after the last message is received
|
| 136 |
while True:
|
| 137 |
try:
|
| 138 |
data = await asyncio.wait_for(self.message_queue.get(), timeout)
|
| 139 |
+
logger.important(f"Message in queue: {user_message}")
|
| 140 |
yield data
|
| 141 |
except asyncio.TimeoutError:
|
| 142 |
+
logger.important(f"send_message function timed out")
|
| 143 |
break
|
| 144 |
except Exception as e:
|
| 145 |
logger.error(f"Failed to send message: {e}")
|
| 146 |
|
| 147 |
async def disconnect(self):
|
| 148 |
try:
|
| 149 |
+
logger.important(f"Attempting to disconnect: {id(self.sio)}")
|
| 150 |
await self.sio.disconnect()
|
| 151 |
logger.important("Disconnected from the server.")
|
| 152 |
except Exception as e:
|