File size: 4,919 Bytes
b13eb29
 
d1ddaff
b13eb29
 
 
 
 
 
577a154
b13eb29
 
 
 
577a154
b13eb29
 
 
 
 
 
 
 
 
 
 
577a154
b13eb29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5487a42
 
 
 
 
 
b13eb29
c67297b
b13eb29
 
 
71a0008
39bcecc
 
454bd37
71a0008
 
 
 
 
39bcecc
 
 
 
 
 
 
 
454bd37
5487a42
454bd37
71a0008
454bd37
 
 
 
71a0008
b13eb29
454bd37
 
 
48f1f36
577a154
b13eb29
 
1
2
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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())