IProject-10 commited on
Commit
58ebc0c
Β·
verified Β·
1 Parent(s): 306a4b3

Update app.py - Gradio UI Improvements

Browse files
Files changed (1) hide show
  1. app.py +76 -7
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
- 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(placeholder="Ask your question here...", show_label=False, scale=8)
 
 
 
 
110
  send_btn = gr.Button("πŸ“¨ Send", scale=1)
111
- msg.submit(chat_interface, inputs=[msg, state], outputs=[chatbot, state])
112
- send_btn.click(chat_interface, inputs=[msg, state], outputs=[chatbot, state])
 
 
 
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()