nazdridoy commited on
Commit
a368442
Β·
verified Β·
1 Parent(s): 79a256a

refactor(ui): re-architect chat interface

Browse files

- [refactor] Remove `gr.ChatInterface` component (app.py:199)
- [refactor] Add `gr.Chatbot` for display and `gr.Textbox` with `gr.Button` for chat input (app.py:202-221)
- [refactor] Introduce dedicated `gr.Textbox` and `gr.Slider` components for chat configuration (app.py:224-245)
- [refactor] Define `handle_chat_submit()` to manage chat logic and history updates (app.py:264-288)
- [refactor] Configure `chat_submit.click()` and `chat_input.submit()` events to call `handle_chat_submit()` (app.py:291-303)

Files changed (1) hide show
  1. app.py +76 -18
app.py CHANGED
@@ -199,38 +199,52 @@ with gr.Blocks(title="HF-Inferoxy AI Hub", theme=gr.themes.Soft()) as demo:
199
 
200
  # ==================== CHAT TAB ====================
201
  with gr.Tab("πŸ’¬ Chat Assistant", id="chat"):
202
- # Main chat interface - full width and prominent
203
- chatbot = gr.ChatInterface(
204
- chat_respond,
205
  type="messages",
206
- title="",
207
- description="",
208
- additional_inputs=[
209
- gr.Textbox(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  value="openai/gpt-oss-20b",
211
  label="Model Name",
212
  placeholder="e.g., openai/gpt-oss-20b or openai/gpt-oss-20b:fireworks-ai"
213
- ),
214
- gr.Textbox(
215
  value="You are a helpful and friendly AI assistant. Provide clear, accurate, and helpful responses.",
216
  label="System Message",
217
  lines=2,
218
  placeholder="Define the assistant's personality and behavior..."
219
- ),
220
- gr.Slider(
 
 
221
  minimum=1, maximum=4096, value=1024, step=1,
222
  label="Max New Tokens"
223
- ),
224
- gr.Slider(
225
  minimum=0.1, maximum=2.0, value=0.7, step=0.1,
226
  label="Temperature"
227
- ),
228
- gr.Slider(
229
  minimum=0.1, maximum=1.0, value=0.95, step=0.05,
230
  label="Top-p (nucleus sampling)"
231
- ),
232
- ],
233
- )
234
 
235
  # Configuration tips below the chat
236
  with gr.Row():
@@ -264,6 +278,50 @@ with gr.Blocks(title="HF-Inferoxy AI Hub", theme=gr.themes.Soft()) as demo:
264
  - `openai/gpt-oss-20b` (auto provider)
265
  - `openai/gpt-oss-20b:fireworks-ai` (specific provider)
266
  """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
 
268
  # ==================== IMAGE GENERATION TAB ====================
269
  with gr.Tab("🎨 Image Generator", id="image"):
 
199
 
200
  # ==================== CHAT TAB ====================
201
  with gr.Tab("πŸ’¬ Chat Assistant", id="chat"):
202
+ # Chat interface at the top - most prominent
203
+ chatbot_display = gr.Chatbot(
204
+ label="Chat",
205
  type="messages",
206
+ height=500,
207
+ show_copy_button=True
208
+ )
209
+
210
+ # Chat input
211
+ with gr.Row():
212
+ chat_input = gr.Textbox(
213
+ placeholder="Type your message here...",
214
+ label="Message",
215
+ scale=4,
216
+ container=False
217
+ )
218
+ chat_submit = gr.Button("Send", variant="primary", scale=1)
219
+
220
+ # Configuration options below the chat
221
+ with gr.Row():
222
+ with gr.Column(scale=1):
223
+ chat_model_name = gr.Textbox(
224
  value="openai/gpt-oss-20b",
225
  label="Model Name",
226
  placeholder="e.g., openai/gpt-oss-20b or openai/gpt-oss-20b:fireworks-ai"
227
+ )
228
+ chat_system_message = gr.Textbox(
229
  value="You are a helpful and friendly AI assistant. Provide clear, accurate, and helpful responses.",
230
  label="System Message",
231
  lines=2,
232
  placeholder="Define the assistant's personality and behavior..."
233
+ )
234
+
235
+ with gr.Column(scale=1):
236
+ chat_max_tokens = gr.Slider(
237
  minimum=1, maximum=4096, value=1024, step=1,
238
  label="Max New Tokens"
239
+ )
240
+ chat_temperature = gr.Slider(
241
  minimum=0.1, maximum=2.0, value=0.7, step=0.1,
242
  label="Temperature"
243
+ )
244
+ chat_top_p = gr.Slider(
245
  minimum=0.1, maximum=1.0, value=0.95, step=0.05,
246
  label="Top-p (nucleus sampling)"
247
+ )
 
 
248
 
249
  # Configuration tips below the chat
250
  with gr.Row():
 
278
  - `openai/gpt-oss-20b` (auto provider)
279
  - `openai/gpt-oss-20b:fireworks-ai` (specific provider)
280
  """)
281
+
282
+ # Chat functionality
283
+ def handle_chat_submit(message, history, system_msg, model_name, max_tokens, temperature, top_p):
284
+ if not message.strip():
285
+ return history, ""
286
+
287
+ # Add user message to history
288
+ history = history + [{"role": "user", "content": message}]
289
+
290
+ # Generate response
291
+ response_generator = chat_respond(
292
+ message,
293
+ history[:-1], # Don't include the current message in history for the function
294
+ system_msg,
295
+ model_name,
296
+ max_tokens,
297
+ temperature,
298
+ top_p
299
+ )
300
+
301
+ # Get the final response
302
+ assistant_response = ""
303
+ for partial_response in response_generator:
304
+ assistant_response = partial_response
305
+
306
+ # Add assistant response to history
307
+ history = history + [{"role": "assistant", "content": assistant_response}]
308
+
309
+ return history, ""
310
+
311
+ # Connect chat events
312
+ chat_submit.click(
313
+ fn=handle_chat_submit,
314
+ inputs=[chat_input, chatbot_display, chat_system_message, chat_model_name,
315
+ chat_max_tokens, chat_temperature, chat_top_p],
316
+ outputs=[chatbot_display, chat_input]
317
+ )
318
+
319
+ chat_input.submit(
320
+ fn=handle_chat_submit,
321
+ inputs=[chat_input, chatbot_display, chat_system_message, chat_model_name,
322
+ chat_max_tokens, chat_temperature, chat_top_p],
323
+ outputs=[chatbot_display, chat_input]
324
+ )
325
 
326
  # ==================== IMAGE GENERATION TAB ====================
327
  with gr.Tab("🎨 Image Generator", id="image"):