Denis202 commited on
Commit
4ab418d
Β·
verified Β·
1 Parent(s): 152c163

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -22
app.py CHANGED
@@ -2,16 +2,22 @@ import gradio as gr
2
  import time
3
 
4
  def chat_response(message, history):
5
- # Simulate a response with a delay, replace with your model call
6
  time.sleep(1)
7
  if not message or message.strip() == "":
8
  return history, "Tafadhali andika ujumbe..."
9
  response = f"Majibu: {message}"
10
- new_history = history + [{"role": "user", "content": message},
11
- {"role": "assistant", "content": response}]
 
 
12
  return new_history, ""
13
 
14
- with gr.Blocks(theme=gr.themes.Base(), css="""
 
 
 
 
15
  .gradio-container {max-width: 650px; margin:30px auto; border-radius:16px;}
16
  .header {text-align:center; margin:16px 0; font-size:1.2em;}
17
  #chat_area {background:#fff; border-radius:12px; min-height:340px; overflow-y:auto; box-shadow:0 3px 16px #eee;}
@@ -19,53 +25,74 @@ with gr.Blocks(theme=gr.themes.Base(), css="""
19
  textarea {flex:1; resize:none; border-radius:10px; padding:10px; font-size:1em;}
20
  .button-primary {background:#1db954; color:#fff; border-radius:10px; font-weight:bold;}
21
  .button-secondary {background:#ff6666; color:#fff; border-radius:10px; font-weight:bold;}
 
 
22
  @media(max-width: 680px){.gradio-container{max-width: 98vw; padding: 0 2vw;}}
23
- """) as demo:
24
- # Header title
 
 
25
  gr.HTML('<div class="header"><b>KiswahiliChetu</b><br>Jifunze Kiswahili fasaha na LLM</div>')
26
 
27
- # Dark mode toggle
28
  dark_mode = gr.State(False)
 
 
 
29
  def toggle_theme(current):
30
  return not current
31
- gr.Button("πŸŒ™ Toggle Dark Mode", variant="secondary").click(toggle_theme, dark_mode, dark_mode, _js="""
32
- () => { document.body.classList.toggle('dark'); }
33
- """)
34
 
35
- # Chat display
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  chat = gr.Chatbot(elem_id="chat_area", label="", show_copy_button=True, value=[], type="messages")
37
- state = gr.State([])
38
 
39
- # Input
40
  with gr.Row():
41
  msg_input = gr.Textbox(placeholder="Andika ujumbe wa Kiswahili...", lines=3, label="")
42
- send_btn = gr.Button("Tuma")
43
- clear_btn = gr.Button("Futa")
44
 
45
- # Examples
46
  with gr.Accordion("πŸ“š Mifano ya Maswali", open=False):
47
  gr.Examples(
48
  examples=[["Habari yako?"], ["Jina lako nani?"], ["Eleza kuhusu Tanzania"]],
49
  inputs=msg_input,
50
  )
51
 
52
- # Process message
53
  def process(msg, chat_history, state):
54
  if not msg.strip():
55
  return chat_history, "Tafadhali andika ujumbe..."
56
- # Show loading indicator
57
  chat_history.append({"role": "user", "content": msg})
58
- # Simulate response delay
59
  response = f"Majibu: {msg}"
60
  chat_history.append({"role": "assistant", "content": response})
61
  return chat_history, ""
62
 
63
- # Button actions
64
- send_btn.click(process, [msg_input, chat, state], [chat, msg_input])
65
- msg_input.submit(process, [msg_input, chat, state], [chat, msg_input])
66
  def clear_chat():
67
  return [], ""
 
 
 
 
68
  clear_btn.click(clear_chat, None, [chat, msg_input])
69
 
 
 
70
  if __name__ == "__main__":
71
  demo.launch(server_name="0.0.0.0", share=False, show_error=True)
 
2
  import time
3
 
4
  def chat_response(message, history):
5
+ # Simulate a response with a delay
6
  time.sleep(1)
7
  if not message or message.strip() == "":
8
  return history, "Tafadhali andika ujumbe..."
9
  response = f"Majibu: {message}"
10
+ new_history = history + [
11
+ {"role": "user", "content": message},
12
+ {"role": "assistant", "content": response},
13
+ ]
14
  return new_history, ""
15
 
16
+
17
+ # ---- APP UI ----
18
+ with gr.Blocks(
19
+ theme=gr.themes.Base(),
20
+ css="""
21
  .gradio-container {max-width: 650px; margin:30px auto; border-radius:16px;}
22
  .header {text-align:center; margin:16px 0; font-size:1.2em;}
23
  #chat_area {background:#fff; border-radius:12px; min-height:340px; overflow-y:auto; box-shadow:0 3px 16px #eee;}
 
25
  textarea {flex:1; resize:none; border-radius:10px; padding:10px; font-size:1em;}
26
  .button-primary {background:#1db954; color:#fff; border-radius:10px; font-weight:bold;}
27
  .button-secondary {background:#ff6666; color:#fff; border-radius:10px; font-weight:bold;}
28
+ body.dark {background-color:#1e1e1e; color:#fff;}
29
+ #chat_area.dark {background:#333;}
30
  @media(max-width: 680px){.gradio-container{max-width: 98vw; padding: 0 2vw;}}
31
+ """
32
+ ) as demo:
33
+
34
+ # Header
35
  gr.HTML('<div class="header"><b>KiswahiliChetu</b><br>Jifunze Kiswahili fasaha na LLM</div>')
36
 
37
+ # State
38
  dark_mode = gr.State(False)
39
+ state = gr.State([])
40
+
41
+ # Toggle function (backend)
42
  def toggle_theme(current):
43
  return not current
 
 
 
44
 
45
+ # Create toggle button
46
+ toggle_btn = gr.Button("πŸŒ™ Toggle Dark Mode", variant="secondary")
47
+
48
+ # Try JS injection safely
49
+ try:
50
+ toggle_btn.click(
51
+ toggle_theme,
52
+ inputs=dark_mode,
53
+ outputs=dark_mode,
54
+ _js="() => { document.body.classList.toggle('dark'); }"
55
+ )
56
+ except TypeError:
57
+ # Older Gradio fallback (no _js support)
58
+ toggle_btn.click(toggle_theme, inputs=dark_mode, outputs=dark_mode)
59
+
60
+ # Chat area
61
  chat = gr.Chatbot(elem_id="chat_area", label="", show_copy_button=True, value=[], type="messages")
 
62
 
63
+ # Input row
64
  with gr.Row():
65
  msg_input = gr.Textbox(placeholder="Andika ujumbe wa Kiswahili...", lines=3, label="")
66
+ send_btn = gr.Button("Tuma", elem_classes="button-primary")
67
+ clear_btn = gr.Button("Futa", elem_classes="button-secondary")
68
 
69
+ # Examples section
70
  with gr.Accordion("πŸ“š Mifano ya Maswali", open=False):
71
  gr.Examples(
72
  examples=[["Habari yako?"], ["Jina lako nani?"], ["Eleza kuhusu Tanzania"]],
73
  inputs=msg_input,
74
  )
75
 
76
+ # Main message handler
77
  def process(msg, chat_history, state):
78
  if not msg.strip():
79
  return chat_history, "Tafadhali andika ujumbe..."
 
80
  chat_history.append({"role": "user", "content": msg})
81
+ time.sleep(0.8)
82
  response = f"Majibu: {msg}"
83
  chat_history.append({"role": "assistant", "content": response})
84
  return chat_history, ""
85
 
86
+ # Clear chat function
 
 
87
  def clear_chat():
88
  return [], ""
89
+
90
+ # Button and Enter bindings
91
+ send_btn.click(process, [msg_input, chat, state], [chat, msg_input])
92
+ msg_input.submit(process, [msg_input, chat, state], [chat, msg_input])
93
  clear_btn.click(clear_chat, None, [chat, msg_input])
94
 
95
+
96
+ # ---- LAUNCH ----
97
  if __name__ == "__main__":
98
  demo.launch(server_name="0.0.0.0", share=False, show_error=True)