Spaces:
Sleeping
Sleeping
CI: sync project3-document-qa to Space
Browse files- README.md +83 -14
- UPDATE_GUIDE.md +52 -0
- app.py +45 -0
README.md
CHANGED
|
@@ -1,14 +1,83 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Project 3: Document Q&A (Gradio / Hugging Face Spaces)
|
| 2 |
+
|
| 3 |
+
[](https://huggingface.co/spaces/zhangju2023/document-qa-rag)
|
| 4 |
+
|
| 5 |
+
A RAG (Retrieval-Augmented Generation) application using:
|
| 6 |
+
- ChromaDB for vector storage
|
| 7 |
+
- SentenceTransformers for embeddings (`all-MiniLM-L6-v2`)
|
| 8 |
+
- Hugging Face Transformers for text generation (`distilgpt2`)
|
| 9 |
+
- Gradio UI
|
| 10 |
+
|
| 11 |
+
## Local Run
|
| 12 |
+
|
| 13 |
+
```bash
|
| 14 |
+
cd project3-document-qa
|
| 15 |
+
pip install -r requirements.txt
|
| 16 |
+
python app.py
|
| 17 |
+
# Then open http://localhost:7860
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
Live app: https://huggingface.co/spaces/zhangju2023/document-qa-rag
|
| 21 |
+
|
| 22 |
+
## Try it online (3 steps)
|
| 23 |
+
|
| 24 |
+
1) Open the Space
|
| 25 |
+
- https://huggingface.co/spaces/zhangju2023/document-qa-rag
|
| 26 |
+
- First load can take 1β3 minutes while models download
|
| 27 |
+
|
| 28 |
+
2) Load documents
|
| 29 |
+
- Go to the "π Manage Documents" tab
|
| 30 |
+
- Click "π₯ Load Sample AI/ML Documents" or "π₯ Load Sample HR Policy Documents" and wait for the status to confirm
|
| 31 |
+
- (Optional) Paste your own text into "Document Text" and click "β Add Document"
|
| 32 |
+
|
| 33 |
+
3) Ask a question
|
| 34 |
+
- Switch to the "π¬ Ask Questions" tab
|
| 35 |
+
- Type a question or click one of the sample buttons
|
| 36 |
+
- Adjust "Number of context documents" if you like (default: 3)
|
| 37 |
+
- Click "π Ask Question" and read the answer
|
| 38 |
+
- Expand "π Retrieved Context" to see the sources and relevance scores
|
| 39 |
+
|
| 40 |
+
Troubleshooting
|
| 41 |
+
- HR use case tips:
|
| 42 |
+
- Try questions like "What is the overtime policy?", "When is payroll processed?", or "How does PTO accrue?"
|
| 43 |
+
- Load the HR sample first to populate the knowledge base with HR/policy docs.
|
| 44 |
+
- If you see "No relevant documents", make sure you loaded sample or custom documents
|
| 45 |
+
- First inference might be slow while the model warms up; later queries are faster
|
| 46 |
+
- Click "π View Statistics" or "ποΈ Clear Knowledge Base" in the Manage tab to inspect/reset the index
|
| 47 |
+
|
| 48 |
+
## CI/CD (Auto-deploy from GitHub)
|
| 49 |
+
|
| 50 |
+
This folder is configured to auto-sync to the Hugging Face Space via GitHub Actions.
|
| 51 |
+
|
| 52 |
+
- Workflow: `.github/workflows/deploy-project3-to-hf.yml`
|
| 53 |
+
- Trigger: Any push to `main` that changes files under `project3-document-qa/**`
|
| 54 |
+
- Requirements: GitHub repo secret `HF_TOKEN` containing a Hugging Face write token
|
| 55 |
+
- Target Space: `zhangju2023/document-qa-rag`
|
| 56 |
+
|
| 57 |
+
If you just pushed changes and don't see them live yet:
|
| 58 |
+
1. Open the GitHub "Actions" tab and check the latest run for "Deploy Project 3 (RAG) to Hugging Face Space".
|
| 59 |
+
2. Wait 1β3 minutes for the Space to rebuild after the upload completes.
|
| 60 |
+
3. Hard refresh the Space page (Cmd+Shift+R) if the UI was open during the update.
|
| 61 |
+
|
| 62 |
+
## Deploy to Hugging Face Spaces
|
| 63 |
+
|
| 64 |
+
Option A β Create a new Space and upload:
|
| 65 |
+
1. Go to https://huggingface.co/spaces
|
| 66 |
+
2. New Space β Type: "Gradio" β Name: `document-qa-rag`
|
| 67 |
+
3. Upload these files from this repo:
|
| 68 |
+
- `project3-document-qa/app.py`
|
| 69 |
+
- `project3-document-qa/requirements.txt`
|
| 70 |
+
- (optional) `README.md`
|
| 71 |
+
4. Set the Space hardware to CPU Basic (free)
|
| 72 |
+
5. The app will auto-build and launch
|
| 73 |
+
|
| 74 |
+
Option B β Connect to GitHub (recommended):
|
| 75 |
+
1. Create a new Space as above
|
| 76 |
+
2. In Space settings β "Link Git Repositories"
|
| 77 |
+
3. Connect to this GitHub repo and map `/project3-document-qa` folder
|
| 78 |
+
4. Set `app.py` as the entry file
|
| 79 |
+
|
| 80 |
+
Notes:
|
| 81 |
+
- First build may take a while to download models.
|
| 82 |
+
- You can switch to a lighter LLM in `app.py` (e.g., `sshleifer/tiny-gpt2`) if needed.
|
| 83 |
+
- For production, consider larger models via Inference Endpoints or OpenAI with an API key.
|
UPDATE_GUIDE.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Update Your Hugging Face Space (RAG)
|
| 2 |
+
|
| 3 |
+
This guide helps you update your existing Space to reflect the new HR policy features (sample corpus + loader button).
|
| 4 |
+
|
| 5 |
+
## What Changed
|
| 6 |
+
|
| 7 |
+
- Added "Load Sample HR Policy Documents" in the Manage Documents tab
|
| 8 |
+
- Included curated HR policy snippets (PTO, overtime, payroll schedule, benefits, FMLA, expenses, reviews)
|
| 9 |
+
- Minor README improvements
|
| 10 |
+
|
| 11 |
+
## Files to Update in Your Space
|
| 12 |
+
|
| 13 |
+
Upload these 3 files from this repo to your Space:
|
| 14 |
+
|
| 15 |
+
1. `project3-document-qa/app.py`
|
| 16 |
+
2. `project3-document-qa/requirements.txt`
|
| 17 |
+
3. (optional) `project3-document-qa/README.md`
|
| 18 |
+
|
| 19 |
+
## Option A β Upload via Web UI (Easiest)
|
| 20 |
+
|
| 21 |
+
1. Go to your Space: https://huggingface.co/spaces/zhangju2023/document-qa-rag
|
| 22 |
+
2. Click the "Files" tab
|
| 23 |
+
3. Upload the files listed above (drag & drop or click Upload)
|
| 24 |
+
4. Commit the changes β the Space will rebuild automatically
|
| 25 |
+
5. Wait 1β3 minutes for the rebuild
|
| 26 |
+
|
| 27 |
+
## Option B β Link to GitHub (Recommended)
|
| 28 |
+
|
| 29 |
+
1. In your Space, open "Settings" β "Repository" β "Link Git Repositories"
|
| 30 |
+
2. Connect GitHub repo: `justin-mbca/enterprise-ai-workflows`
|
| 31 |
+
3. Map the subdirectory: `/project3-document-qa`
|
| 32 |
+
4. App file: `app.py` (Gradio)
|
| 33 |
+
5. Save. The Space will auto-sync on every push to `main`.
|
| 34 |
+
|
| 35 |
+
## After Update β Quick Test
|
| 36 |
+
|
| 37 |
+
In the running app:
|
| 38 |
+
|
| 39 |
+
- Go to "π Manage Documents"
|
| 40 |
+
- Click "π₯ Load Sample HR Policy Documents" β wait for success
|
| 41 |
+
- Switch to "π¬ Ask Questions" and try:
|
| 42 |
+
- "What is the overtime policy?"
|
| 43 |
+
- "When is payroll processed?"
|
| 44 |
+
- "How does PTO accrue?"
|
| 45 |
+
|
| 46 |
+
If you see "No relevant documents", ensure you loaded the HR sample first.
|
| 47 |
+
|
| 48 |
+
## Troubleshooting
|
| 49 |
+
|
| 50 |
+
- First load can take 1β3 minutes while models download
|
| 51 |
+
- If the Space errors, check Logs β look for package install or model download issues
|
| 52 |
+
- You can switch to a lighter LLM in `app.py` (e.g., `sshleifer/tiny-gpt2`) to reduce cold start time
|
app.py
CHANGED
|
@@ -265,6 +265,17 @@ SAMPLE_DOCUMENTS = [
|
|
| 265 |
"Kubernetes is an open-source container orchestration platform that automates deploying, scaling, and managing containerized applications. It groups containers into logical units for easy management and discovery, and provides tools for load balancing, rolling updates, and service discovery.",
|
| 266 |
]
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
# Helper: add sample documents with dedup by deterministic IDs
|
| 270 |
def _add_sample_documents_dedup() -> int:
|
|
@@ -319,6 +330,35 @@ def load_sample_documents():
|
|
| 319 |
return f"β
Loaded {added} sample documents. Total documents: {stats['total_documents']}"
|
| 320 |
|
| 321 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
def add_custom_document(text: str):
|
| 323 |
"""Add a custom document"""
|
| 324 |
if not text.strip():
|
|
@@ -450,12 +490,17 @@ with gr.Blocks(title="Document Q&A System", theme=gr.themes.Soft()) as demo:
|
|
| 450 |
with gr.Column():
|
| 451 |
gr.Markdown("#### Load Sample Documents")
|
| 452 |
load_sample_btn = gr.Button("π₯ Load Sample AI/ML Documents", variant="primary")
|
|
|
|
| 453 |
load_status = gr.Textbox(label="Status", interactive=False)
|
| 454 |
|
| 455 |
load_sample_btn.click(
|
| 456 |
load_sample_documents,
|
| 457 |
outputs=load_status
|
| 458 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
|
| 460 |
with gr.Column():
|
| 461 |
gr.Markdown("#### Add Custom Document")
|
|
|
|
| 265 |
"Kubernetes is an open-source container orchestration platform that automates deploying, scaling, and managing containerized applications. It groups containers into logical units for easy management and discovery, and provides tools for load balancing, rolling updates, and service discovery.",
|
| 266 |
]
|
| 267 |
|
| 268 |
+
# HR/Payroll policy sample documents for domain-specific demo
|
| 269 |
+
HR_SAMPLE_DOCUMENTS = [
|
| 270 |
+
"Paid Time Off (PTO): Full-time employees accrue 1.67 days of PTO per month (20 days/year). Unused PTO carries over up to 5 days. PTO requests must be submitted at least 2 weeks in advance via the HR portal.",
|
| 271 |
+
"Overtime Policy: Non-exempt hourly employees are eligible for overtime pay at 1.5x the regular rate for hours worked over 40 in a workweek. Overtime must be pre-approved by a manager.",
|
| 272 |
+
"Payroll Schedule: Employees are paid bi-weekly on Fridays. Direct deposit is required. Payroll cut-off is Tuesday 5pm for the current pay period.",
|
| 273 |
+
"Benefits Eligibility: Employees working 30+ hours per week are eligible for medical, dental, and vision plans starting on the first day of the month following 30 days of employment.",
|
| 274 |
+
"Leave of Absence: FMLA provides up to 12 weeks of unpaid, job-protected leave for eligible employees. Employees must provide 30 days' notice when foreseeable and submit required documentation.",
|
| 275 |
+
"Expense Reimbursement: Business expenses must be submitted within 30 days with itemized receipts. Reimbursements are processed in the next payroll cycle upon approval.",
|
| 276 |
+
"Performance Reviews: Formal performance reviews occur annually in Q4 with mid-year checkpoints. Salary adjustments, if any, are effective in the first payroll of Q1."
|
| 277 |
+
]
|
| 278 |
+
|
| 279 |
|
| 280 |
# Helper: add sample documents with dedup by deterministic IDs
|
| 281 |
def _add_sample_documents_dedup() -> int:
|
|
|
|
| 330 |
return f"β
Loaded {added} sample documents. Total documents: {stats['total_documents']}"
|
| 331 |
|
| 332 |
|
| 333 |
+
def load_sample_hr_documents():
|
| 334 |
+
"""Load HR/policy sample documents into the system (idempotent)"""
|
| 335 |
+
# Build deterministic IDs for HR docs
|
| 336 |
+
ids = [f"hr_doc_{i}" for i in range(len(HR_SAMPLE_DOCUMENTS))]
|
| 337 |
+
metadata = [{"source": ids[i], "domain": "hr", "topic": "HR/Payroll"} for i in range(len(HR_SAMPLE_DOCUMENTS))]
|
| 338 |
+
|
| 339 |
+
# Determine which ones already exist
|
| 340 |
+
existing_ids = set()
|
| 341 |
+
try:
|
| 342 |
+
got = qa_system.collection.get(ids=ids)
|
| 343 |
+
if got and isinstance(got.get("ids", None), list):
|
| 344 |
+
existing_ids = set(got["ids"]) # existing subset
|
| 345 |
+
except Exception:
|
| 346 |
+
existing_ids = set()
|
| 347 |
+
|
| 348 |
+
to_add_idx = [i for i, _id in enumerate(ids) if _id not in existing_ids]
|
| 349 |
+
if not to_add_idx:
|
| 350 |
+
stats = qa_system.get_collection_stats()
|
| 351 |
+
return f"βΉοΈ HR policy documents already loaded. Total documents: {stats['total_documents']}"
|
| 352 |
+
|
| 353 |
+
docs = [HR_SAMPLE_DOCUMENTS[i] for i in to_add_idx]
|
| 354 |
+
metas = [metadata[i] for i in to_add_idx]
|
| 355 |
+
sel_ids = [ids[i] for i in to_add_idx]
|
| 356 |
+
|
| 357 |
+
qa_system.add_documents(docs, metas, ids=sel_ids)
|
| 358 |
+
stats = qa_system.get_collection_stats()
|
| 359 |
+
return f"β
Loaded {len(docs)} HR policy documents. Total documents: {stats['total_documents']}"
|
| 360 |
+
|
| 361 |
+
|
| 362 |
def add_custom_document(text: str):
|
| 363 |
"""Add a custom document"""
|
| 364 |
if not text.strip():
|
|
|
|
| 490 |
with gr.Column():
|
| 491 |
gr.Markdown("#### Load Sample Documents")
|
| 492 |
load_sample_btn = gr.Button("π₯ Load Sample AI/ML Documents", variant="primary")
|
| 493 |
+
load_hr_btn = gr.Button("π₯ Load Sample HR Policy Documents")
|
| 494 |
load_status = gr.Textbox(label="Status", interactive=False)
|
| 495 |
|
| 496 |
load_sample_btn.click(
|
| 497 |
load_sample_documents,
|
| 498 |
outputs=load_status
|
| 499 |
)
|
| 500 |
+
load_hr_btn.click(
|
| 501 |
+
load_sample_hr_documents,
|
| 502 |
+
outputs=load_status
|
| 503 |
+
)
|
| 504 |
|
| 505 |
with gr.Column():
|
| 506 |
gr.Markdown("#### Add Custom Document")
|