Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -198,7 +198,133 @@ def create_interface():
|
|
198 |
preset_creative = gr.Button("β¨ Creative Assistant")
|
199 |
preset_technical = gr.Button("π§ Technical Writer")
|
200 |
|
|
|
|
|
|
|
|
|
201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
-
|
|
|
|
|
|
|
|
|
|
198 |
preset_creative = gr.Button("β¨ Creative Assistant")
|
199 |
preset_technical = gr.Button("π§ Technical Writer")
|
200 |
|
201 |
+
#### Event Handling
|
202 |
+
def handle_api_key(api_key):
|
203 |
+
status = chatbot.set_api_key(api_key)
|
204 |
+
return status
|
205 |
|
206 |
+
# Connect events
|
207 |
+
api_key_input.change(
|
208 |
+
handle_api_key,
|
209 |
+
inputs=[api_key_input],
|
210 |
+
outputs=[api_status]
|
211 |
+
)
|
212 |
+
|
213 |
+
def handle_chat(user_input, history): #user query, chat history
|
214 |
+
if not user_input.strip():
|
215 |
+
return history or [], ""
|
216 |
+
|
217 |
+
response, updated_history = chatbot.generate_response(user_input, history or [])
|
218 |
+
return updated_history, ""
|
219 |
+
|
220 |
+
send_btn.click(
|
221 |
+
handle_chat,
|
222 |
+
inputs=[user_input, chatbot_interface],
|
223 |
+
outputs=[chatbot_interface, user_input]
|
224 |
+
)
|
225 |
+
|
226 |
+
user_input.submit(
|
227 |
+
handle_chat,
|
228 |
+
inputs=[user_input, chatbot_interface],
|
229 |
+
outputs=[chatbot_interface, user_input]
|
230 |
+
)
|
231 |
+
|
232 |
+
def handle_settings_update(model, system_prompt, max_tokens, temperature):
|
233 |
+
status = chatbot.update_settings(model, system_prompt, max_tokens, temperature)
|
234 |
+
settings_display = f"Model: {model}\nTokens: {max_tokens}\nTemp: {temperature}"
|
235 |
+
return status, settings_display
|
236 |
+
|
237 |
+
update_settings_btn.click(
|
238 |
+
handle_settings_update,
|
239 |
+
inputs=[model_dropdown, system_prompt_input, max_tokens_slider, temperature_slider],
|
240 |
+
outputs=[settings_status, current_settings]
|
241 |
+
)
|
242 |
+
|
243 |
+
def handle_data_integration(custom_data):
|
244 |
+
status = chatbot.preprocess_data(custom_data)
|
245 |
+
return status
|
246 |
+
|
247 |
+
integrate_data_btn.click(
|
248 |
+
handle_data_integration,
|
249 |
+
inputs=[custom_data_input],
|
250 |
+
outputs=[settings_status]
|
251 |
+
)
|
252 |
+
|
253 |
+
def handle_clear():
|
254 |
+
return chatbot.clear_conversation()
|
255 |
+
|
256 |
+
clear_btn.click(
|
257 |
+
handle_clear,
|
258 |
+
outputs=[user_input, chatbot_interface]
|
259 |
+
)
|
260 |
+
|
261 |
+
def handle_regenerate(history):
|
262 |
+
if not history:
|
263 |
+
return history or []
|
264 |
|
265 |
+
last_user_msg = history[-1][0] ### what is the national bird of Bangladesh? ask again.... Ans: Dove.... Q. what is the national bird of Bangladesh? Ans: Doel
|
266 |
+
history_without_last = history[:-1]
|
267 |
+
response, updated_history = chatbot.generate_response(last_user_msg, history_without_last)
|
268 |
+
return updated_history
|
269 |
+
|
270 |
+
regenerate_btn.click(
|
271 |
+
handle_regenerate,
|
272 |
+
inputs=[chatbot_interface],
|
273 |
+
outputs=[chatbot_interface]
|
274 |
+
)
|
275 |
+
|
276 |
+
def update_settings_display(model, max_tokens, temperature):
|
277 |
+
return f"Model: {model}\nTokens: {max_tokens}\nTemp: {temperature}"
|
278 |
+
|
279 |
+
for component in [model_dropdown, max_tokens_slider, temperature_slider]:
|
280 |
+
component.change(
|
281 |
+
update_settings_display,
|
282 |
+
inputs=[model_dropdown, max_tokens_slider, temperature_slider],
|
283 |
+
outputs=[current_settings]
|
284 |
+
)
|
285 |
+
|
286 |
+
def reset_prompt():
|
287 |
+
default_prompt = "You are a helpful AI assistant. Respond in a friendly and informative manner."
|
288 |
+
return default_prompt, "β
System prompt reset to default"
|
289 |
+
|
290 |
+
reset_prompt_btn.click(
|
291 |
+
reset_prompt,
|
292 |
+
outputs=[system_prompt_input, settings_status]
|
293 |
+
)
|
294 |
+
|
295 |
+
def load_preset_prompt(preset_type):
|
296 |
+
presets = {
|
297 |
+
"customer_support": "You are a helpful customer support representative. You are friendly, professional, and knowledgeable. Always try to resolve customer issues and provide clear solutions. If you cannot solve a problem, escalate it politely. Always give complete responses.",
|
298 |
+
"tutor": "You are an experienced tutor. Explain concepts clearly, use examples, and encourage students when they struggle. Break down complex problems into smaller, manageable steps. Always check for understanding. Always give complete responses.",
|
299 |
+
"creative": "You are a creative writing assistant who helps with stories, poems, and creative content. Provide constructive feedback, suggest improvements, and inspire creativity while maintaining quality standards. Always give complete responses.",
|
300 |
+
"technical": "You are a technical writer who creates clear, concise documentation. Use precise language, provide examples when relevant, and structure information logically for developers and technical users. Always give complete responses."
|
301 |
+
}
|
302 |
+
return presets.get(preset_type, ""), f"β
Loaded {preset_type.replace('_', ' ').title()} preset"
|
303 |
+
|
304 |
+
preset_customer_support.click(
|
305 |
+
lambda: load_preset_prompt("customer_support"),
|
306 |
+
outputs=[system_prompt_input, settings_status]
|
307 |
+
)
|
308 |
+
|
309 |
+
preset_tutor.click(
|
310 |
+
lambda: load_preset_prompt("tutor"),
|
311 |
+
outputs=[system_prompt_input, settings_status]
|
312 |
+
)
|
313 |
+
|
314 |
+
preset_creative.click(
|
315 |
+
lambda: load_preset_prompt("creative"),
|
316 |
+
outputs=[system_prompt_input, settings_status]
|
317 |
+
)
|
318 |
+
|
319 |
+
preset_technical.click(
|
320 |
+
lambda: load_preset_prompt("technical"),
|
321 |
+
outputs=[system_prompt_input, settings_status]
|
322 |
+
)
|
323 |
+
|
324 |
+
return demo
|
325 |
|
326 |
+
if __name__ == "__main__":
|
327 |
+
demo = create_interface()
|
328 |
+
demo.launch(
|
329 |
+
share=True
|
330 |
+
)
|