FESG1234 commited on
Commit
f3dff51
·
verified ·
1 Parent(s): 88350fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
app.py CHANGED
@@ -64,18 +64,17 @@ SYSTEM_PROMPT = f"""You are Foton, Swahili AI assistant. Tasks:
64
  Maintain friendly, patient demeanor with cultural context.
65
  """
66
 
67
- WELCOME_MESSAGE = "**Karibu Lugha Tausi!** Mimi ni Foton, msaidizi wako wa Kiswahili. Niko hapa kukusaidia kujifunza na kuzungumza Kiswahili. **Ninaweza kukusaidiaje leo?** 😊"
68
-
 
 
 
69
  def format_messages(history):
70
  """Format chat history with system prompt"""
71
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
72
- for entry in history:
73
- if isinstance(entry, tuple):
74
- user, bot = entry
75
- messages.extend([
76
- {"role": "user", "content": user},
77
- {"role": "assistant", "content": bot}
78
- ])
79
  return messages
80
 
81
  def generate_response(message, history):
@@ -103,23 +102,31 @@ def generate_response(message, history):
103
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
104
  gr.Markdown("# Lugha Tausi - Swahili Assistant")
105
 
 
106
  chatbot = gr.Chatbot(
107
- value=[[None, WELCOME_MESSAGE]],
108
- height=600,
109
- show_label=False,
110
- avatar_images=(None, "user.png"),
111
- bubble_full_width=False,
112
- show_share_button=False,
113
- type="messages"
114
  )
115
 
116
  msg = gr.Textbox(placeholder="Andika ujumbe wako hapa...", show_label=False)
117
  clear = gr.Button("Futa Mazungumzo")
118
 
119
  def respond(message, chat_history):
120
- bot_response = generate_response(message, chat_history)
121
- chat_history.append((message, bot_response))
122
- return "", chat_history
 
 
 
 
 
 
 
123
 
124
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
125
  clear.click(lambda: None, None, chatbot, queue=False)
 
64
  Maintain friendly, patient demeanor with cultural context.
65
  """
66
 
67
+ # Update the WELCOME_MESSAGE format
68
+ WELCOME_MESSAGE = {
69
+ "role": "assistant",
70
+ "content": "**Karibu Lugha Tausi!** Mimi ni Foton, msaidizi wako wa Kiswahili. Niko hapa kukusaidia kujifunza na kuzungumza Kiswahili. **Ninaweza kukusaidiaje leo?** 😊"
71
+ }
72
  def format_messages(history):
73
  """Format chat history with system prompt"""
74
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
75
+ for msg in history:
76
+ if isinstance(msg, dict) and "role" in msg and "content" in msg:
77
+ messages.append(msg)
 
 
 
 
78
  return messages
79
 
80
  def generate_response(message, history):
 
102
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
103
  gr.Markdown("# Lugha Tausi - Swahili Assistant")
104
 
105
+ # Update the Chatbot initialization
106
  chatbot = gr.Chatbot(
107
+ value=[WELCOME_MESSAGE], # Now using proper message format
108
+ height=600,
109
+ show_label=False,
110
+ avatar_images=(None, "user.png"),
111
+ bubble_full_width=False,
112
+ show_share_button=False,
113
+ type="messages"
114
  )
115
 
116
  msg = gr.Textbox(placeholder="Andika ujumbe wako hapa...", show_label=False)
117
  clear = gr.Button("Futa Mazungumzo")
118
 
119
  def respond(message, chat_history):
120
+ # Add user message
121
+ chat_history.append({"role": "user", "content": message})
122
+
123
+ # Generate response
124
+ bot_response = generate_response(message, chat_history)
125
+
126
+ # Add assistant response
127
+ chat_history.append({"role": "assistant", "content": bot_response})
128
+
129
+ return "", chat_history
130
 
131
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
132
  clear.click(lambda: None, None, chatbot, queue=False)