alibayram commited on
Commit
0eefbc1
Β·
1 Parent(s): ff7d616

space update

Browse files
Files changed (1) hide show
  1. app.py +84 -38
app.py CHANGED
@@ -189,6 +189,11 @@ def respond(
189
  max_tokens,
190
  temperature,
191
  top_p,
 
 
 
 
 
192
  ):
193
  """
194
  Generate a response using the UstaModel
@@ -234,44 +239,88 @@ def respond(
234
  except Exception as e:
235
  yield f"Sorry, I encountered an error: {str(e)}"
236
 
237
- # Create the simple ChatInterface with additional inputs for model loading
238
- demo = gr.ChatInterface(
239
- respond,
240
- additional_inputs=[
241
- gr.Textbox(
242
- value="You are Usta, a geographical knowledge assistant trained from scratch.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
  label="System message"
244
- ),
245
- gr.Slider(minimum=1, maximum=30, value=20, step=1, label="Max new tokens"),
246
- gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Temperature"),
247
- gr.Slider(
248
- minimum=0.1,
249
- maximum=1.0,
250
- value=0.95,
251
- step=0.05,
252
- label="Top-p (nucleus sampling)"
253
- ),
254
- gr.File(label="Upload Model File (.pth)", file_types=[".pth", ".pt"]),
255
- gr.Textbox(label="Or Model URL", placeholder="https://github.com/user/repo/raw/main/model.pth"),
256
- gr.Button("Load from File", variant="secondary"),
257
- gr.Button("Load from URL", variant="secondary"),
258
- gr.Textbox(label="Model Status", value=model_status, interactive=False)
259
- ],
260
- title="πŸ€– Usta Model Chat",
261
- description="Chat with a custom transformer language model built from scratch! Upload your own model file or provide a URL to load a different model."
262
- )
263
-
264
- # Add event handlers after creating the interface
265
- def setup_events():
266
- # Get the additional inputs
267
- inputs = demo.additional_inputs
268
- model_file = inputs[4] # File upload
269
- model_url = inputs[5] # URL input
270
- load_file_btn = inputs[6] # Load from file button
271
- load_url_btn = inputs[7] # Load from URL button
272
- status_display = inputs[8] # Status display
 
 
 
 
 
273
 
274
  # Set up event handlers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  load_file_btn.click(
276
  load_model_from_file,
277
  inputs=[model_file],
@@ -284,8 +333,5 @@ def setup_events():
284
  outputs=[status_display]
285
  )
286
 
287
- # Set up events after interface creation
288
- demo.load(setup_events)
289
-
290
  if __name__ == "__main__":
291
  demo.launch()
 
189
  max_tokens,
190
  temperature,
191
  top_p,
192
+ model_file,
193
+ model_url,
194
+ load_file_btn,
195
+ load_url_btn,
196
+ status_display
197
  ):
198
  """
199
  Generate a response using the UstaModel
 
239
  except Exception as e:
240
  yield f"Sorry, I encountered an error: {str(e)}"
241
 
242
+ # Create a Blocks interface to properly handle events
243
+ with gr.Blocks(title="πŸ€– Usta Model Chat", theme=gr.themes.Soft()) as demo:
244
+ gr.Markdown("# πŸ€– Usta Model Chat")
245
+ gr.Markdown("Chat with a custom transformer language model built from scratch! Upload your own model file or provide a URL to load a different model.")
246
+
247
+ # Model loading section
248
+ with gr.Accordion("πŸ”§ Model Loading Options", open=False):
249
+ with gr.Row():
250
+ with gr.Column():
251
+ gr.Markdown("### πŸ“ Upload Model File")
252
+ model_file = gr.File(label="Upload Model File (.pth)", file_types=[".pth", ".pt"])
253
+ load_file_btn = gr.Button("Load from File", variant="primary")
254
+
255
+ with gr.Column():
256
+ gr.Markdown("### πŸ”— Load from URL")
257
+ model_url = gr.Textbox(label="Model URL", placeholder="https://github.com/malibayram/llm-from-scratch/raw/main/u_model_4000.pth")
258
+ load_url_btn = gr.Button("Load from URL", variant="primary")
259
+
260
+ status_display = gr.Textbox(label="Model Status", value=model_status, interactive=False)
261
+
262
+ # Chat interface (simpler version)
263
+ chatbot = gr.Chatbot(label="Chat", type="messages")
264
+ msg = gr.Textbox(label="Message", placeholder="Type your message here...")
265
+
266
+ # Generation settings
267
+ with gr.Accordion("βš™οΈ Generation Settings", open=False):
268
+ system_msg = gr.Textbox(
269
+ value="You are Usta, a geographical knowledge assistant trained from scratch.",
270
  label="System message"
271
+ )
272
+ max_tokens = gr.Slider(minimum=1, maximum=30, value=20, step=1, label="Max new tokens")
273
+ temperature = gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Temperature")
274
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
275
+
276
+ # Button controls
277
+ with gr.Row():
278
+ submit_btn = gr.Button("Send", variant="primary")
279
+ clear_btn = gr.Button("Clear Chat", variant="secondary")
280
+
281
+ # Event handlers
282
+ def chat_respond(message, history, sys_msg, max_tok, temp, top_p_val):
283
+ if not message.strip():
284
+ return history, ""
285
+
286
+ # Convert messages format for our respond function
287
+ tuple_history = [(h["role"], h["content"]) for h in history if h["role"] != "system"]
288
+
289
+ # Generate response using our existing function
290
+ response_gen = respond(
291
+ message, tuple_history, sys_msg, max_tok, temp, top_p_val,
292
+ None, None, None, None, None # Dummy values for unused params
293
+ )
294
+
295
+ # Get the response
296
+ response = ""
297
+ for r in response_gen:
298
+ response = r
299
+
300
+ # Add to history in messages format
301
+ history.append({"role": "user", "content": message})
302
+ history.append({"role": "assistant", "content": response})
303
+
304
+ return history, ""
305
 
306
  # Set up event handlers
307
+ submit_btn.click(
308
+ chat_respond,
309
+ inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
310
+ outputs=[chatbot, msg]
311
+ )
312
+
313
+ msg.submit(
314
+ chat_respond,
315
+ inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
316
+ outputs=[chatbot, msg]
317
+ )
318
+
319
+ clear_btn.click(
320
+ lambda: ([], ""),
321
+ outputs=[chatbot, msg]
322
+ )
323
+
324
  load_file_btn.click(
325
  load_model_from_file,
326
  inputs=[model_file],
 
333
  outputs=[status_display]
334
  )
335
 
 
 
 
336
  if __name__ == "__main__":
337
  demo.launch()