MaxLSB commited on
Commit
603f014
·
verified ·
1 Parent(s): 000eb94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -33
app.py CHANGED
@@ -48,8 +48,11 @@ def respond(message, max_tokens, temperature, top_p):
48
  thread = threading.Thread(target=run)
49
  thread.start()
50
 
 
51
  for new_text in streamer:
52
- yield new_text
 
 
53
 
54
  # User input handler
55
  def user(message, chat_history):
@@ -57,18 +60,12 @@ def user(message, chat_history):
57
  return "", chat_history
58
 
59
  # Bot response handler
60
- def bot(chat_history, max_tokens, temperature, top_p):
61
- # Insert model name bubble and placeholder for output
62
- chat_history.append([None, f"**{current_model_name}**"])
63
- chat_history.append([None, ""])
64
- # Render model name immediately
65
- yield chat_history
66
-
67
- # Stream generation into the last bubble
68
- message = chat_history[-3][0] # Original user message
69
- for chunk in respond(message, max_tokens, temperature, top_p):
70
- chat_history[-1][1] += chunk
71
- yield chat_history
72
 
73
  # Model selector handler
74
  def update_model(model_name):
@@ -76,7 +73,7 @@ def update_model(model_name):
76
  return []
77
 
78
  # Gradio UI
79
- with gr.Blocks(css=".gr-chatbot .message { margin: 2px 0 !important; }", title=$1) as demo:
80
  with gr.Row():
81
  gr.HTML("""
82
  <div style="text-align: center; width: 100%;">
@@ -85,7 +82,6 @@ with gr.Blocks(css=".gr-chatbot .message { margin: 2px 0 !important; }", title=$
85
  """ )
86
 
87
  with gr.Row():
88
- # Options column
89
  with gr.Column(scale=1, min_width=150):
90
  model_selector = gr.Dropdown(
91
  choices=["LeCarnet-3M", "LeCarnet-8M", "LeCarnet-21M"],
@@ -97,7 +93,6 @@ with gr.Blocks(css=".gr-chatbot .message { margin: 2px 0 !important; }", title=$
97
  top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p Sampling")
98
  clear_button = gr.Button("Clear Chat")
99
 
100
- # Chat column
101
  with gr.Column(scale=4):
102
  chatbot = gr.Chatbot(
103
  bubble_full_width=False,
@@ -117,25 +112,11 @@ with gr.Blocks(css=".gr-chatbot .message { margin: 2px 0 !important; }", title=$
117
  label="Example Prompts"
118
  )
119
 
120
- # Event handlers
121
  model_selector.change(fn=update_model, inputs=[model_selector], outputs=[])
122
-
123
- msg_input.submit(
124
- fn=user,
125
- inputs=[msg_input, chatbot],
126
- outputs=[msg_input, chatbot],
127
- queue=False
128
- ).then(
129
- fn=bot,
130
- inputs=[chatbot, max_tokens, temperature, top_p],
131
- outputs=[chatbot]
132
- )
133
- clear_button.click(
134
- fn=lambda: [],
135
- inputs=None,
136
- outputs=chatbot,
137
- queue=False
138
  )
 
139
 
140
  if __name__ == "__main__":
141
  demo.queue(default_concurrency_limit=10, max_size=10).launch(ssr_mode=False, max_threads=10)
 
48
  thread = threading.Thread(target=run)
49
  thread.start()
50
 
51
+ response = ""
52
  for new_text in streamer:
53
+ response += new_text
54
+ # prepend model name on its own line
55
+ yield f"**{current_model_name}**\n\n{response}"
56
 
57
  # User input handler
58
  def user(message, chat_history):
 
60
  return "", chat_history
61
 
62
  # Bot response handler
63
+ def bot(chatbot, max_tokens, temperature, top_p):
64
+ message = chatbot[-1][0]
65
+ response_generator = respond(message, max_tokens, temperature, top_p)
66
+ for response in response_generator:
67
+ chatbot[-1][1] = response
68
+ yield chatbot
 
 
 
 
 
 
69
 
70
  # Model selector handler
71
  def update_model(model_name):
 
73
  return []
74
 
75
  # Gradio UI
76
+ with gr.Blocks(title="LeCarnet - Chat Interface") as demo:
77
  with gr.Row():
78
  gr.HTML("""
79
  <div style="text-align: center; width: 100%;">
 
82
  """ )
83
 
84
  with gr.Row():
 
85
  with gr.Column(scale=1, min_width=150):
86
  model_selector = gr.Dropdown(
87
  choices=["LeCarnet-3M", "LeCarnet-8M", "LeCarnet-21M"],
 
93
  top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p Sampling")
94
  clear_button = gr.Button("Clear Chat")
95
 
 
96
  with gr.Column(scale=4):
97
  chatbot = gr.Chatbot(
98
  bubble_full_width=False,
 
112
  label="Example Prompts"
113
  )
114
 
 
115
  model_selector.change(fn=update_model, inputs=[model_selector], outputs=[])
116
+ msg_input.submit(fn=user, inputs=[msg_input, chatbot], outputs=[msg_input, chatbot], queue=False).then(
117
+ fn=bot, inputs=[chatbot, max_tokens, temperature, top_p], outputs=[chatbot]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  )
119
+ clear_button.click(fn=lambda: None, inputs=None, outputs=chatbot, queue=False)
120
 
121
  if __name__ == "__main__":
122
  demo.queue(default_concurrency_limit=10, max_size=10).launch(ssr_mode=False, max_threads=10)