Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -352,36 +352,49 @@ class OpenRouterLLM:
|
|
| 352 |
|
| 353 |
|
| 354 |
class UserSession:
|
| 355 |
-
def __init__(self):
|
| 356 |
self.current_user = None
|
| 357 |
self.welcome_message = None
|
| 358 |
self.conversation_history = [] # Add conversation history storage
|
| 359 |
-
|
|
|
|
| 360 |
def set_user(self, user_info):
|
| 361 |
self.current_user = user_info
|
| 362 |
self.set_welcome_message(user_info.get("Nickname", "Guest"))
|
| 363 |
# Initialize conversation history with welcome message
|
| 364 |
welcome = self.get_welcome_message()
|
| 365 |
-
#initial_message = (" "
|
| 366 |
-
#)
|
| 367 |
self.conversation_history = [
|
| 368 |
{"role": "assistant", "content": welcome},
|
| 369 |
-
#{"role": "assistant", "content": initial_message}
|
| 370 |
]
|
| 371 |
-
|
| 372 |
def get_user(self):
|
| 373 |
return self.current_user
|
| 374 |
-
|
| 375 |
def set_welcome_message(self, Nickname):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
self.welcome_message = (
|
| 377 |
f"<div style='font-size: 24px; font-weight: bold; color: #2E86C1;'>"
|
| 378 |
f"Welcome {Nickname}! 👋</div>"
|
| 379 |
-
f"<div style='font-size: 20px'>"
|
| 380 |
-
f"
|
| 381 |
-
f"Here, you can find guidance on gender-based violence (GBV) and legal assistance.<br><br>"
|
| 382 |
f"</div>"
|
| 383 |
)
|
| 384 |
-
|
| 385 |
def get_welcome_message(self):
|
| 386 |
return self.welcome_message
|
| 387 |
|
|
@@ -401,8 +414,9 @@ class UserSession:
|
|
| 401 |
formatted_history += f"{role}: {entry['content']}\n\n"
|
| 402 |
return formatted_history
|
| 403 |
|
| 404 |
-
|
| 405 |
-
|
|
|
|
| 406 |
|
| 407 |
# Store user details and handle session
|
| 408 |
def collect_user_info(Nickname):
|
|
@@ -581,7 +595,7 @@ def chatbot_interface():
|
|
| 581 |
with gr.Blocks() as demo:
|
| 582 |
# User registration section
|
| 583 |
with gr.Column(visible=True, elem_id="registration_container") as registration_container:
|
| 584 |
-
gr.Markdown("### Your privacy
|
| 585 |
|
| 586 |
with gr.Row():
|
| 587 |
first_name = gr.Textbox(
|
|
@@ -605,7 +619,7 @@ def chatbot_interface():
|
|
| 605 |
)
|
| 606 |
|
| 607 |
# Footer with version info
|
| 608 |
-
gr.Markdown("Ijwi ry'
|
| 609 |
|
| 610 |
# Handle user registration
|
| 611 |
submit_btn.click(
|
|
|
|
| 352 |
|
| 353 |
|
| 354 |
class UserSession:
|
| 355 |
+
def __init__(self, llm: OpenRouterLLM): # Accept an instance of OpenRouterLLM
|
| 356 |
self.current_user = None
|
| 357 |
self.welcome_message = None
|
| 358 |
self.conversation_history = [] # Add conversation history storage
|
| 359 |
+
self.llm = llm # Store the LLM instance
|
| 360 |
+
|
| 361 |
def set_user(self, user_info):
|
| 362 |
self.current_user = user_info
|
| 363 |
self.set_welcome_message(user_info.get("Nickname", "Guest"))
|
| 364 |
# Initialize conversation history with welcome message
|
| 365 |
welcome = self.get_welcome_message()
|
|
|
|
|
|
|
| 366 |
self.conversation_history = [
|
| 367 |
{"role": "assistant", "content": welcome},
|
|
|
|
| 368 |
]
|
| 369 |
+
|
| 370 |
def get_user(self):
|
| 371 |
return self.current_user
|
| 372 |
+
|
| 373 |
def set_welcome_message(self, Nickname):
|
| 374 |
+
"""Set a dynamic welcome message using the OpenRouterLLM."""
|
| 375 |
+
# Define a prompt for the LLM to generate a welcome message
|
| 376 |
+
prompt = (
|
| 377 |
+
f"Create a very brief welcome message for {Nickname} that fits in 3 lines. "
|
| 378 |
+
f"The message should: "
|
| 379 |
+
f"1. Welcome {Nickname} warmly and professionally. "
|
| 380 |
+
f"2. Emphasize that this is a safe and trusted space. "
|
| 381 |
+
f"3. Highlight specialized support for gender-based violence (GBV) and legal assistance. "
|
| 382 |
+
f"4. Use a tone that is warm, reassuring, and professional. "
|
| 383 |
+
f"5. Keep the message concise and impactful, ensuring it fits within the character limit."
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
# Use the OpenRouterLLM to generate the message
|
| 387 |
+
welcome_text = "".join(self.llm.stream(prompt)) # Stream and concatenate the response
|
| 388 |
+
|
| 389 |
+
# Format the message with HTML styling
|
| 390 |
self.welcome_message = (
|
| 391 |
f"<div style='font-size: 24px; font-weight: bold; color: #2E86C1;'>"
|
| 392 |
f"Welcome {Nickname}! 👋</div>"
|
| 393 |
+
f"<div style='font-size: 20px;'>"
|
| 394 |
+
f"{welcome_text}"
|
|
|
|
| 395 |
f"</div>"
|
| 396 |
)
|
| 397 |
+
|
| 398 |
def get_welcome_message(self):
|
| 399 |
return self.welcome_message
|
| 400 |
|
|
|
|
| 414 |
formatted_history += f"{role}: {entry['content']}\n\n"
|
| 415 |
return formatted_history
|
| 416 |
|
| 417 |
+
api_key =api
|
| 418 |
+
llm_instance = OpenRouterLLM(key=api_key)
|
| 419 |
+
user_session = UserSession(llm_instance)
|
| 420 |
|
| 421 |
# Store user details and handle session
|
| 422 |
def collect_user_info(Nickname):
|
|
|
|
| 595 |
with gr.Blocks() as demo:
|
| 596 |
# User registration section
|
| 597 |
with gr.Column(visible=True, elem_id="registration_container") as registration_container:
|
| 598 |
+
gr.Markdown("### Your privacy matters to us! Just share a nickname you feel comfy with to start chatting..")
|
| 599 |
|
| 600 |
with gr.Row():
|
| 601 |
first_name = gr.Textbox(
|
|
|
|
| 619 |
)
|
| 620 |
|
| 621 |
# Footer with version info
|
| 622 |
+
gr.Markdown("Ijwi ry'Ubufasha Chatbot v1.0.1 © 2025")
|
| 623 |
|
| 624 |
# Handle user registration
|
| 625 |
submit_btn.click(
|