File size: 7,124 Bytes
b8c0485 | 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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | import gradio as gr
import uuid
from langchain_core.messages import HumanMessage
from memory_chatbot import chatbot
# -----------------------
# Generate IDs
# -----------------------
def new_thread_id():
return "thread_" + str(uuid.uuid4())[:8]
def new_user_id():
return "user_" + str(uuid.uuid4())[:6]
# -----------------------
# Initial User + Thread
# -----------------------
first_user = new_user_id()
first_thread = new_thread_id()
# -----------------------
# Chat Function
# -----------------------
def chat_fn(message, chat_history, thread_id, user_id, all_threads):
# tool_logs = []
config = {
"configurable": {
"thread_id": thread_id,
"user_id": user_id
}
}
res = chatbot.invoke(
{"messages": [HumanMessage(content=message)]},
config=config
)
ai_message = res["messages"][-1].content
chat_history.append({"role": "user", "content": message})
chat_history.append({"role": "assistant", "content": ai_message})
all_threads[(user_id, thread_id)] = chat_history
return "", chat_history, all_threads
# -----------------------
# Create New Thread
# -----------------------
def create_new_thread(user_id, all_threads):
new_tid = new_thread_id()
all_threads[(user_id, new_tid)] = []
return (
all_threads,
gr.update(choices=[t for (u, t) in all_threads if u == user_id], value=new_tid),
new_tid,
[]
)
# -----------------------
# Switch Thread
# -----------------------
def switch_thread(thread_id, user_id, all_threads):
history = all_threads.get((user_id, thread_id), [])
return thread_id, history
# -----------------------
# Create New User
# -----------------------
def create_new_user(all_threads):
new_uid = new_user_id()
new_tid = new_thread_id()
all_threads[(new_uid, new_tid)] = []
return (
all_threads,
gr.update(choices=list(set([u for (u, _) in all_threads])), value=new_uid),
new_uid,
gr.update(choices=[new_tid], value=new_tid),
new_tid,
[]
)
# -----------------------
# Switch User
# -----------------------
def switch_user(user_id, all_threads):
user_threads = [t for (u, t) in all_threads if u == user_id]
if not user_threads:
new_tid = new_thread_id()
all_threads[(user_id, new_tid)] = []
user_threads = [new_tid]
first_thread = user_threads[0]
history = all_threads.get((user_id, first_thread), [])
return (
user_id,
gr.update(choices=user_threads, value=first_thread),
first_thread,
history
)
# -----------------------
# UI
# -----------------------
with gr.Blocks() as demo:
user_state = gr.State(first_user)
thread_state = gr.State(first_thread)
threads_state = gr.State({
(first_user, first_thread): []
})
with gr.Row():
# Sidebar
with gr.Column(scale=1):
gr.Markdown("## 👤 Users")
user_dropdown = gr.Dropdown(
choices=[first_user],
value=first_user,
label="Select User"
)
new_user_btn = gr.Button("➕ New User")
gr.Markdown("## 🧵 Threads")
thread_radio = gr.Radio(
choices=[first_thread],
value=first_thread,
label="Select Thread"
)
new_chat_btn = gr.Button("➕ New Chat")
# Chat Area
with gr.Column(scale=4):
chatbot_ui = gr.Chatbot()
msg_box = gr.Textbox(placeholder="Type message and press Enter")
msg_box.submit(
chat_fn,
inputs=[msg_box, chatbot_ui, thread_state, user_state, threads_state],
outputs=[msg_box, chatbot_ui, threads_state],
)
# -----------------------
# New User
# -----------------------
new_user_btn.click(
create_new_user,
inputs=threads_state,
outputs=[
threads_state,
user_dropdown,
user_state,
thread_radio,
thread_state,
chatbot_ui
]
)
# -----------------------
# Switch User
# -----------------------
user_dropdown.change(
switch_user,
inputs=[user_dropdown, threads_state],
outputs=[
user_state,
thread_radio,
thread_state,
chatbot_ui
]
)
# -----------------------
# New Thread
# -----------------------
new_chat_btn.click(
create_new_thread,
inputs=[user_state, threads_state],
outputs=[
threads_state,
thread_radio,
thread_state,
chatbot_ui
]
)
# -----------------------
# Switch Thread
# -----------------------
thread_radio.change(
switch_thread,
inputs=[thread_radio, user_state, threads_state],
outputs=[thread_state, chatbot_ui]
)
demo.launch()
# import gradio as gr
# import uuid
# from langchain_core.messages import HumanMessage
# from memory_chatbot import chatbot
# # -----------------------
# # Generate IDs
# # -----------------------
# def new_thread_id():
# return "thread_" + str(uuid.uuid4())[:8]
# def new_user_id():
# return "user_" + str(uuid.uuid4())[:6]
# # -----------------------
# # Chat Function
# # -----------------------
# def chat_fn(message, chat_history, thread_id, user_id):
# config = {
# "configurable": {
# "thread_id": thread_id,
# "user_id": user_id
# }
# }
# res = chatbot.invoke(
# {"messages": [HumanMessage(content=message)]},
# config=config
# )
# ai_message = res["messages"][-1].content
# chat_history.append({"role": "user", "content": message})
# chat_history.append({"role": "assistant", "content": ai_message})
# return "", chat_history
# # -----------------------
# # New Chat (Reset Thread)
# # -----------------------
# def new_chat():
# return new_thread_id(), []
# # -----------------------
# # UI
# # -----------------------
# with gr.Blocks() as demo:
# # Hidden states
# user_state = gr.State(new_user_id())
# thread_state = gr.State(new_thread_id())
# gr.Markdown("# 🤖 AI Chatbot")
# chatbot_ui = gr.Chatbot(height=500)
# msg_box = gr.Textbox(placeholder="Type your message...")
# with gr.Row():
# send_btn = gr.Button("Send")
# new_chat_btn = gr.Button("🧹 New Chat")
# # Send message
# send_btn.click(
# chat_fn,
# inputs=[msg_box, chatbot_ui, thread_state, user_state],
# outputs=[msg_box, chatbot_ui],
# )
# msg_box.submit(
# chat_fn,
# inputs=[msg_box, chatbot_ui, thread_state, user_state],
# outputs=[msg_box, chatbot_ui],
# )
# # New chat (reset thread)
# new_chat_btn.click(
# new_chat,
# outputs=[thread_state, chatbot_ui]
# )
# demo.launch() |