manthilaffs commited on
Commit
9ae1d9d
·
verified ·
1 Parent(s): f250f88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -20
app.py CHANGED
@@ -15,10 +15,8 @@ alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර
15
  ### ප්‍රතිචාරය:
16
  {}"""
17
 
18
- @spaces.GPU
19
- def infer(message, history, enable_history=False, max_new_tokens=1024):
20
  global model, tokenizer
21
-
22
  if model is None:
23
  tokenizer = AutoTokenizer.from_pretrained("manthilaffs/Gamunu-4B-Instruct-Alpha")
24
  model = AutoModelForCausalLM.from_pretrained(
@@ -27,6 +25,12 @@ def infer(message, history, enable_history=False, max_new_tokens=1024):
27
  device_map="auto",
28
  )
29
  model.eval()
 
 
 
 
 
 
30
 
31
  # Add history only if enabled
32
  if enable_history and history:
@@ -77,6 +81,9 @@ def infer(message, history, enable_history=False, max_new_tokens=1024):
77
 
78
  if response_started:
79
  yield partial_text
 
 
 
80
 
81
  # Custom CSS for styling with copy button
82
  custom_css = """
@@ -296,25 +303,36 @@ splash_html = """
296
  with gr.Blocks(css=custom_css, head=copy_js) as demo:
297
  gr.HTML(splash_html)
298
 
299
- chat = gr.ChatInterface(
300
- fn=lambda message, history: infer(message, history, enable_history.value, max_new_tokens.value),
301
- title="🧠 Gamunu 4B Instruct - Demo",
302
- theme=gr.themes.Default(text_size="sm"),
303
- examples=[
304
- ["හෙලෝ ගැමුණු! මම සමන්, ඔයාට කොහොමද?"],
305
- ["ෆොටෝසින්තසිස් ක්‍රියාවලිය පැහැදිලි කරන්න."],
306
- ["මෙම වාක්‍යය සිංහලයට පරිවර්තනය කරන්න: 'The sun rises in the east.'"],
307
- ["'completed' තත්ත්වයේ ඇති වාර්තා ගණන ගණනය කිරීමට දත්ත සමුදා විමසුමක් (database query) ගොඩනඟන්න."],
308
- ["ඔබ ගුරුවරයෙකු ලෙස ක්‍රියාකරන්න. ශිෂ්‍යයාට ඉතිහාසය උගන්වන්න."],
309
- ["පහත ප්‍රකාශය ප්‍රංශ භාෂාවට පරිවර්තනය කරන්න. Laughter is the best medicine."],
310
- ["ඝන වස්තුවක හා ද්‍රවයක පරිමාවන්හි වෙනස පැහැදිලි කරන්න."],
311
- ["වෙස් මුහුණු කලාවේ ප්‍රධාන අංග මොනවාද? වර්තමානයේ මෙම කලාව ප්‍රචලිතව පවතින ප්‍රදේශ මොනවාද?"]
312
- ]
313
- )
 
 
 
 
 
 
 
314
 
315
  with gr.Accordion("⚙️ Advanced Settings", open=False):
316
- enable_history = gr.Checkbox(label="Enable chat history", value=False)
317
- max_new_tokens = gr.Slider(64, 1024, value=512, step=32, label="🔢 Max New Tokens")
 
 
 
 
318
 
319
  gr.Markdown("""
320
  ---
 
15
  ### ප්‍රතිචාරය:
16
  {}"""
17
 
18
+ def load_model():
 
19
  global model, tokenizer
 
20
  if model is None:
21
  tokenizer = AutoTokenizer.from_pretrained("manthilaffs/Gamunu-4B-Instruct-Alpha")
22
  model = AutoModelForCausalLM.from_pretrained(
 
25
  device_map="auto",
26
  )
27
  model.eval()
28
+
29
+ @spaces.GPU
30
+ def generate_response(message, history, enable_history=False, max_new_tokens=1024):
31
+ global model, tokenizer
32
+
33
+ load_model()
34
 
35
  # Add history only if enabled
36
  if enable_history and history:
 
81
 
82
  if response_started:
83
  yield partial_text
84
+
85
+ # Make sure thread completes
86
+ thread.join()
87
 
88
  # Custom CSS for styling with copy button
89
  custom_css = """
 
303
  with gr.Blocks(css=custom_css, head=copy_js) as demo:
304
  gr.HTML(splash_html)
305
 
306
+ enable_history = gr.State(value=False)
307
+ max_new_tokens = gr.State(value=512)
308
+
309
+ with gr.Row():
310
+ with gr.Column():
311
+ chat = gr.ChatInterface(
312
+ fn=generate_response,
313
+ title="🧠 Gamunu 4B Instruct - Demo",
314
+ theme=gr.themes.Default(text_size="sm"),
315
+ type="messages", # Use new messages format
316
+ examples=[
317
+ ["හෙලෝ ගැමුණු! මම සමන්, ඔයාට කොහොමද?"],
318
+ ["ෆොටෝසින්තසිස් ක්‍රියාවලිය පැහැදිලි කරන්න."],
319
+ ["මෙම වාක්‍යය සිංහලයට පරිවර්තනය කරන්න: 'The sun rises in the east.'"],
320
+ ["'completed' තත්ත්වයේ ඇති වාර්තා ගණන ගණනය කිරීමට දත්ත සමුදා විමසුමක් (database query) ගොඩනඟන්න."],
321
+ ["ඔබ ගුරුවරයෙකු ලෙස ක්‍රියාකරන්න. ශිෂ්‍යයාට ඉතිහාසය උගන්වන්න."],
322
+ ["පහත ප්‍රකාශය ප්‍රංශ භාෂාවට පරිවර්තනය කරන්න. Laughter is the best medicine."],
323
+ ["ඝන වස්තුවක හා ද්‍රවයක පරිමාවන්හි වෙනස පැහැදිලි කරන්න."],
324
+ ["වෙස් මුහුණු කලාවේ ප්‍රධාන අංග මොනවාද? වර්තමානයේ මෙම කලාව ප්‍රචලිතව පවතින ප්‍රදේශ මොනවාද?"]
325
+ ],
326
+ additional_inputs=[enable_history, max_new_tokens]
327
+ )
328
 
329
  with gr.Accordion("⚙️ Advanced Settings", open=False):
330
+ history_checkbox = gr.Checkbox(label="Enable chat history", value=False)
331
+ tokens_slider = gr.Slider(64, 1024, value=512, step=32, label="🔢 Max New Tokens")
332
+
333
+ # Update state when controls change
334
+ history_checkbox.change(fn=lambda x: x, inputs=history_checkbox, outputs=enable_history)
335
+ tokens_slider.change(fn=lambda x: x, inputs=tokens_slider, outputs=max_new_tokens)
336
 
337
  gr.Markdown("""
338
  ---