Spaces:
Sleeping
Sleeping
docs: expand README with a comprehensive project overview and feat: implement dynamic headless mode for the Playwright browser based on environment.
Browse files- README.md +69 -1
- nps_agent.py +7 -1
README.md
CHANGED
|
@@ -7,4 +7,72 @@ sdk: docker
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# NPS-ChatAgentBot (NPS-AI)
|
| 11 |
+
|
| 12 |
+
Welcome to the **NPS-ChatAgentBot** repository! This project (also referred to as GNDU Assistant) is a comprehensive AI solution combining a **Retrieval-Augmented Generation (RAG) Chatbot** and an **Autonomous Web Browser Agent**. It is built using Python, FastAPI, LangChain, and state-of-the-art Large Language Models (LLMs) like Mistral AI and local Ollama models.
|
| 13 |
+
|
| 14 |
+
## π Key Features
|
| 15 |
+
|
| 16 |
+
1. **RAG-Powered Chatbot**:
|
| 17 |
+
- Uses the **Mistral AI API** to answer queries accurately based on uploaded documents (PDF, DOCX, TXT, MD).
|
| 18 |
+
- Custom `EnsembleRetriever` combining Scikit-Learn's TF-IDF for keyword matching and FAISS for semantic similarity (using HuggingFace embeddings).
|
| 19 |
+
- **Multi-language Support**: Automatically translates non-English queries to English for internal processing, and translates the generated answer back to the user's language using Mistral.
|
| 20 |
+
|
| 21 |
+
2. **Autonomous Web Browser Agent**:
|
| 22 |
+
- Uses an autonomous web browsing framework (integrated via `nps_agent.py`) to search the web, navigate pages, and extract information.
|
| 23 |
+
- Powered by a local **Ollama** LLM (default model: `kimi-k2.5:cloud`), ensuring privacy and saving API costs.
|
| 24 |
+
- The agent is accessible via a dedicated `/agent/run` endpoint or via a standalone script.
|
| 25 |
+
|
| 26 |
+
3. **FastAPI Backend & Frontend Integration**:
|
| 27 |
+
- Modern, fast, and robust backend using **FastAPI**.
|
| 28 |
+
- Includes an admin API (`/admin/files`) for managing documents (upload/delete files with HTTP Basic Auth).
|
| 29 |
+
- Serves static files for the frontend user interface.
|
| 30 |
+
|
| 31 |
+
4. **Hugging Face Spaces Ready**:
|
| 32 |
+
- Contains configuration (`Dockerfile`, YAML frontmatter) for seamless deployment on Hugging Face Spaces.
|
| 33 |
+
- Intelligent data handling: Uses `/data` persistent storage on HF Spaces while defaulting to a local `data` folder during local development.
|
| 34 |
+
|
| 35 |
+
## ποΈ Project Architecture
|
| 36 |
+
|
| 37 |
+
- `app.py`: The main entry point for the FastAPI server. Features the `/ask` endpoint (for RAG/Agent/Chat), document processing, initialization of the vector store, translation wrappers, and admin routes.
|
| 38 |
+
- `mistral.py`: A standalone alternative or legacy iteration emphasizing the Mistral-based RAG logic without full agent integration.
|
| 39 |
+
- `nps_agent.py`: Contains the logic for the Autonomous Browser Agent, communicating with a local Ollama instance to perform multi-step web navigation tasks.
|
| 40 |
+
- `STARTUP.md`: A step-by-step guide to setting up and running the application, including environment setup and starting local servers.
|
| 41 |
+
- `AGENTS.md`: Instructions and behavioral guidelines governing the browsing agent's deep-research flow.
|
| 42 |
+
- `/data`: Directory storing the knowledge base files.
|
| 43 |
+
- `/frontend`: Contains the static web files (HTML, CSS, JS) forming the UI.
|
| 44 |
+
|
| 45 |
+
## π Getting Started
|
| 46 |
+
|
| 47 |
+
If you want to run this project locally, follow the detailed setup guide in [`STARTUP.md`](./STARTUP.md).
|
| 48 |
+
|
| 49 |
+
Here's the quick summary:
|
| 50 |
+
|
| 51 |
+
1. **Clone & Env Setup**:
|
| 52 |
+
Ensure you have a `.env` file containing your `MISTRAL_API_KEY`, `ADMIN_USERNAME`, and `ADMIN_PASSWORD`.
|
| 53 |
+
2. **Install Requirements**:
|
| 54 |
+
```bash
|
| 55 |
+
pip install -r requirements.txt
|
| 56 |
+
playwright install
|
| 57 |
+
```
|
| 58 |
+
3. **Start Ollama** (for the Agent):
|
| 59 |
+
```bash
|
| 60 |
+
ollama serve
|
| 61 |
+
ollama pull kimi-k2.5:cloud
|
| 62 |
+
```
|
| 63 |
+
4. **Run the Application**:
|
| 64 |
+
```bash
|
| 65 |
+
python app.py
|
| 66 |
+
```
|
| 67 |
+
*Access the Web UI at http://localhost:7860.*
|
| 68 |
+
|
| 69 |
+
## π How to Use
|
| 70 |
+
|
| 71 |
+
- **Chatbot**: Submit queries via the Web UI. If you ask about details contained in the `/data` folder, the bot will synthesize an answer and cite the source pages.
|
| 72 |
+
- **Agent Mode**: Select "agent" mode or send a request to `/agent/run` with a specific `task` (e.g., "Find the latest news about AI"). The backend invokes the browser agent to complete the task and return the result.
|
| 73 |
+
- **Document Management**: Use the secure endpoints under `/admin` to upload or delete PDFs dynamically without restarting the server.
|
| 74 |
+
|
| 75 |
+
## π οΈ Configuration Details
|
| 76 |
+
|
| 77 |
+
Hugging Face Spaces config blocks the top of this `README.md`.
|
| 78 |
+
For full configuration parameters, check https://huggingface.co/docs/hub/spaces-config-reference
|
nps_agent.py
CHANGED
|
@@ -66,9 +66,15 @@ class PlaywrightTool(Tool):
|
|
| 66 |
async def _ensure_initialized(self):
|
| 67 |
if not self._initialized:
|
| 68 |
self._playwright = await async_playwright().start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Launch with anti-detection args
|
| 70 |
self._browser = await self._playwright.chromium.launch(
|
| 71 |
-
headless=
|
| 72 |
args=[
|
| 73 |
"--no-sandbox",
|
| 74 |
"--disable-blink-features=AutomationControlled",
|
|
|
|
| 66 |
async def _ensure_initialized(self):
|
| 67 |
if not self._initialized:
|
| 68 |
self._playwright = await async_playwright().start()
|
| 69 |
+
|
| 70 |
+
# Auto-detect HF Spaces or allow HEADLESS env var override
|
| 71 |
+
is_spaces = "SPACE_ID" in os.environ
|
| 72 |
+
default_headless = "true" if is_spaces else "false"
|
| 73 |
+
is_headless = os.getenv("HEADLESS", default_headless).lower() == "true"
|
| 74 |
+
|
| 75 |
# Launch with anti-detection args
|
| 76 |
self._browser = await self._playwright.chromium.launch(
|
| 77 |
+
headless=is_headless,
|
| 78 |
args=[
|
| 79 |
"--no-sandbox",
|
| 80 |
"--disable-blink-features=AutomationControlled",
|