Spaces:
Runtime error
Runtime error
Mehul Patel commited on
Commit ·
b13eb29
1
Parent(s): 577a154
socketio is working
Browse files- app.py +124 -24
- custom_logging.py +12 -0
- rasa_socketio_client.py +141 -0
app.py
CHANGED
|
@@ -1,32 +1,132 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
msg.submit(interact_with_rasa, [msg, chatbot], [msg, chatbot])
|
| 29 |
|
| 30 |
if __name__ == "__main__":
|
| 31 |
-
|
|
|
|
| 32 |
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
# import requests
|
| 3 |
|
| 4 |
+
# theme=gr.themes.Default(primary_hue="cyan", secondary_hue="violet")
|
| 5 |
+
|
| 6 |
+
# with gr.Blocks(theme=theme) as demo:
|
| 7 |
+
# chatbot = gr.Chatbot()
|
| 8 |
+
# msg = gr.Textbox(placeholder="Say Hi! to wake me up", show_label=False)
|
| 9 |
+
# clear = gr.ClearButton([msg, chatbot])
|
| 10 |
|
| 11 |
+
# def interact_with_rasa(user_input, chat_history):
|
| 12 |
+
# rasa_url = "https://clairify.ai/webhooks/rest/webhook"
|
| 13 |
+
# payload = {"sender": "user", "message": user_input}
|
| 14 |
+
# response = requests.post(rasa_url, json=payload)
|
| 15 |
|
| 16 |
+
# if response.status_code == 200:
|
| 17 |
+
# rasa_response = response.json()
|
| 18 |
+
# if rasa_response and len(rasa_response) > 0:
|
| 19 |
+
# bot_responses = [msg.get("text", "") for msg in rasa_response]
|
| 20 |
+
# chat_history.append((user_input, bot_responses[0]))
|
| 21 |
+
# for response in bot_responses[1:]:
|
| 22 |
+
# chat_history.append((None, response))
|
| 23 |
+
# else:
|
| 24 |
+
# bot_responses = ["Error: Empty response from Rasa server."]
|
| 25 |
+
# else:
|
| 26 |
+
# bot_responses = ["Error: Unable to connect to Rasa server."]
|
| 27 |
|
| 28 |
+
# return "", chat_history
|
| 29 |
+
|
| 30 |
+
# msg.submit(interact_with_rasa, [msg, chatbot], [msg, chatbot])
|
| 31 |
+
|
| 32 |
+
# if __name__ == "__main__":
|
| 33 |
+
# demo.launch()
|
| 34 |
+
|
| 35 |
+
# import gradio as gr
|
| 36 |
+
# import asyncio
|
| 37 |
+
# from rasa_socketio_client import RasaSocketIOClient
|
| 38 |
+
# import logging
|
| 39 |
+
# from custom_logging import IMPORTANT_LEVEL_NUM
|
| 40 |
+
|
| 41 |
+
# # Enable logging for the whole app
|
| 42 |
+
# logging.basicConfig(level=IMPORTANT_LEVEL_NUM)
|
| 43 |
+
# logger = logging.getLogger(__name__)
|
| 44 |
+
|
| 45 |
+
# rasa_io_url = "https://clairify.ai/socket.io"
|
| 46 |
+
# rasa_client = RasaSocketIOClient(rasa_io_url)
|
| 47 |
+
|
| 48 |
+
# theme = gr.themes.Default(primary_hue="cyan", secondary_hue="violet")
|
| 49 |
+
|
| 50 |
+
# async def interact_with_rasa(user_input, chat_history):
|
| 51 |
+
# if not rasa_client.sio.connected:
|
| 52 |
+
# await rasa_client.connect()
|
| 53 |
+
# payload = {"message": user_input}
|
| 54 |
+
# rasa_response = await rasa_client.send_message(payload)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# # if rasa_response:
|
| 58 |
+
# # bot_utterances = []
|
| 59 |
+
# # for key, value in rasa_response.items():
|
| 60 |
+
# # bot_utterance = value.get("text", "") # Accessing the "text" key of each value
|
| 61 |
+
# # bot_utterances.append(bot_utterance)
|
| 62 |
+
|
| 63 |
+
# # if bot_utterances:
|
| 64 |
+
# # chat_history.append((user_input, bot_utterances[0]))
|
| 65 |
+
# # for utterance in bot_utterances[1:]:
|
| 66 |
+
# # chat_history.append((None, utterance))
|
| 67 |
+
# # else:
|
| 68 |
+
# # chat_history.append((user_input, "Error: Empty response from Rasa server."))
|
| 69 |
+
# chat_history.append((user_input, rasa_response["text"]))
|
| 70 |
+
# return "", chat_history
|
| 71 |
+
|
| 72 |
+
# with gr.Blocks(theme=theme) as demo:
|
| 73 |
+
# chatbot = gr.Chatbot()
|
| 74 |
+
# msg = gr.Textbox(placeholder="Say Hi! to wake me up", show_label=False)
|
| 75 |
+
# clear = gr.ClearButton([msg, chatbot])
|
| 76 |
+
|
| 77 |
+
# msg.submit(interact_with_rasa, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 78 |
+
|
| 79 |
+
# if __name__ == "__main__":
|
| 80 |
+
# asyncio.run(demo.launch())
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
import gradio as gr
|
| 84 |
+
import asyncio
|
| 85 |
+
from rasa_socketio_client import RasaSocketIOClient
|
| 86 |
+
import logging
|
| 87 |
+
from custom_logging import IMPORTANT_LEVEL_NUM
|
| 88 |
+
|
| 89 |
+
# Assuming custom_logging.py defines the IMPORTANT_LEVEL_NUM and extends the Logger class
|
| 90 |
+
# Enable logging for the whole app
|
| 91 |
+
logging.basicConfig(level=IMPORTANT_LEVEL_NUM, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s')
|
| 92 |
+
logger = logging.getLogger(__name__)
|
| 93 |
+
|
| 94 |
+
rasa_io_url = "https://clairify.ai/socket.io"
|
| 95 |
+
rasa_client = RasaSocketIOClient(rasa_io_url)
|
| 96 |
+
|
| 97 |
+
theme = gr.themes.Default(primary_hue="cyan", secondary_hue="violet")
|
| 98 |
+
|
| 99 |
+
async def interact_with_rasa(user_input, chat_history):
|
| 100 |
+
logger.important(f"User input received: {user_input}")
|
| 101 |
+
if not rasa_client.sio.connected:
|
| 102 |
+
logger.important("Rasa client not connected, attempting to connect...")
|
| 103 |
+
await rasa_client.connect()
|
| 104 |
+
if rasa_client.sio.connected:
|
| 105 |
+
logger.important("Rasa client successfully connected.")
|
| 106 |
+
else:
|
| 107 |
+
logger.important("Failed to connect to Rasa client.")
|
| 108 |
+
return "Error connecting to chat service, please try again later.", chat_history
|
| 109 |
+
|
| 110 |
+
payload = {"message": user_input}
|
| 111 |
+
logger.important(f"Sending payload to Rasa: {payload}")
|
| 112 |
+
rasa_response = await rasa_client.send_message(payload)
|
| 113 |
+
|
| 114 |
+
if rasa_response is None or "text" not in rasa_response:
|
| 115 |
+
logger.important(f"Invalid or None response from Rasa: {rasa_response}")
|
| 116 |
+
return "Received an invalid response from the chat service.", chat_history
|
| 117 |
+
|
| 118 |
+
logger.important(f"Rasa response: {rasa_response}")
|
| 119 |
+
chat_history.append((user_input, rasa_response["text"]))
|
| 120 |
+
return "", chat_history
|
| 121 |
+
|
| 122 |
+
with gr.Blocks(theme=theme) as demo:
|
| 123 |
+
chatbot = gr.Chatbot()
|
| 124 |
+
msg = gr.Textbox(placeholder="Say Hi! to wake me up", show_label=False)
|
| 125 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 126 |
|
| 127 |
+
msg.submit(interact_with_rasa, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
+
logger.important("Launching Gradio demo...")
|
| 131 |
+
asyncio.run(demo.launch())
|
| 132 |
|
custom_logging.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
|
| 3 |
+
# Step 1: Define a new logging level
|
| 4 |
+
IMPORTANT_LEVEL_NUM = 25
|
| 5 |
+
logging.addLevelName(IMPORTANT_LEVEL_NUM, "IMPORTANT")
|
| 6 |
+
|
| 7 |
+
def important(self, message, *args, **kws):
|
| 8 |
+
if self.isEnabledFor(IMPORTANT_LEVEL_NUM):
|
| 9 |
+
self._log(IMPORTANT_LEVEL_NUM, message, args, **kws)
|
| 10 |
+
|
| 11 |
+
# Add the custom level to the logging.Logger class
|
| 12 |
+
logging.Logger.important = important
|
rasa_socketio_client.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import asyncio
|
| 2 |
+
# import socketio
|
| 3 |
+
# import logging
|
| 4 |
+
|
| 5 |
+
# # Configure logging
|
| 6 |
+
# logging.basicConfig(level=logging.DEBUG)
|
| 7 |
+
# logger = logging.getLogger(__name__)
|
| 8 |
+
|
| 9 |
+
# class RasaSocketIOClient:
|
| 10 |
+
# def __init__(self, uri):
|
| 11 |
+
# self.sio = socketio.AsyncClient(logger=True, engineio_logger=True) # Enable internal logging
|
| 12 |
+
# self.uri = uri
|
| 13 |
+
# self.response = None
|
| 14 |
+
|
| 15 |
+
# @self.sio.on('bot_uttered') # Adjust the event name as per your Rasa setup
|
| 16 |
+
# async def on_message(data):
|
| 17 |
+
# logger.info("Received response: %s", data)
|
| 18 |
+
# self.response = data # Store the latest response
|
| 19 |
+
|
| 20 |
+
# async def connect(self):
|
| 21 |
+
# try:
|
| 22 |
+
# await self.sio.connect(self.uri)
|
| 23 |
+
# logger.info("Successfully connected to the server.")
|
| 24 |
+
# except Exception as e:
|
| 25 |
+
# logger.error("Failed to connect to the server: %s", e)
|
| 26 |
+
|
| 27 |
+
# async def send_message(self, message):
|
| 28 |
+
# self.response = None # Reset previous response
|
| 29 |
+
# try:
|
| 30 |
+
# await self.sio.emit('user_uttered', message) # Adjust the event name as per your Rasa setup
|
| 31 |
+
# logger.debug("Message sent: %s", message)
|
| 32 |
+
# await asyncio.sleep(1) # Wait for the response to be received
|
| 33 |
+
# except Exception as e:
|
| 34 |
+
# logger.error("Failed to send message: %s", e)
|
| 35 |
+
# return self.response
|
| 36 |
+
|
| 37 |
+
# async def disconnect(self):
|
| 38 |
+
# try:
|
| 39 |
+
# await self.sio.disconnect()
|
| 40 |
+
# logger.info("Disconnected from the server.")
|
| 41 |
+
# except Exception as e:
|
| 42 |
+
# logger.error("Failed to disconnect: %s", e)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# import asyncio
|
| 46 |
+
# import socketio
|
| 47 |
+
# import logging
|
| 48 |
+
# from custom_logging import IMPORTANT_LEVEL_NUM
|
| 49 |
+
|
| 50 |
+
# logger = logging.getLogger(__name__)
|
| 51 |
+
|
| 52 |
+
# class RasaSocketIOClient:
|
| 53 |
+
# def __init__(self, uri):
|
| 54 |
+
# self.sio = socketio.AsyncClient(logger=False, engineio_logger=False)
|
| 55 |
+
# self.response_event = asyncio.Event()
|
| 56 |
+
# self.uri = uri
|
| 57 |
+
# self.response = None
|
| 58 |
+
|
| 59 |
+
# @self.sio.on('bot_uttered') # Adjust the event name as per your Rasa setup
|
| 60 |
+
# async def on_message(data):
|
| 61 |
+
# # Use the new logging level
|
| 62 |
+
# logger.important("Socket ID: %s Received response: %s", self.sio.sid, data)
|
| 63 |
+
# self.response = data # Store the latest response
|
| 64 |
+
|
| 65 |
+
# async def connect(self):
|
| 66 |
+
# try:
|
| 67 |
+
# await self.sio.connect(self.uri)
|
| 68 |
+
# # Use the new logging level
|
| 69 |
+
# logger.important("Successfully connected to the server.")
|
| 70 |
+
# except Exception as e:
|
| 71 |
+
# # Use the new logging level for errors too, for consistency
|
| 72 |
+
# logger.important("Failed to connect to the server: %s", e)
|
| 73 |
+
# return "Failed to connect to the server."
|
| 74 |
+
|
| 75 |
+
# async def send_message(self, message):
|
| 76 |
+
# self.response = None # Reset previous response
|
| 77 |
+
# try:
|
| 78 |
+
# await self.sio.emit('user_uttered', message) # Adjust the event name as per your Rasa setup
|
| 79 |
+
# # Since this is more of a debug level, consider if you want to keep it under IMPORTANT or not
|
| 80 |
+
# logger.important("Message sent: %s", message)
|
| 81 |
+
# await asyncio.sleep(1) # Wait for the response to be received
|
| 82 |
+
# except Exception as e:
|
| 83 |
+
# logger.important("Failed to send message: %s", e)
|
| 84 |
+
# return self.response
|
| 85 |
+
|
| 86 |
+
# async def disconnect(self):
|
| 87 |
+
# try:
|
| 88 |
+
# await self.sio.disconnect()
|
| 89 |
+
# logger.important("Disconnected from the server.")
|
| 90 |
+
# except Exception as e:
|
| 91 |
+
# logger.important("Failed to disconnect: %s", e)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
import asyncio
|
| 95 |
+
import socketio
|
| 96 |
+
import logging
|
| 97 |
+
from custom_logging import IMPORTANT_LEVEL_NUM
|
| 98 |
+
|
| 99 |
+
# Configure logging
|
| 100 |
+
logging.basicConfig(level=IMPORTANT_LEVEL_NUM)
|
| 101 |
+
logger = logging.getLogger(__name__)
|
| 102 |
+
|
| 103 |
+
class RasaSocketIOClient:
|
| 104 |
+
def __init__(self, uri):
|
| 105 |
+
self.sio = socketio.AsyncClient(logger=False, engineio_logger=False)
|
| 106 |
+
self.response_event = asyncio.Event()
|
| 107 |
+
self.uri = uri
|
| 108 |
+
self.response = None
|
| 109 |
+
|
| 110 |
+
@self.sio.on('bot_uttered') # Adjust the event name as per your Rasa setup
|
| 111 |
+
async def on_message(data):
|
| 112 |
+
# Use the new logging level
|
| 113 |
+
logger.important("Socket ID: %s Received response: %s", self.sio.sid, data)
|
| 114 |
+
self.response = data # Store the latest response
|
| 115 |
+
self.response_event.set() # Signal that response has been received
|
| 116 |
+
|
| 117 |
+
async def connect(self):
|
| 118 |
+
try:
|
| 119 |
+
await self.sio.connect(self.uri)
|
| 120 |
+
logger.important("Successfully connected to the server.")
|
| 121 |
+
except Exception as e:
|
| 122 |
+
logger.important("Failed to connect to the server: %s", e)
|
| 123 |
+
return "Failed to connect to the server."
|
| 124 |
+
|
| 125 |
+
async def send_message(self, message):
|
| 126 |
+
self.response = None # Reset previous response
|
| 127 |
+
self.response_event.clear() # Reset the event for a new message
|
| 128 |
+
try:
|
| 129 |
+
await self.sio.emit('user_uttered', message) # Adjust the event name as per your Rasa setup
|
| 130 |
+
logger.important("Message sent: %s", message)
|
| 131 |
+
await self.response_event.wait() # Wait here until the response has been received
|
| 132 |
+
except Exception as e:
|
| 133 |
+
logger.important("Failed to send message: %s", e)
|
| 134 |
+
return self.response
|
| 135 |
+
|
| 136 |
+
async def disconnect(self):
|
| 137 |
+
try:
|
| 138 |
+
await self.sio.disconnect()
|
| 139 |
+
logger.important("Disconnected from the server.")
|
| 140 |
+
except Exception as e:
|
| 141 |
+
logger.important("Failed to disconnect: %s", e)
|