Spaces:
Sleeping
Sleeping
Download app/cli.py from QDB95/qdb-knowledge-bot: direct link, hf CLI and curl.
- Browser
- Download file 2.4 kB
-
https://huggingface.co/spaces/QDB95/qdb-knowledge-bot/resolve/main/app/cli.py
- Command line
-
hf download hf://spaces/QDB95/qdb-knowledge-bot/app/cli.py
-
curl -L -o cli.py https://huggingface.co/spaces/QDB95/qdb-knowledge-bot/resolve/main/app/cli.py
2.4 kB
| """CLI entry point for testing the bot end-to-end before building the UI. | |
| Usage: | |
| python -m app.cli ingest # index everything in data/documents/ | |
| python -m app.cli ingest path/to/file # index one file | |
| python -m app.cli ask "your question" # ask a question | |
| python -m app.cli sources # list indexed files | |
| python -m app.cli reset <filename> # remove a file from the index | |
| """ | |
| from __future__ import annotations | |
| import sys | |
| from pathlib import Path | |
| from loguru import logger | |
| from app.ingestion.pipeline import ingest_directory, ingest_file | |
| from app.rag.generator import answer_question | |
| from app.rag.retriever import delete_source, list_sources | |
| def cmd_ingest(args: list[str]) -> None: | |
| if args: | |
| target = Path(args[0]) | |
| if target.is_file(): | |
| ingest_file(target) | |
| else: | |
| ingest_directory(target) | |
| else: | |
| ingest_directory() | |
| def cmd_ask(args: list[str]) -> None: | |
| if not args: | |
| print("Usage: python -m app.cli ask \"your question\"") | |
| sys.exit(1) | |
| question = " ".join(args) | |
| print(f"\nQ: {question}\n") | |
| ans = answer_question(question) | |
| print(f"A: {ans.text}\n") | |
| if ans.citations: | |
| print("Sources:") | |
| for i, c in enumerate(ans.citations, 1): | |
| sec = f" — {c['section']}" if c["section"] else "" | |
| print(f" {i}. {c['source_file']}{sec} (score: {c['score']})") | |
| def cmd_sources(_args: list[str]) -> None: | |
| sources = list_sources() | |
| if not sources: | |
| print("No documents indexed yet.") | |
| return | |
| print(f"\nIndexed sources ({len(sources)}):") | |
| for s in sources: | |
| print(f" - {s['source_file']}: {s['chunks']} chunks") | |
| def cmd_reset(args: list[str]) -> None: | |
| if not args: | |
| print("Usage: python -m app.cli reset <filename>") | |
| sys.exit(1) | |
| n = delete_source(args[0]) | |
| print(f"Removed {n} chunks for {args[0]}") | |
| COMMANDS = { | |
| "ingest": cmd_ingest, | |
| "ask": cmd_ask, | |
| "sources": cmd_sources, | |
| "reset": cmd_reset, | |
| } | |
| def main() -> None: | |
| if len(sys.argv) < 2 or sys.argv[1] not in COMMANDS: | |
| print(__doc__) | |
| sys.exit(1) | |
| cmd = sys.argv[1] | |
| try: | |
| COMMANDS[cmd](sys.argv[2:]) | |
| except Exception as e: | |
| logger.exception(f"Command failed: {e}") | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() | |