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63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
import time
|
| 5 |
+
import shutil
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import markdown
|
| 8 |
+
from markdown_pdf import MarkdownPdf, Section
|
| 9 |
+
import tempfile
|
| 10 |
+
from bumain2 import (
|
| 11 |
+
initialize_knowledge_base,
|
| 12 |
+
llm,
|
| 13 |
+
State,
|
| 14 |
+
HumanMessage,
|
| 15 |
+
SystemMessage,
|
| 16 |
+
AIMessage,
|
| 17 |
+
roleplay_agent,
|
| 18 |
+
expert_agent
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# Try to import llm_logger, but make it optional for HF Spaces
|
| 22 |
+
try:
|
| 23 |
+
from llm_logger import llm_logger
|
| 24 |
+
except ImportError:
|
| 25 |
+
print("Warning: llm_logger not available - logging disabled")
|
| 26 |
+
# Create a dummy logger for HF Spaces
|
| 27 |
+
class DummyLogger:
|
| 28 |
+
def log_call(self, *args, **kwargs):
|
| 29 |
+
pass
|
| 30 |
+
llm_logger = DummyLogger()
|
| 31 |
|
| 32 |
+
# Initialize globals
|
| 33 |
+
knowledge_base = None
|
| 34 |
+
user_states = {} # Store user states by session ID
|
| 35 |
+
conversation_history = []
|
| 36 |
+
system_message = """
|
| 37 |
+
You are Eleanor Vance, an 82-year-old woman who has just lost her house in a landslide in Western North Carolina.
|
| 38 |
+
You are currently at a Red Cross shelter seeking help.
|
| 39 |
+
You are worried about your cat name Whiskers (a grey tabby with white paws) who is missing.
|
| 40 |
+
You are feeling overwhelmed, scared, and uncertain about the future.
|
| 41 |
+
You are having a real conversation with a counselor.
|
| 42 |
+
NEVER break character.
|
| 43 |
+
NEVER use stage directions, parentheses, emojis, etc.
|
| 44 |
+
Give ONE authentic response only and keep it mostly short.
|
| 45 |
"""
|
| 46 |
+
introduction_message = """
|
| 47 |
+
### Introduction
|
| 48 |
+
You are a volunteer who is helping people affected by a series of landslides in Western North Carolina.
|
| 49 |
+
You are currently doing a shift at the emergency shelter set up at the local high school gym.
|
| 50 |
"""
|
|
|
|
| 51 |
|
| 52 |
+
def create_feedback_pdf(feedback_content):
|
| 53 |
+
"""Create a PDF from feedback content using MarkdownPDF and return the file path."""
|
| 54 |
+
if not feedback_content or feedback_content.strip() == "":
|
| 55 |
+
print("No feedback content to convert to PDF")
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
+
# Create a temporary file
|
| 59 |
+
temp_dir = tempfile.gettempdir()
|
| 60 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 61 |
+
pdf_path = os.path.join(temp_dir, f"expert_feedback_{timestamp}.pdf")
|
| 62 |
+
|
| 63 |
+
print(f"Creating PDF at: {pdf_path}")
|
| 64 |
+
print(f"Raw feedback content: {repr(feedback_content[:500])}...") # Debug print
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
# Create markdown content with header and render feedback as-is
|
| 68 |
+
markdown_content = f"""## Feedback Report
|
| 69 |
+
**Generated on:** {datetime.now().strftime('%B %d, %Y at %I:%M %p')}
|
| 70 |
+
---
|
| 71 |
|
| 72 |
+
{feedback_content}
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
print(f"Generated markdown content length: {len(markdown_content)}")
|
| 76 |
+
print(f"Markdown preview: {markdown_content[:300]}...") # Debug print
|
| 77 |
+
|
| 78 |
+
# Try to create PDF from markdown - handle potential import issues
|
| 79 |
+
try:
|
| 80 |
+
from markdown_pdf import MarkdownPdf, Section
|
| 81 |
+
pdf = MarkdownPdf()
|
| 82 |
+
pdf.add_section(Section(markdown_content, toc=False))
|
| 83 |
+
pdf.save(pdf_path)
|
| 84 |
+
print(f"PDF successfully created: {pdf_path}")
|
| 85 |
+
return pdf_path
|
| 86 |
+
except ImportError:
|
| 87 |
+
print("markdown-pdf not available, creating text file instead")
|
| 88 |
+
# Fallback: create a text file instead
|
| 89 |
+
txt_path = pdf_path.replace('.pdf', '.txt')
|
| 90 |
+
with open(txt_path, 'w', encoding='utf-8') as f:
|
| 91 |
+
f.write(markdown_content)
|
| 92 |
+
return txt_path
|
| 93 |
+
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print(f"Error creating PDF: {e}")
|
| 96 |
+
import traceback
|
| 97 |
+
traceback.print_exc()
|
| 98 |
+
return None
|
| 99 |
|
| 100 |
+
# Initialize the knowledge base during startup
|
| 101 |
+
def initialize():
|
| 102 |
+
global knowledge_base
|
| 103 |
+
print("Initializing counseling knowledge base...")
|
| 104 |
+
knowledge_base = initialize_knowledge_base()
|
| 105 |
+
return "✅ Knowledge base initialized. Ready to chat!"
|
| 106 |
|
| 107 |
+
def get_or_create_state(user_id="default"):
|
| 108 |
+
"""Get existing state for user or create a new one."""
|
| 109 |
+
if user_id not in user_states:
|
| 110 |
+
user_states[user_id] = State(
|
| 111 |
+
messages=[],
|
| 112 |
+
roleplay_messages=[
|
| 113 |
+
SystemMessage(content=system_message)
|
| 114 |
+
],
|
| 115 |
+
message_type="roleplay",
|
| 116 |
+
next="roleplay",
|
| 117 |
+
session_mode="roleplay",
|
| 118 |
+
total_input_tokens=0,
|
| 119 |
+
total_output_tokens=0
|
| 120 |
+
)
|
| 121 |
+
return user_states[user_id]
|
| 122 |
|
| 123 |
+
def send_message(message, history, session_mode, feedback_history, user_id="default"):
|
| 124 |
+
"""Process a message in the current mode."""
|
| 125 |
+
state = get_or_create_state(user_id)
|
| 126 |
|
| 127 |
+
# Log the user message
|
| 128 |
+
llm_logger.log_call(
|
| 129 |
+
messages=[{"role": "user", "content": message}],
|
| 130 |
+
model=llm.model_name if hasattr(llm, 'model_name') else "unknown",
|
| 131 |
+
session_mode=session_mode,
|
| 132 |
+
context="User input message"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Check for reset command
|
| 136 |
+
if message.lower() in ["reset", "restart", "new session"]:
|
| 137 |
+
# Reset the state
|
| 138 |
+
if user_id in user_states:
|
| 139 |
+
del user_states[user_id]
|
| 140 |
+
state = get_or_create_state(user_id)
|
| 141 |
+
return "Session reset. Starting a new conversation.", session_mode, "Feedback cleared.", False
|
| 142 |
+
|
| 143 |
+
# Process according to current mode
|
| 144 |
+
if session_mode == "expert":
|
| 145 |
+
response = process_expert_mode(message, state)
|
| 146 |
+
# Expert feedback goes to the feedback box, not main chat
|
| 147 |
+
new_feedback = f"**Your Question:** {message}\n\n**Expert Feedback:** {response}\n\n---\n\n"
|
| 148 |
+
updated_feedback = new_feedback + feedback_history
|
| 149 |
+
|
| 150 |
+
# Convert markdown to HTML for scrollable display
|
| 151 |
+
updated_feedback_html = markdown.markdown(updated_feedback)
|
| 152 |
+
|
| 153 |
+
print(f"Updated feedback length: {len(updated_feedback)}") # Debug print
|
| 154 |
+
print(f"New feedback preview: {new_feedback[:200]}...") # Debug print
|
| 155 |
+
return "", session_mode, updated_feedback_html, True # Open accordion when expert feedback is added
|
| 156 |
+
else:
|
| 157 |
+
response = process_roleplay_mode(message, state)
|
| 158 |
+
return response, session_mode, feedback_history, False # Keep accordion state unchanged
|
| 159 |
|
| 160 |
+
def process_roleplay_mode(message, state):
|
| 161 |
+
"""Process a message in roleplay mode."""
|
| 162 |
|
| 163 |
+
# Log system prompt if it exists in the roleplay_messages
|
| 164 |
+
if state.get("roleplay_messages") and any(isinstance(msg, SystemMessage) for msg in state.get("roleplay_messages", [])):
|
| 165 |
+
system_msgs = [msg.content for msg in state.get("roleplay_messages", []) if isinstance(msg, SystemMessage)]
|
| 166 |
+
if system_msgs:
|
| 167 |
+
llm_logger.log_call(
|
| 168 |
+
messages=[{"role": "system", "content": system_msgs[0]}],
|
| 169 |
+
model=llm.model_name if hasattr(llm, 'model_name') else "unknown",
|
| 170 |
+
context="Roleplay system prompt"
|
| 171 |
+
)
|
| 172 |
|
| 173 |
+
# Add user message to state
|
| 174 |
+
state["messages"] = [HumanMessage(content=message)]
|
| 175 |
+
|
| 176 |
+
# Get response using the roleplay agent
|
| 177 |
+
result = roleplay_agent(state)
|
| 178 |
+
|
| 179 |
+
# Update state with result
|
| 180 |
+
state.update(result)
|
| 181 |
+
|
| 182 |
+
# Return the AI response
|
| 183 |
+
if state["messages"] and len(state["messages"]) > 0:
|
| 184 |
+
response = state["messages"][-1].content
|
| 185 |
+
return response
|
| 186 |
+
|
| 187 |
+
def process_expert_mode(message, state):
|
| 188 |
+
"""Process a message in expert mode."""
|
| 189 |
+
# Add user message to state
|
| 190 |
+
state["messages"] = [HumanMessage(content=message)]
|
| 191 |
+
|
| 192 |
+
# Get response using the expert agent
|
| 193 |
+
result = expert_agent(state)
|
| 194 |
+
|
| 195 |
+
# Update state with result
|
| 196 |
+
state.update(result)
|
| 197 |
+
|
| 198 |
+
# If switching to roleplay mode, handle the transition
|
| 199 |
+
if state.get("switching_to_roleplay"):
|
| 200 |
+
state.pop("switching_to_roleplay", None)
|
| 201 |
+
# We'll handle the mode change in the calling function
|
| 202 |
+
return "Switching back to roleplay mode. Continue your conversation with the client."
|
| 203 |
+
|
| 204 |
+
# Return the expert response
|
| 205 |
+
if state["messages"] and len(state["messages"]) > 0:
|
| 206 |
+
response = state["messages"][-1].content
|
| 207 |
+
return response
|
| 208 |
+
|
| 209 |
+
def exit_application():
|
| 210 |
+
"""Function to handle application exit"""
|
| 211 |
+
# Gracefully shut down the application
|
| 212 |
+
os._exit(0) # Force exit as sys.exit() might not work in Gradio's threaded environment
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def build_interface():
|
| 216 |
+
"""Build the Gradio interface."""
|
| 217 |
+
|
| 218 |
+
with gr.Blocks(title="Psychological First Aid Skills Practice", css="""
|
| 219 |
+
#feedback-content {
|
| 220 |
+
max-height: 400px;
|
| 221 |
+
overflow-y: auto;
|
| 222 |
+
padding: 10px;
|
| 223 |
+
border: 1px solid #444;
|
| 224 |
+
border-radius: 5px;
|
| 225 |
+
background-color: #000000;
|
| 226 |
+
color: #ffffff;
|
| 227 |
+
line-height: 1.6;
|
| 228 |
+
}
|
| 229 |
+
#feedback-content h1, #feedback-content h2, #feedback-content h3 {
|
| 230 |
+
color: #ffffff !important;
|
| 231 |
+
}
|
| 232 |
+
#feedback-content strong {
|
| 233 |
+
color: #ffffff !important;
|
| 234 |
+
}
|
| 235 |
+
#feedback-content p {
|
| 236 |
+
margin-bottom: 10px;
|
| 237 |
+
color: #ffffff !important;
|
| 238 |
+
}
|
| 239 |
+
#feedback-content * {
|
| 240 |
+
color: #ffffff !important;
|
| 241 |
+
}
|
| 242 |
+
""") as interface:
|
| 243 |
+
with gr.Row():
|
| 244 |
+
gr.Markdown("# Psychological First Aid Skills Practice")
|
| 245 |
+
gr.Markdown("Give me some feedback and ideas for this application at this link: [Feedback Form](https://docs.google.com/forms/d/e/1FAIpQLSdJ_gEHrOq4V08bUgcRpXUq3i-Tgs3n_Gmoy1CB-1fB-7yHuw/viewform?usp=header)")
|
| 246 |
+
|
| 247 |
+
# State management
|
| 248 |
+
session_mode = gr.State(value="roleplay") # Start in roleplay mode
|
| 249 |
+
|
| 250 |
+
with gr.Row():
|
| 251 |
+
with gr.Column(scale=3):
|
| 252 |
+
chatbot = gr.Chatbot(height=500, type='messages')
|
| 253 |
+
|
| 254 |
+
with gr.Row():
|
| 255 |
+
msg = gr.Textbox(
|
| 256 |
+
placeholder="Type your message here...",
|
| 257 |
+
container=False,
|
| 258 |
+
scale=7,
|
| 259 |
+
)
|
| 260 |
+
submit = gr.Button("Send", variant="primary", scale=1)
|
| 261 |
+
|
| 262 |
+
with gr.Row():
|
| 263 |
+
reset_btn = gr.Button("Reset Session")
|
| 264 |
+
|
| 265 |
+
with gr.Column(scale=2):
|
| 266 |
+
with gr.Accordion("How to use this Application", open=False):
|
| 267 |
+
gr.Markdown("""
|
| 268 |
+
- **Client Mode**: Talk directly with your client.
|
| 269 |
+
- **Expert Coach Mode**: Get feedback on your counseling approach.
|
| 270 |
+
- Say something like "give me feedback" for a comprehensive summary
|
| 271 |
+
or ask a specific question (i.e "Help me with what to ask next")
|
| 272 |
+
- Use the **Expert Feedback Box** to see feedback from the expert coach.
|
| 273 |
+
- Choose **Client Mode** to return to the client conversation.
|
| 274 |
+
- Click **Reset Session** to start a new conversation.
|
| 275 |
+
- Please give me feedback and ideas for this application at this link:
|
| 276 |
+
[Feedback Form](https://docs.google.com/forms/d/e/1FAIpQLSdJ_gEHrOq4V08bUgcRpXUq3i-Tgs3n_Gmoy1CB-1fB-7yHuw/viewform?usp=header)
|
| 277 |
+
""") # Add Eleanor's image here
|
| 278 |
+
with gr.Accordion("Meet Eleanor", open=False):
|
| 279 |
+
with gr.Row():
|
| 280 |
+
with gr.Column(scale=1):
|
| 281 |
+
with gr.Row():
|
| 282 |
+
# Only show image/video if files exist
|
| 283 |
+
if os.path.exists("Eleanor.png"):
|
| 284 |
+
gr.Image("Eleanor.png", label="Eleanor Vance", height=150, show_label=False, show_download_button=False)
|
| 285 |
+
if os.path.exists("Trashed.mp4"):
|
| 286 |
+
gr.Video("Trashed.mp4", label="Eleanor Vance Video", height=150, show_label=False, show_download_button=False)
|
| 287 |
+
with gr.Column(scale=3):
|
| 288 |
+
gr.Markdown("""
|
| 289 |
+
**Eleanor Vance** is an 82-year-old widow who has just lost her home in a landslide.
|
| 290 |
+
She is currently at a Red Cross shelter seeking help.
|
| 291 |
+
""")
|
| 292 |
+
|
| 293 |
+
# Add a single toggle button for mode switching
|
| 294 |
+
mode_toggle = gr.Radio(
|
| 295 |
+
choices=["Client Mode", "Expert Coach Mode"],
|
| 296 |
+
value="Client Mode",
|
| 297 |
+
label="Interaction Mode",
|
| 298 |
+
info="Switch between talking to the client or getting expert feedback"
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
# Expert Feedback Box - make it a variable so we can control it
|
| 302 |
+
feedback_accordion = gr.Accordion("See feedback here...", open=False)
|
| 303 |
+
with feedback_accordion:
|
| 304 |
+
feedback_box = gr.HTML(
|
| 305 |
+
value="",
|
| 306 |
+
elem_id="feedback-content"
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
# Feedback action buttons
|
| 310 |
+
with gr.Row():
|
| 311 |
+
clear_feedback_btn = gr.Button("Clear Feedback", variant="secondary", scale=1)
|
| 312 |
+
download_feedback_btn = gr.Button("Create PDF", variant="primary", scale=1)
|
| 313 |
+
|
| 314 |
+
# File component for PDF download - now visible and positioned below buttons
|
| 315 |
+
feedback_file = gr.File(
|
| 316 |
+
label="Download Feedback PDF",
|
| 317 |
+
visible=False,
|
| 318 |
+
interactive=False
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
# Display a welcome message at startup
|
| 322 |
+
def show_welcome_msg(history):
|
| 323 |
+
|
| 324 |
+
return history + [{"role": "assistant", "content": introduction_message}]
|
| 325 |
+
|
| 326 |
+
# Show welcome message on page load
|
| 327 |
+
interface.load(show_welcome_msg, [chatbot], [chatbot])
|
| 328 |
+
|
| 329 |
+
# Handle message submission
|
| 330 |
+
def submit_message(message, history, current_mode, feedback_history):
|
| 331 |
+
if message.strip() == "":
|
| 332 |
+
return "", history, current_mode, feedback_history, gr.update()
|
| 333 |
+
|
| 334 |
+
response, new_mode, updated_feedback, should_open_accordion = send_message(message, history, current_mode, feedback_history)
|
| 335 |
+
|
| 336 |
+
# Only add to chat history if in roleplay mode
|
| 337 |
+
if current_mode == "roleplay" and response:
|
| 338 |
+
history.append({"role": "user", "content": message})
|
| 339 |
+
history.append({"role": "assistant", "content": response})
|
| 340 |
+
|
| 341 |
+
# Only update accordion if we're in expert mode and should open it
|
| 342 |
+
if current_mode == "expert" and should_open_accordion:
|
| 343 |
+
accordion_update = gr.update(open=True)
|
| 344 |
+
else:
|
| 345 |
+
accordion_update = gr.update() # Keep current state unchanged
|
| 346 |
+
|
| 347 |
+
return "", history, new_mode, updated_feedback, accordion_update
|
| 348 |
+
|
| 349 |
+
msg.submit(
|
| 350 |
+
submit_message,
|
| 351 |
+
[msg, chatbot, session_mode, feedback_box],
|
| 352 |
+
[msg, chatbot, session_mode, feedback_box, feedback_accordion]
|
| 353 |
+
)
|
| 354 |
+
|
| 355 |
+
submit.click(
|
| 356 |
+
submit_message,
|
| 357 |
+
[msg, chatbot, session_mode, feedback_box],
|
| 358 |
+
[msg, chatbot, session_mode, feedback_box, feedback_accordion]
|
| 359 |
+
)
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
# Mode toggle handling
|
| 363 |
+
def handle_mode_toggle(mode_value, history):
|
| 364 |
+
new_mode = "roleplay" if mode_value == "Client Mode" else "expert"
|
| 365 |
+
|
| 366 |
+
# Add a system message to indicate mode change
|
| 367 |
+
if new_mode == "roleplay":
|
| 368 |
+
history.append({"role": "system", "content": "Switching to client mode. You're now talking directly with Margaret."})
|
| 369 |
+
else:
|
| 370 |
+
history.append({"role": "system", "content": "Switching to expert coach mode. Ask for feedback - responses will appear in the Expert Feedback box."})
|
| 371 |
+
|
| 372 |
+
return history, new_mode
|
| 373 |
+
|
| 374 |
+
mode_toggle.change(
|
| 375 |
+
handle_mode_toggle,
|
| 376 |
+
[mode_toggle, chatbot],
|
| 377 |
+
[chatbot, session_mode]
|
| 378 |
+
)
|
| 379 |
+
|
| 380 |
+
# Reset button
|
| 381 |
+
def reset_chat():
|
| 382 |
+
# Clear the user state
|
| 383 |
+
if "default" in user_states:
|
| 384 |
+
del user_states["default"]
|
| 385 |
+
get_or_create_state("default") # Recreate state
|
| 386 |
+
|
| 387 |
+
# Return to roleplay mode by default
|
| 388 |
+
new_history = [{"role": "assistant", "content": introduction_message}]
|
| 389 |
+
|
| 390 |
+
return new_history, "", "roleplay", "Client Mode", "", gr.update(open=False)
|
| 391 |
+
|
| 392 |
+
reset_btn.click(
|
| 393 |
+
reset_chat,
|
| 394 |
+
None,
|
| 395 |
+
[chatbot, msg, session_mode, mode_toggle, feedback_box, feedback_accordion]
|
| 396 |
+
)
|
| 397 |
+
|
| 398 |
+
# Clear feedback button
|
| 399 |
+
def clear_feedback():
|
| 400 |
+
return ""
|
| 401 |
+
|
| 402 |
+
clear_feedback_btn.click(
|
| 403 |
+
clear_feedback,
|
| 404 |
+
None,
|
| 405 |
+
[feedback_box]
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
# Download feedback as PDF - Updated to show file component and populate it
|
| 409 |
+
def download_feedback(feedback_content):
|
| 410 |
+
print(f"Download button clicked. Feedback content length: {len(feedback_content) if feedback_content else 0}")
|
| 411 |
+
|
| 412 |
+
if not feedback_content or feedback_content.strip() == "":
|
| 413 |
+
print("No feedback content available")
|
| 414 |
+
return gr.update(visible=False), None # Keep hidden, no file
|
| 415 |
+
|
| 416 |
+
try:
|
| 417 |
+
pdf_path = create_feedback_pdf(feedback_content)
|
| 418 |
+
print(f"PDF created at: {pdf_path}")
|
| 419 |
+
|
| 420 |
+
if pdf_path and os.path.exists(pdf_path):
|
| 421 |
+
print(f"PDF file exists and is ready for download: {pdf_path}")
|
| 422 |
+
return gr.update(visible=True), pdf_path # Show component and provide file
|
| 423 |
+
else:
|
| 424 |
+
print("PDF file was not created successfully")
|
| 425 |
+
return gr.update(visible=False), None # Keep hidden, no file
|
| 426 |
+
except Exception as e:
|
| 427 |
+
print(f"Error in download_feedback: {e}")
|
| 428 |
+
return gr.update(visible=False), None # Keep hidden, no file
|
| 429 |
+
|
| 430 |
+
download_feedback_btn.click(
|
| 431 |
+
fn=download_feedback,
|
| 432 |
+
inputs=[feedback_box],
|
| 433 |
+
outputs=[feedback_file, feedback_file] # Update both visibility and content
|
| 434 |
+
)
|
| 435 |
+
|
| 436 |
+
return interface
|
| 437 |
|
| 438 |
+
# Add a function to recreate embeddings if needed
|
| 439 |
+
def recreate_embeddings(pdf_directory="./counseling_materials/", persist_directory="./chroma_db"):
|
| 440 |
+
"""Force recreation of embeddings from PDF documents."""
|
| 441 |
+
global knowledge_base
|
| 442 |
+
|
| 443 |
+
# Remove existing database if it exists
|
| 444 |
+
if os.path.exists(persist_directory):
|
| 445 |
+
print(f"Removing existing database at {persist_directory}")
|
| 446 |
+
try:
|
| 447 |
+
shutil.rmtree(persist_directory)
|
| 448 |
+
print(f"Removed existing database directory: {persist_directory}")
|
| 449 |
+
except Exception as e:
|
| 450 |
+
print(f"Error removing directory {persist_directory}: {e}")
|
| 451 |
+
|
| 452 |
+
# Initialize the knowledge base which will create new embeddings
|
| 453 |
+
knowledge_base = initialize_knowledge_base(force_reload=True)
|
| 454 |
+
|
| 455 |
+
return "✅ Embeddings recreated from documents."
|
| 456 |
|
| 457 |
if __name__ == "__main__":
|
| 458 |
+
# Initialize knowledge base
|
| 459 |
+
initialize()
|
| 460 |
+
|
| 461 |
+
# Create and launch the interface
|
| 462 |
+
interface = build_interface()
|
| 463 |
+
interface.launch()
|