FESG1234 commited on
Commit
0b1754f
·
verified ·
1 Parent(s): f3dff51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -32
app.py CHANGED
@@ -3,8 +3,7 @@ from transformers import pipeline
3
  import gradio as gr
4
  import PyPDF2
5
  import os
6
- from huggingface_hub import login, HfFolder
7
- from getpass import getpass
8
 
9
  # Space configuration
10
  SPACE_DIR = os.environ.get("HF_HOME", os.getcwd())
@@ -64,11 +63,6 @@ SYSTEM_PROMPT = f"""You are Foton, Swahili AI assistant. Tasks:
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}]
@@ -77,13 +71,12 @@ def format_messages(history):
77
  messages.append(msg)
78
  return messages
79
 
80
- def generate_response(message, history):
81
  """Generate AI response"""
82
  try:
83
- formatted_history = format_messages(history)
84
- formatted_history.append({"role": "user", "content": message})
85
 
86
- prompt = "\n".join([f"{m['role']}: {m['content']}" for m in formatted_history])
87
 
88
  output = pipe(
89
  prompt,
@@ -102,39 +95,33 @@ 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)
133
 
134
  if __name__ == "__main__":
135
  demo.launch(
136
  server_name="0.0.0.0",
137
  server_port=7860,
138
  auth=os.getenv("SPACE_AUTH"),
139
- show_error=True
 
140
  )
 
3
  import gradio as gr
4
  import PyPDF2
5
  import os
6
+ from huggingface_hub import login
 
7
 
8
  # Space configuration
9
  SPACE_DIR = os.environ.get("HF_HOME", os.getcwd())
 
63
  Maintain friendly, patient demeanor with cultural context.
64
  """
65
 
 
 
 
 
 
66
  def format_messages(history):
67
  """Format chat history with system prompt"""
68
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
 
71
  messages.append(msg)
72
  return messages
73
 
74
+ def generate_response(history):
75
  """Generate AI response"""
76
  try:
77
+ messages = format_messages(history)
 
78
 
79
+ prompt = "\n".join([f"{m['role']}: {m['content']}" for m in messages])
80
 
81
  output = pipe(
82
  prompt,
 
95
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
96
  gr.Markdown("# Lugha Tausi - Swahili Assistant")
97
 
 
98
  chatbot = gr.Chatbot(
99
+ value=[{"role": "assistant", "content": "**Karibu Lugha Tausi!** Mimi ni Foton, msaidizi wako wa Kiswahili. Niko hapa kukusaidia kujifunza na kuzungumza Kiswahili. **Ninaweza kukusaidiaje leo?** 😊"}],
100
+ height=600,
101
+ show_label=False,
102
+ avatar_images=(None, "user.png"),
103
+ bubble_full_width=False,
104
+ show_share_button=False,
105
+ type="messages"
106
  )
107
 
108
  msg = gr.Textbox(placeholder="Andika ujumbe wako hapa...", show_label=False)
109
  clear = gr.Button("Futa Mazungumzo")
110
 
111
+ def respond(message, history):
112
+ history.append({"role": "user", "content": message})
113
+ bot_response = generate_response(history)
114
+ history.append({"role": "assistant", "content": bot_response})
115
+ return "", history
 
 
 
 
 
 
116
 
117
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
118
+ clear.click(lambda: [None, []], None, [msg, chatbot], queue=False)
119
 
120
  if __name__ == "__main__":
121
  demo.launch(
122
  server_name="0.0.0.0",
123
  server_port=7860,
124
  auth=os.getenv("SPACE_AUTH"),
125
+ show_error=True,
126
+ share=False # Explicitly disable sharing
127
  )