Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -409,11 +409,32 @@ kb = None
|
|
| 409 |
|
| 410 |
def ensure_index():
|
| 411 |
"""Build index on first run or load from cache."""
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
else:
|
| 416 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
|
| 418 |
# ----------- Response Generation -----------
|
| 419 |
def format_citations(citations: List[Dict]) -> str:
|
|
@@ -500,26 +521,18 @@ def handle_file_upload(file):
|
|
| 500 |
return "ℹ️ No file uploaded.", ""
|
| 501 |
|
| 502 |
try:
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
file_path = file
|
| 506 |
-
filename = Path(file).name
|
| 507 |
-
else:
|
| 508 |
-
# Fallback for other types (e.g. temp file object)
|
| 509 |
-
file_path = getattr(file, "name", None)
|
| 510 |
-
if not file_path:
|
| 511 |
-
raise ValueError("Unsupported file object from Gradio.")
|
| 512 |
-
filename = Path(file_path).name
|
| 513 |
-
|
| 514 |
-
num_chunks, file_type = kb.build_from_uploaded_file(file_path, filename)
|
| 515 |
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
|
|
|
|
|
|
|
|
|
| 520 |
except Exception as e:
|
| 521 |
-
return f"❌ Error processing file
|
| 522 |
-
|
| 523 |
|
| 524 |
def clear_uploaded_file():
|
| 525 |
"""Clear uploaded file and reload KB index."""
|
|
@@ -666,12 +679,19 @@ def create_interface():
|
|
| 666 |
return demo
|
| 667 |
|
| 668 |
# ----------- Main Entry Point -----------
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
|
| 410 |
def ensure_index():
|
| 411 |
"""Build index on first run or load from cache."""
|
| 412 |
+
try:
|
| 413 |
+
# Try to load existing index first
|
| 414 |
+
if kb.load():
|
| 415 |
+
print(f"✅ Loaded existing index from {config.index_directory}")
|
| 416 |
+
return
|
| 417 |
+
except Exception as e:
|
| 418 |
+
print(f"⚠️ Could not load existing index: {e}")
|
| 419 |
+
|
| 420 |
+
# Try to build new index if KB directory exists and has files
|
| 421 |
+
if config.kb_directory.exists():
|
| 422 |
+
md_files = list(config.kb_directory.glob("*.md"))
|
| 423 |
+
if md_files:
|
| 424 |
+
try:
|
| 425 |
+
print(f"🔨 Building index from {len(md_files)} markdown files...")
|
| 426 |
+
kb.build(config.kb_directory)
|
| 427 |
+
print(f"✅ Index built successfully!")
|
| 428 |
+
except Exception as e:
|
| 429 |
+
print(f"⚠️ Could not build index: {e}")
|
| 430 |
+
print(f"ℹ️ You can upload documents via the UI or add .md files to {config.kb_directory}")
|
| 431 |
else:
|
| 432 |
+
print(f"ℹ️ No markdown files found in {config.kb_directory}")
|
| 433 |
+
print(f"ℹ️ Upload documents via the UI or add .md files to start using the knowledge base")
|
| 434 |
+
else:
|
| 435 |
+
print(f"ℹ️ KB directory {config.kb_directory} not found. Creating it...")
|
| 436 |
+
config.kb_directory.mkdir(exist_ok=True, parents=True)
|
| 437 |
+
print(f"ℹ️ Add .md files to {config.kb_directory} or upload documents via the UI")
|
| 438 |
|
| 439 |
# ----------- Response Generation -----------
|
| 440 |
def format_citations(citations: List[Dict]) -> str:
|
|
|
|
| 521 |
return "ℹ️ No file uploaded.", ""
|
| 522 |
|
| 523 |
try:
|
| 524 |
+
filename = Path(file.name).name
|
| 525 |
+
num_chunks, file_type = kb.build_from_uploaded_file(file.name, filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 526 |
|
| 527 |
+
return (
|
| 528 |
+
f"✅ **File processed successfully!**\n\n"
|
| 529 |
+
f"📄 **File:** {filename}\n"
|
| 530 |
+
f"📋 **Type:** {file_type}\n"
|
| 531 |
+
f"🔢 **Chunks:** {num_chunks}\n\n"
|
| 532 |
+
f"You can now ask questions about this document!"
|
| 533 |
+
), filename
|
| 534 |
except Exception as e:
|
| 535 |
+
return f"❌ **Error processing file:** {str(e)}\n\nPlease ensure the file is a valid PDF, DOCX, TXT, or MD file.", ""
|
|
|
|
| 536 |
|
| 537 |
def clear_uploaded_file():
|
| 538 |
"""Clear uploaded file and reload KB index."""
|
|
|
|
| 679 |
return demo
|
| 680 |
|
| 681 |
# ----------- Main Entry Point -----------
|
| 682 |
+
if __name__ == "__main__":
|
| 683 |
+
parser = argparse.ArgumentParser(description='Configurable RAG Assistant')
|
| 684 |
+
parser.add_argument('--config', type=str, default='config.yaml',
|
| 685 |
+
help='Path to configuration YAML file (default: config.yaml)')
|
| 686 |
+
args = parser.parse_args()
|
| 687 |
+
|
| 688 |
+
# Load configuration
|
| 689 |
+
config = Config(args.config)
|
| 690 |
+
|
| 691 |
+
# Initialize KB with config
|
| 692 |
+
kb = KBIndex()
|
| 693 |
+
ensure_index()
|
| 694 |
+
|
| 695 |
+
# Create and launch interface
|
| 696 |
+
demo = create_interface()
|
| 697 |
+
demo.launch()
|