Spaces:
Running
Running
Update app.py - Gradio UI Improvements
Browse files
app.py
CHANGED
@@ -82,37 +82,106 @@ def get_references(query):
|
|
82 |
return retrieve_with_metadata(query)["references"]
|
83 |
|
84 |
# Gradio UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
def chat_interface(message, history):
|
86 |
history = history or []
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
try:
|
|
|
|
|
|
|
|
|
89 |
answer = rag_chain.invoke(message)
|
90 |
references = get_references(message)
|
|
|
91 |
if references:
|
92 |
ref = references[0]
|
93 |
ref_string = f"\n\nπ **Reference:**\nSection: {ref['section']}\nURL: {ref['source']}"
|
94 |
else:
|
95 |
ref_string = "\n\nπ **Reference:**\n_None available_"
|
|
|
96 |
full_response = answer + ref_string
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
except Exception as e:
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
def launch_gradio():
|
103 |
with gr.Blocks() as demo:
|
104 |
gr.Markdown("# π¬ ImageOnline RAG Chatbot")
|
105 |
gr.Markdown("Ask about Website Designing, Web Development, App Development, About Us, Testimonials etc.")
|
|
|
106 |
chatbot = gr.Chatbot()
|
107 |
state = gr.State([])
|
|
|
108 |
with gr.Row():
|
109 |
-
msg = gr.Textbox(
|
|
|
|
|
|
|
|
|
110 |
send_btn = gr.Button("π¨ Send", scale=1)
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
113 |
with gr.Row():
|
114 |
clear_btn = gr.Button("π§Ή Clear Chat")
|
115 |
clear_btn.click(fn=lambda: ([], []), outputs=[chatbot, state])
|
|
|
116 |
return demo
|
117 |
|
118 |
demo = launch_gradio()
|
|
|
82 |
return retrieve_with_metadata(query)["references"]
|
83 |
|
84 |
# Gradio UI
|
85 |
+
# def chat_interface(message, history):
|
86 |
+
# history = history or []
|
87 |
+
# history.append(("π§ You: " + message, "β³ Generating response..."))
|
88 |
+
# try:
|
89 |
+
# answer = rag_chain.invoke(message)
|
90 |
+
# references = get_references(message)
|
91 |
+
# if references:
|
92 |
+
# ref = references[0]
|
93 |
+
# ref_string = f"\n\nπ **Reference:**\nSection: {ref['section']}\nURL: {ref['source']}"
|
94 |
+
# else:
|
95 |
+
# ref_string = "\n\nπ **Reference:**\n_None available_"
|
96 |
+
# full_response = answer + ref_string
|
97 |
+
# history[-1] = ("π§ You: " + message, "π€ Bot: " + full_response)
|
98 |
+
# except Exception as e:
|
99 |
+
# history[-1] = ("π§ You: " + message, f"π€ Bot: β οΈ {str(e)}")
|
100 |
+
# return history, history
|
101 |
+
from datetime import datetime
|
102 |
+
import time
|
103 |
+
|
104 |
def chat_interface(message, history):
|
105 |
history = history or []
|
106 |
+
|
107 |
+
# π Timestamp for user
|
108 |
+
timestamp = datetime.now().strftime("%H:%M:%S")
|
109 |
+
user_msg = f"π§ **You**\n{message}\n\n<span style='font-size: 0.8em; color: gray;'>β±οΈ {timestamp}</span>"
|
110 |
+
|
111 |
+
# β³ Show typing indicator
|
112 |
+
bot_msg = "β³ _Bot is typing..._"
|
113 |
+
history.append((user_msg, bot_msg))
|
114 |
+
|
115 |
try:
|
116 |
+
# π¬ Optional: simulate typing delay (cosmetic only)
|
117 |
+
time.sleep(0.5)
|
118 |
+
|
119 |
+
# RAG response generation
|
120 |
answer = rag_chain.invoke(message)
|
121 |
references = get_references(message)
|
122 |
+
|
123 |
if references:
|
124 |
ref = references[0]
|
125 |
ref_string = f"\n\nπ **Reference:**\nSection: {ref['section']}\nURL: {ref['source']}"
|
126 |
else:
|
127 |
ref_string = "\n\nπ **Reference:**\n_None available_"
|
128 |
+
|
129 |
full_response = answer + ref_string
|
130 |
+
|
131 |
+
# π Timestamp for bot
|
132 |
+
timestamp_bot = datetime.now().strftime("%H:%M:%S")
|
133 |
+
bot_response = f"π€ **Bot**\n{full_response}\n\n<span style='font-size: 0.8em; color: gray;'>β±οΈ {timestamp_bot}</span>"
|
134 |
+
|
135 |
+
# Replace typing placeholder
|
136 |
+
history[-1] = (user_msg, bot_response)
|
137 |
+
|
138 |
except Exception as e:
|
139 |
+
timestamp_bot = datetime.now().strftime("%H:%M:%S")
|
140 |
+
error_msg = f"π€ **Bot**\nβ οΈ {str(e)}\n\n<span style='font-size: 0.8em; color: gray;'>β±οΈ {timestamp_bot}</span>"
|
141 |
+
history[-1] = (user_msg, error_msg)
|
142 |
+
|
143 |
+
return history, history, "" # clear input box
|
144 |
|
145 |
+
|
146 |
+
# def launch_gradio():
|
147 |
+
# with gr.Blocks() as demo:
|
148 |
+
# gr.Markdown("# π¬ ImageOnline RAG Chatbot")
|
149 |
+
# gr.Markdown("Ask about Website Designing, Web Development, App Development, About Us, Testimonials etc.")
|
150 |
+
# chatbot = gr.Chatbot()
|
151 |
+
# state = gr.State([])
|
152 |
+
# with gr.Row():
|
153 |
+
# msg = gr.Textbox(placeholder="Ask your question here...", show_label=False, scale=8)
|
154 |
+
# send_btn = gr.Button("π¨ Send", scale=1)
|
155 |
+
# msg.submit(chat_interface, inputs=[msg, state], outputs=[chatbot, state])
|
156 |
+
# send_btn.click(chat_interface, inputs=[msg, state], outputs=[chatbot, state])
|
157 |
+
# with gr.Row():
|
158 |
+
# clear_btn = gr.Button("π§Ή Clear Chat")
|
159 |
+
# clear_btn.click(fn=lambda: ([], []), outputs=[chatbot, state])
|
160 |
+
# return demo
|
161 |
def launch_gradio():
|
162 |
with gr.Blocks() as demo:
|
163 |
gr.Markdown("# π¬ ImageOnline RAG Chatbot")
|
164 |
gr.Markdown("Ask about Website Designing, Web Development, App Development, About Us, Testimonials etc.")
|
165 |
+
|
166 |
chatbot = gr.Chatbot()
|
167 |
state = gr.State([])
|
168 |
+
|
169 |
with gr.Row():
|
170 |
+
msg = gr.Textbox(
|
171 |
+
placeholder="Ask your question here...",
|
172 |
+
show_label=False,
|
173 |
+
scale=8
|
174 |
+
)
|
175 |
send_btn = gr.Button("π¨ Send", scale=1)
|
176 |
+
|
177 |
+
# π Trigger chat and clear input
|
178 |
+
msg.submit(chat_interface, inputs=[msg, state], outputs=[chatbot, state, msg])
|
179 |
+
send_btn.click(chat_interface, inputs=[msg, state], outputs=[chatbot, state, msg])
|
180 |
+
|
181 |
with gr.Row():
|
182 |
clear_btn = gr.Button("π§Ή Clear Chat")
|
183 |
clear_btn.click(fn=lambda: ([], []), outputs=[chatbot, state])
|
184 |
+
|
185 |
return demo
|
186 |
|
187 |
demo = launch_gradio()
|