SBK commited on
Commit
f7ddeab
·
verified ·
1 Parent(s): 4483fdb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -58,32 +58,39 @@ def generate_response(history, system_prompt):
58
  thread.start()
59
 
60
  # Stream tokens
 
61
  for token in streamer:
62
- yield token
 
63
 
64
  # === Gradio interface ===
65
  with gr.Blocks() as demo:
66
  gr.Markdown("## 🧠 Chat with SBK LLM")
67
  system_prompt = gr.Textbox(label="System Prompt", value=SYSTEM_PROMPT, lines=2)
68
- chatbot = gr.Chatbot() # Reverted to original format
69
  msg = gr.Textbox(label="Your Message", placeholder="Ask me anything...", lines=1)
70
  clear = gr.Button("Clear")
71
 
72
  history = gr.State([]) # memory for session
73
 
74
- def respond(user_message, history, system_prompt):
75
- history = history + [[user_message, ""]]
76
- return "", history, generate_response(history, system_prompt)
 
 
 
 
 
 
 
 
 
77
 
78
- # Updated submit handler
79
  msg.submit(
80
  respond,
81
- [msg, history, system_prompt],
82
- [msg, history, chatbot]
83
  )
84
- clear.click(lambda: ([], "", ""), outputs=[chatbot, msg, history])
85
 
86
- # Enable streaming through queue
87
- demo.queue()
88
-
89
- demo.launch()
 
58
  thread.start()
59
 
60
  # Stream tokens
61
+ partial_message = ""
62
  for token in streamer:
63
+ partial_message += token
64
+ yield partial_message
65
 
66
  # === Gradio interface ===
67
  with gr.Blocks() as demo:
68
  gr.Markdown("## 🧠 Chat with SBK LLM")
69
  system_prompt = gr.Textbox(label="System Prompt", value=SYSTEM_PROMPT, lines=2)
70
+ chatbot = gr.Chatbot() # Using default tuple format
71
  msg = gr.Textbox(label="Your Message", placeholder="Ask me anything...", lines=1)
72
  clear = gr.Button("Clear")
73
 
74
  history = gr.State([]) # memory for session
75
 
76
+ def respond(user_message, chat_history, system_prompt):
77
+ # Append user message to history
78
+ chat_history.append((user_message, ""))
79
+
80
+ # Generate response
81
+ full_response = ""
82
+ for response in generate_response(chat_history, system_prompt):
83
+ full_response = response
84
+ chat_history[-1] = (user_message, full_response)
85
+ yield chat_history
86
+
87
+ return "", chat_history
88
 
 
89
  msg.submit(
90
  respond,
91
+ [msg, chatbot, system_prompt],
92
+ [chatbot, history],
93
  )
94
+ clear.click(lambda: ([], []), outputs=[chatbot, history])
95
 
96
+ demo.queue().launch()