second-space / app.py
Mehul Patel
updated rasa client url
c67297b
Raw
History Blame Contribute Delete
4.92 kB
# import gradio as gr
# import requests
# theme=gr.themes.Default(primary_hue="cyan", secondary_hue="violet")
# with gr.Blocks(theme=theme) as demo:
# chatbot = gr.Chatbot()
# msg = gr.Textbox(placeholder="Say Hi! to wake me up", show_label=False)
# clear = gr.ClearButton([msg, chatbot])
# def interact_with_rasa(user_input, chat_history):
# rasa_url = "https://clairify.ai/webhooks/rest/webhook"
# payload = {"sender": "user", "message": user_input}
# response = requests.post(rasa_url, json=payload)
# if response.status_code == 200:
# rasa_response = response.json()
# if rasa_response and len(rasa_response) > 0:
# bot_responses = [msg.get("text", "") for msg in rasa_response]
# chat_history.append((user_input, bot_responses[0]))
# for response in bot_responses[1:]:
# chat_history.append((None, response))
# else:
# bot_responses = ["Error: Empty response from Rasa server."]
# else:
# bot_responses = ["Error: Unable to connect to Rasa server."]
# return "", chat_history
# msg.submit(interact_with_rasa, [msg, chatbot], [msg, chatbot])
# if __name__ == "__main__":
# demo.launch()
# import gradio as gr
# import asyncio
# from rasa_socketio_client import RasaSocketIOClient
# import logging
# from custom_logging import IMPORTANT_LEVEL_NUM
# # Enable logging for the whole app
# logging.basicConfig(level=IMPORTANT_LEVEL_NUM)
# logger = logging.getLogger(__name__)
# rasa_io_url = "https://clairify.ai/socket.io"
# rasa_client = RasaSocketIOClient(rasa_io_url)
# theme = gr.themes.Default(primary_hue="cyan", secondary_hue="violet")
# async def interact_with_rasa(user_input, chat_history):
# if not rasa_client.sio.connected:
# await rasa_client.connect()
# payload = {"message": user_input}
# rasa_response = await rasa_client.send_message(payload)
# # if rasa_response:
# # bot_utterances = []
# # for key, value in rasa_response.items():
# # bot_utterance = value.get("text", "") # Accessing the "text" key of each value
# # bot_utterances.append(bot_utterance)
# # if bot_utterances:
# # chat_history.append((user_input, bot_utterances[0]))
# # for utterance in bot_utterances[1:]:
# # chat_history.append((None, utterance))
# # else:
# # chat_history.append((user_input, "Error: Empty response from Rasa server."))
# chat_history.append((user_input, rasa_response["text"]))
# return "", chat_history
# with gr.Blocks(theme=theme) as demo:
# chatbot = gr.Chatbot()
# msg = gr.Textbox(placeholder="Say Hi! to wake me up", show_label=False)
# clear = gr.ClearButton([msg, chatbot])
# msg.submit(interact_with_rasa, inputs=[msg, chatbot], outputs=[msg, chatbot])
# if __name__ == "__main__":
# asyncio.run(demo.launch())
import gradio as gr
import asyncio
from rasa_socketio_client import RasaSocketIOClient
import logging
import custom_logging
# Obtain a logger for this module
logger = logging.getLogger("gradio_app")
logger.important(f"Logger handlers: {logger.handlers}")
rasa_io_url = "https://chat.clairify.ai/socket.io"
theme = gr.themes.Default(primary_hue="cyan", secondary_hue="violet")
with gr.Blocks(theme=theme) as demo:
rasa_client = RasaSocketIOClient(rasa_io_url)
async def interact_with_rasa(user_input, chat_history):
logger.important(f"User input received: {user_input}")
if not rasa_client.sio.connected:
logger.important("Rasa client not connected, attempting to connect...")
await rasa_client.connect()
logger.important(f"Sending payload to Rasa: {user_input}")
is_first_message = True
async for message in rasa_client.send_message(user_input):
logger.important(f"Processing messages: {message}")
if is_first_message:
new_entry = (user_input, message.get('text', ''))
is_first_message = False
else:
new_entry = (None, message.get('text', ''))
chat_history.append(new_entry)
logger.important(f"Latest chat history: {chat_history}")
yield "", chat_history
async def handle_clear_button():
await rasa_client.disconnect()
return "", None
chatbot = gr.Chatbot(label="Conversation")
msg = gr.Textbox(placeholder="Say Hi! to wake me up", show_label=False)
clear = gr.Button("Clear")
clear.click(handle_clear_button, inputs=[], outputs=[msg, chatbot])
msg.submit(interact_with_rasa, inputs=[msg, chatbot], outputs=[msg, chatbot])
if __name__ == "__main__":
logger.important("Launching Gradio demo...")
asyncio.run(demo.launch())