ajsbsd commited on
Commit
3eac2fe
·
verified ·
1 Parent(s): b797037

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -76,7 +76,6 @@ def predict_chat(message: str, history: list):
76
  return
77
 
78
  # history is already in the 'messages' format if type='messages' is set on chatbot
79
- # It contains dictionaries with 'role' and 'content'
80
  messages = [{"role": "system", "content": "You are a friendly chatbot."}] + history
81
  messages.append({"role": "user", "content": message})
82
 
@@ -131,15 +130,18 @@ if __name__ == "__main__":
131
  load_model_for_zerocpu()
132
 
133
  # Initial message for the chatbot in the 'messages' format
134
- initial_messages = [{"role": "assistant", "content":
135
  "Hello! I'm an AI assistant. I'm currently running in a CPU-only "
136
  "environment for efficient demonstration. How can I help you today?"
137
  }]
138
 
 
 
 
139
  demo = gr.ChatInterface(
140
  fn=predict_chat,
141
- # Define the chatbot here, with type='messages' and initial value in the correct format
142
- chatbot=gr.Chatbot(height=500, type='messages', value=initial_messages),
143
  textbox=gr.Textbox(
144
  placeholder="Ask me a question...",
145
  container=False,
@@ -162,4 +164,8 @@ if __name__ == "__main__":
162
  clear_btn="Clear Chat"
163
  )
164
 
 
 
 
 
165
  demo.launch()
 
76
  return
77
 
78
  # history is already in the 'messages' format if type='messages' is set on chatbot
 
79
  messages = [{"role": "system", "content": "You are a friendly chatbot."}] + history
80
  messages.append({"role": "user", "content": message})
81
 
 
130
  load_model_for_zerocpu()
131
 
132
  # Initial message for the chatbot in the 'messages' format
133
+ initial_messages_for_value = [{"role": "assistant", "content":
134
  "Hello! I'm an AI assistant. I'm currently running in a CPU-only "
135
  "environment for efficient demonstration. How can I help you today?"
136
  }]
137
 
138
+ # Define the Chatbot component without initial value in the constructor
139
+ chatbot_component = gr.Chatbot(height=500, type='messages')
140
+
141
  demo = gr.ChatInterface(
142
  fn=predict_chat,
143
+ # Pass the pre-defined chatbot_component
144
+ chatbot=chatbot_component,
145
  textbox=gr.Textbox(
146
  placeholder="Ask me a question...",
147
  container=False,
 
164
  clear_btn="Clear Chat"
165
  )
166
 
167
+ # NEW LINE: Set the initial value after the demo (ChatInterface) is created
168
+ # This specifically targets the internal chatbot component that ChatInterface uses.
169
+ demo.chatbot.value = initial_messages_for_value
170
+
171
  demo.launch()