11import gradio as gr
22import websockets
3- import json
4- import asyncio
5- import logging
63from typing import Tuple , List , Optional , Dict , Any
74from loguru import logger
85
1512guardrails_model = GuardRails ()
1613
1714
18- async def search_click (msg , history ):
15+ async def search_click (msg : str , history : List [Tuple [str , str ]]) -> Tuple [str , List [Tuple [str , str ]], gr .Info ]:
16+
17+ if not msg .strip ():
18+ logger .error (f"No input provided" )
19+ return "" , history , gr .Warning ("Please enter a query." )
1920
2021 response = int (guardrails_model .classify_prompt (msg ))
2122
2223 if response == 0 :
23- return await ws_client .handle_request (
24+ result = await ws_client .handle_request (
2425 "search" ,
2526 {"query" : msg , "history" : history if history else []}
2627 )
28+ if result [2 ] == "right" :
29+
30+ styled_response = (f"<div style='direction: rtl; text-align: right; direction: right;'>{ result [1 ]} </div>" )
31+ else :
32+ styled_response = f"<div style='direction: ltr; text-align: left; direction: left;'>{ result [1 ]} </div>"
33+
34+ # Append the styled response to the chat history
35+ updated_history = history + [(msg , styled_response )]
36+
37+
38+ return result [0 ], updated_history , gr .Info ("Query Processed" )
39+
2740 else :
2841 return await return_protection_message (msg , history )
2942
3043
3144async def return_protection_message (msg , history ):
3245
33- new_message = (msg , "Your query appears a prompt injection. I would prefer Not to answer it. " )
46+ new_message = (msg , "Your query appears inappropriate. Do you have any other question?I am here to help.. " )
3447 updated_history = history + [new_message ]
35- return "" , updated_history
48+ return "" , updated_history , gr .Warning ("Query is Inapproprite.." )
49+
3650
3751
3852async def handle_ingest () -> gr .Info :
@@ -74,6 +88,11 @@ async def record_feedback(feedback, msg ) -> gr.Info:
7488 logger .info (feedback )
7589 logger .info (msg )
7690
91+
92+ if not msg .strip ():
93+ logger .error (f"No Comments provided" )
94+ return gr .Info ("Please Enter Some Feed back First" ), ""
95+
7796 message , _ = await ws_client .handle_request (feedback , {"comment" : msg })
7897 return gr .Info (message ) if "success" in message .lower () else gr .Warning (message ), ""
7998
@@ -107,7 +126,7 @@ async def record_feedback(feedback, msg ) -> gr.Info:
107126 margin-top: 0.25rem;
108127 flex: 0 0 auto;
109128 }
110- #chatbot {
129+ #chatbot-left {
111130 border: 1px solid #E5E7EB;
112131 border-radius: 8px;
113132 background-color: #FFFFFF;
@@ -118,6 +137,24 @@ async def record_feedback(feedback, msg ) -> gr.Info:
118137 flex-direction: column;
119138 overflow-y: auto; /* To allow scrolling if content overflows */
120139 min-height: 62vh;
140+ text-direction: left;
141+ direction: left;
142+ text-align: left;
143+ }
144+ #chatbot-right {
145+ border: 1px solid #E5E7EB;
146+ border-radius: 8px;
147+ background-color: #FFFFFF;
148+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
149+ flex: 1 1 auto;
150+ min-height: 0;
151+ display: flex;
152+ flex-direction: column;
153+ overflow-y: auto; /* To allow scrolling if content overflows */
154+ min-height: 62vh;
155+ text-direction: right;
156+ direction: right;
157+ text-align: right;
121158 }
122159 #feedback-button {
123160 max-width: 0.25vh;
@@ -141,7 +178,7 @@ async def record_feedback(feedback, msg ) -> gr.Info:
141178 chatbot = gr .Chatbot (
142179 show_label = False ,
143180 container = True ,
144- elem_id = "chatbot"
181+ elem_id = "chatbot-left "
145182 )
146183
147184 with gr .Row (elem_id = "feedback-container" ):
@@ -173,7 +210,7 @@ async def record_feedback(feedback, msg ) -> gr.Info:
173210 send_button .click (
174211 fn = search_click ,
175212 inputs = [msg , chatbot ],
176- outputs = [msg , chatbot ]
213+ outputs = [msg , chatbot , status_box ]
177214 )
178215 clear_button .click (
179216 fn = clear_chat ,
@@ -203,4 +240,3 @@ async def record_feedback(feedback, msg ) -> gr.Info:
203240 share = False ,
204241 debug = True ,
205242 show_error = True ,)
206-
0 commit comments