Spaces:
Sleeping
Sleeping
Quincy Hsieh commited on
Commit ·
53cab5b
1
Parent(s): f66fdd0
Add team yml and endpoint LLM models
Browse filesFiles changed (5)
- README.md+150-58
- app.py+42-19
- config.json+4
- requirements.txt-1
- team.ymlAdded+6
110 unmodified lines111112113114115116117218 unmodified lines3363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244 unmodified lines42943043143243343443571 unmodified lines507508509510511512110 unmodified lines```pythonimport chromadbclient = chromadb.PersistentClient(path="./chroma_db")collection = client.get_or_create_collection(name="rag_documents",metadata={"hnsw:space": "cosine"},218 unmodified lines## Adding Binary Files to the HF SpaceLarge or binary files (e.g., pre-built ChromaDB databases, PDFs, images, model weights) cannot be pushed with regular `git push` because Hugging Face repositories enforce a **10 MB file-size limit** for standard Git. Use one of the methods below to make them available to your Space at runtime.### Method 1: Git LFS (recommended for files < 5 GB)Hugging Face repos support [Git Large File Storage (LFS)](https://git-lfs.com/). Files tracked by LFS are stored in a dedicated bucket and downloaded transparently when the Space starts.```bash# 1. Install Git LFS (once per machine)git lfs install# 2. Track the binary patterns you needgit lfs track "*.pdf"git lfs track "*.sqlite3"git lfs track "chroma_db/**"git lfs track "DataSet/**"# This updates .gitattributes — commit itgit add .gitattributesgit commit -m "chore: track binary files with Git LFS"# 3. Add and push the binary filesgit add chroma_db/ DataSet/git commit -m "feat: add pre-built vector store and datasets"git push```> **Verify:** After pushing, open your Space repo on huggingface.co and click on an LFS file — it should show `Stored with Git LFS` instead of raw content.### Method 2: Upload via the `huggingface_hub` Python SDKUse this when you want to upload files programmatically (e.g., from a CI pipeline or a notebook) without cloning the full repo.```pythonfrom huggingface_hub import HfApiapi = HfApi()# Upload a single fileapi.upload_file(path_or_fileobj="./chroma_db/chroma.sqlite3",path_in_repo="chroma_db/chroma.sqlite3",repo_id="YOUR_USERNAME/Example-App-Hackathon-Gustave-Eiffel-2026",repo_type="space",)# Upload an entire folderapi.upload_folder(folder_path="./DataSet",path_in_repo="DataSet",repo_id="YOUR_USERNAME/Example-App-Hackathon-Gustave-Eiffel-2026",repo_type="space",)```> **Authentication:** Set `HF_TOKEN` in your environment or call `huggingface_hub.login()` first. The token needs **write** access to the Space.### Method 3: Hugging Face Hub UI (quick one-off uploads)1. Open your Space repository on huggingface.co2. Navigate to **Files and versions**3. Click **Add file → Upload files**4. Drag-and-drop your binary files (up to 50 GB per file via the UI)5. Commit directly to `main`### Accessing Uploaded Files in Your ApplicationOnce binary files are in the repo (via any method above), they are available at their relative path inside the Space container:```pythonimport chromadb# Files land at the repo root, same relative path you committed them atclient = chromadb.PersistentClient(path="./chroma_db")```No extra download logic is needed — Hugging Face clones the full repo (including LFS objects) into the Space container on startup.### Tips- **`.gitattributes`** is the source of truth for LFS tracking. Verify your patterns are listed there before pushing.- For files **> 5 GB**, prefer uploading via the Hub UI or the SDK (Git LFS CLI may time out on slow connections).- If you update a binary file, push the new version the same way — LFS handles versioning automatically.- To exclude files from the Space build (e.g., test data not needed at runtime), add them to `.gitignore` instead.---4 unmodified lines| `HF_TOKEN` | (Space Secret) | Hugging Face API token for Inference API || `EMBEDDING_MODEL_NAME` | `sentence-transformers/all-MiniLM-L6-v2` | Model for text embeddings || `LLM_MODEL_NAME` | `Qwen/Qwen2.5-72B-Instruct` | LLM for answer generation || `CHROMA_PERSIST_DIR` | `./chroma_db` | ChromaDB storage path || `CHUNK_SIZE` | `512` | Text chunk size in characters || `CHUNK_OVERLAP` | `50` | Overlap between chunks || `TOP_K_RESULTS` | `3` | Number of context chunks to retrieve |71 unmodified lines> **How to export your corporate CA certificate:**> Open the failing URL (`https://huggingface.co`) in your browser, click the padlock icon → View Certificate → export the root CA as a `.crt` / `.pem` file, then point `REQUESTS_CA_BUNDLE` to that file.---## License110 unmodified lines111112113114115116117218 unmodified lines3363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364 unmodified lines44144244344444544644744844971 unmodified lines521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604110 unmodified lines```pythonimport chromadbclient = chromadb.PersistentClient(path="./data/chroma_db")collection = client.get_or_create_collection(name="rag_documents",metadata={"hnsw:space": "cosine"},218 unmodified lines## Adding Binary Files to the HF SpaceLarge or binary files (PDFs, pre-built ChromaDB databases, datasets, model weights) are stored in a **Hugging Face bucket** and mounted into the Space container at `/data`. The `hf sync` command keeps your local folder in sync with the bucket.> **Note:** Git LFS is not supported for Hugging Face Spaces persistent storage. Use the bucket + `hf sync` workflow described here instead.### Prerequisites```bash# Install the hf CLI (requires uv)uv tool install "huggingface_hub[cli]"# Authenticate (needs Write access to the bucket)export HF_TOKEN="hf_your_token_here"# or interactively:hf auth login```### Step 1 — Attach the Storage Bucket to Your Space1. Open your Space on huggingface.co2. Go to **Settings → Persistent Storage**3. Under **"Attach storage"**, select the existing bucket **`millimanfrance/Example-App-Hackathon-Gustave-Eiffel-2026-storage`** (or create a new one)4. Save — Hugging Face will mount the bucket at `/data` inside the Space container### Step 2 — Upload Files from Your Local MachinePlace all persistent files under a local `./data` folder. The expected structure is:```./data/├── chroma_db/ # Pre-built ChromaDB vector store│ └── chroma.sqlite3├── sample_documents/ # Text/PDF files ingested on startup│ ├── eiffel_tower.txt│ └── gustave_eiffel.txt└── DataSet/ # Training/test datasets (PDFs, CSVs, etc.)├── Automobile/├── Climatique/└── ...```Then push:```bash# Sync the entire ./data folder to the buckethf sync ./data hf://buckets/millimanfrance/Example-App-Hackathon-Gustave-Eiffel-2026-storage# Sync only a specific sub-folder (e.g., the vector store)hf sync ./data/chroma_db hf://buckets/millimanfrance/Example-App-Hackathon-Gustave-Eiffel-2026-storage/chroma_db# Sync only specific file typeshf sync ./data hf://buckets/millimanfrance/Example-App-Hackathon-Gustave-Eiffel-2026-storage \--include "*.pdf" --include "*.sqlite3"````hf sync` is **incremental** — it computes checksums and only uploads files that have changed.### Step 3 — Download the Bucket to Another MachineTo pull the latest bucket contents back to a local `./data` folder (e.g., on a new dev machine):```bashhf sync hf://buckets/millimanfrance/Example-App-Hackathon-Gustave-Eiffel-2026-storage ./data```### Accessing Files in the Space ApplicationOnce the bucket is mounted, files appear under `/data` inside the Space container regardless of their path in the bucket.The application resolves this automatically via a single `DATA_DIR` constant in `app.py`:```pythonfrom pathlib import Path# /data when running in HF Spaces (bucket mount), ./data for local devDATA_DIR = Path("/data") if Path("/data").is_dir() else Path("./data")CHROMA_PERSIST_DIR = str(DATA_DIR / "chroma_db")SAMPLE_DOCS_DIR = DATA_DIR / "sample_documents"```All persistent data — the vector store, sample documents, datasets — lives under `DATA_DIR` so a single `hf sync ./data ...` covers everything.### Useful `hf sync` Flags| Flag | Description ||---|---|| `--include "*.pdf"` | Only sync files matching the pattern || `--exclude "*.tmp"` | Skip files matching the pattern || `--delete` | Remove files in the destination that no longer exist in the source || `--dry-run` | Preview what would be transferred without actually doing it |```bash# Preview before committinghf sync ./data hf://buckets/millimanfrance/Example-App-Hackathon-Gustave-Eiffel-2026-storage --dry-run```---4 unmodified lines| `HF_TOKEN` | (Space Secret) | Hugging Face API token for Inference API || `EMBEDDING_MODEL_NAME` | `sentence-transformers/all-MiniLM-L6-v2` | Model for text embeddings || `LLM_MODEL_NAME` | `Qwen/Qwen2.5-72B-Instruct` | LLM for answer generation || `DATA_DIR` | `/data` (HF Spaces) or `./data` (local) | Root directory for all persistent data || `CHROMA_PERSIST_DIR` | `DATA_DIR/chroma_db` | ChromaDB storage path || `SAMPLE_DOCS_DIR` | `DATA_DIR/sample_documents` | Documents ingested on startup || `CHUNK_SIZE` | `512` | Text chunk size in characters || `CHUNK_OVERLAP` | `50` | Overlap between chunks || `TOP_K_RESULTS` | `3` | Number of context chunks to retrieve |71 unmodified lines> **How to export your corporate CA certificate:**> Open the failing URL (`https://huggingface.co`) in your browser, click the padlock icon → View Certificate → export the root CA as a `.crt` / `.pem` file, then point `REQUESTS_CA_BUNDLE` to that file.### `hf sync` 401 Unauthorized ErrorIf `hf sync` fails with:```Error: Client error '401 Unauthorized' for url'https://huggingface.co/api/buckets/.../tree?recursive=true'Invalid username or password.```the `hf` CLI has not been authenticated. Log in with your Hugging Face token:```bashhf auth login# Paste your token when prompted (needs read + write access to the bucket)```Or set the token as an environment variable so the CLI picks it up automatically without an interactive prompt:```bashexport HF_TOKEN="hf_your_token_here"hf sync ./data hf://buckets/millimanfrance/Example-App-Hackathon-Gustave-Eiffel-2026-storage```> **Generate a token:** Go to huggingface.co → Settings → Access Tokens → New token. Select **Write** role so the CLI can both read and upload to the bucket.> **Check current login state:**> ```bash> hf auth whoami> ```---### `hf sync` SSL Certificate Verification ErrorThe `hf` CLI (installed via `uv tool install huggingface_hub`) uses `httpx` internally instead of `requests`, so it ignores `REQUESTS_CA_BUNDLE`. If `hf sync` fails with:```httpcore.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed:unable to get local issuer certificate```you must set the certificate bundle through the variables that `httpx` (and the underlying `ssl` module) respects:```bash# Option A — point to your corporate CA bundle file (recommended)export SSL_CERT_FILE=/path/to/corporate-ca.crtexport REQUESTS_CA_BUNDLE=/path/to/corporate-ca.crt # keep this too for other toolshf sync ./data hf://buckets/millimanfrance/Example-App-Hackathon-Gustave-Eiffel-2026-storage# Option B — append your CA cert to the system bundle and point therecat /path/to/corporate-ca.crt >> /etc/ssl/certs/ca-certificates.crtexport SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt````SSL_CERT_FILE` overrides the default CA store for Python's `ssl` module, which `httpx` / `httpcore` uses directly.**If you need to set these variables permanently** (e.g., in a shared dev environment), add them to your shell profile:```bash# ~/.bashrc or ~/.zshrcexport SSL_CERT_FILE=/path/to/corporate-ca.crtexport REQUESTS_CA_BUNDLE=/path/to/corporate-ca.crt```> **Finding your corporate CA cert on Linux:**> ```bash> # List all trusted CAs and look for your company's entry> awk -v cmd='openssl x509 -noout -subject' '/BEGIN CERT/{close(cmd)}; {print | cmd}' \> /etc/ssl/certs/ca-certificates.crt | grep -i "your-company-name">> # Or export the cert directly from the proxy> echo | openssl s_client -connect huggingface.co:443 -showcerts 2>/dev/null \> | openssl x509 -outform PEM > /tmp/hf-chain.pem> export SSL_CERT_FILE=/tmp/hf-chain.pem> ```---## License
31 unmodified lines323334353637387 unmodified lines46474849505152535455565758596061626364656667686970717273747576777879808182838485868764 unmodified lines152153154155156157158159160161162163164146 unmodified lines31131231331431531631731831 unmodified linesfrom pydantic import BaseModelimport chromadbfrom chromadb.config import Settingsfrom sentence_transformers import SentenceTransformerfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom pypdf import PdfReader7 unmodified lines# Configuration# ---------------------------------------------------------------------------EMBEDDING_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"CHROMA_PERSIST_DIR = "./chroma_db"COLLECTION_NAME = "rag_documents"CHUNK_SIZE = 512CHUNK_OVERLAP = 50TOP_K_RESULTS = 3# LLM settings loaded from config.json_CONFIG_PATH = Path(__file__).parent / "config.json"with open(_CONFIG_PATH, encoding="utf-8") as _f:_config = json.load(_f)LLM_ENDPOINT_URL = _config["llm"]["endpoint_url"]LLM_MODEL_NAME = _config["llm"]["model"]LLM_MAX_TOKENS = _config["llm"].get("max_tokens", 512)LLM_TEMPERATURE = _config["llm"].get("temperature", 0.7)LLM_TOP_P = _config["llm"].get("top_p", 0.95)# Azure Foundry API key from environment variableAZURE_API_KEY = os.environ.get("AZURE_API_KEY")if not AZURE_API_KEY:logger.warning("AZURE_API_KEY is not set — LLM calls will fail.")# Prompt template loaded from file so it can be edited without touching application code_PROMPT_TEMPLATE_PATH = Path(__file__).parent / "prompts" / "rag_prompt.txt"RAG_PROMPT_TEMPLATE = _PROMPT_TEMPLATE_PATH.read_text(encoding="utf-8")# ---------------------------------------------------------------------------# Step 1: Initialize the Embedding Model# ---------------------------------------------------------------------------# We use sentence-transformers to convert text chunks into dense vector embeddings.# The model runs locally inside the Space — no external API call needed.logger.info(f"Loading embedding model: {EMBEDDING_MODEL_NAME}")embedding_model = SentenceTransformer(EMBEDDING_MODEL_NAME)logger.info("Embedding model loaded successfully.")# ---------------------------------------------------------------------------# Step 2: Initialize the Vector Store (ChromaDB)64 unmodified linesdef generate_embeddings(texts: list[str]) -> list[list[float]]:"""Transform text chunks into vector embeddings using sentence-transformers.This demonstrates Step 2a: How to convert documents into embeddings.The model maps each text string to a 384-dimensional dense vector."""embeddings = embedding_model.encode(texts, show_progress_bar=False)return embeddings.tolist()def add_documents_to_vectorstore(documents: list[dict]) -> int:146 unmodified lines# ---------------------------------------------------------------------------# Load sample documents so the demo works out of the box.SAMPLE_DOCS_DIR = Path("./sample_documents")def ingest_sample_documents():"""Load and embed sample documents into the vector store on first run."""31 unmodified lines3233343536377 unmodified lines4546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929364 unmodified lines158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189146 unmodified lines33633733833934034131 unmodified linesfrom pydantic import BaseModelimport chromadbfrom chromadb.config import Settingsfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom pypdf import PdfReader7 unmodified lines# Configuration# ---------------------------------------------------------------------------# Resolve the data directory: /data when running inside HF Spaces (bucket mount),# ./data for local development.DATA_DIR = Path("/data") if Path("/data").is_dir() else Path("./data")CHROMA_PERSIST_DIR = str(DATA_DIR / "chroma_db")SAMPLE_DOCS_DIR = DATA_DIR / "sample_documents"COLLECTION_NAME = "rag_documents"CHUNK_SIZE = 512CHUNK_OVERLAP = 50TOP_K_RESULTS = 3# Settings loaded from config.json_CONFIG_PATH = Path(__file__).parent / "config.json"with open(_CONFIG_PATH, encoding="utf-8") as _f:_config = json.load(_f)# Embedding model (Azure OpenAI)EMBEDDING_ENDPOINT_URL = _config["embedding"]["endpoint_url"]EMBEDDING_MODEL_NAME = _config["embedding"]["model"]# LLM (Azure OpenAI)LLM_ENDPOINT_URL = _config["llm"]["endpoint_url"]LLM_MODEL_NAME = _config["llm"]["model"]LLM_MAX_TOKENS = _config["llm"].get("max_tokens", 512)LLM_TEMPERATURE = _config["llm"].get("temperature", 0.7)LLM_TOP_P = _config["llm"].get("top_p", 0.95)# Azure API key from environment variable (shared by both LLM and embedding endpoints)AZURE_API_KEY = os.environ.get("AZURE_API_KEY")if not AZURE_API_KEY:logger.warning("AZURE_API_KEY is not set — LLM and embedding calls will fail.")# Prompt template loaded from file so it can be edited without touching application code_PROMPT_TEMPLATE_PATH = Path(__file__).parent / "prompts" / "rag_prompt.txt"RAG_PROMPT_TEMPLATE = _PROMPT_TEMPLATE_PATH.read_text(encoding="utf-8")# ---------------------------------------------------------------------------# Step 1: Embedding via Azure OpenAI# ---------------------------------------------------------------------------# Embeddings are generated by calling the Azure OpenAI /embeddings endpoint.# No local model is loaded — the API handles all inference.logger.info(f"Embedding model configured: {EMBEDDING_MODEL_NAME} via Azure OpenAI")# ---------------------------------------------------------------------------# Step 2: Initialize the Vector Store (ChromaDB)64 unmodified linesdef generate_embeddings(texts: list[str]) -> list[list[float]]:"""Generate vector embeddings via the Azure OpenAI /embeddings endpoint.The endpoint, model name, and API key are loaded from config.jsonand the AZURE_API_KEY environment variable."""headers = {"api-key": AZURE_API_KEY,"Content-Type": "application/json",}payload = {"input": texts,"model": EMBEDDING_MODEL_NAME,}try:resp = http_requests.post(EMBEDDING_ENDPOINT_URL, headers=headers, json=payload, timeout=60,)resp.raise_for_status()data = resp.json()return [item["embedding"] for item in data["data"]]except http_requests.exceptions.HTTPError as e:logger.error(f"Embedding API call failed: {e} — {resp.text}")raise HTTPException(status_code=503, detail=f"Embedding service unavailable: {str(e)}")except (KeyError, IndexError) as e:logger.error(f"Unexpected embedding response format: {e}")raise HTTPException(status_code=502, detail="Unexpected response from embedding service")def add_documents_to_vectorstore(documents: list[dict]) -> int:146 unmodified lines# ---------------------------------------------------------------------------# Load sample documents so the demo works out of the box.def ingest_sample_documents():"""Load and embed sample documents into the vector store on first run."""
1234{"llm": {"endpoint_url": "https://<your-resource>.openai.azure.com/openai/deployments/<your-deployment>/chat/completions?api-version=2024-12-01-preview","model": "gpt-5",12345678{"embedding": {"endpoint_url": "https://<your-resource>.openai.azure.com/openai/deployments/<your-embedding-deployment>/embeddings?api-version=2024-12-01-preview","model": "text-embedding-3-small"},"llm": {"endpoint_url": "https://<your-resource>.openai.azure.com/openai/deployments/<your-deployment>/chat/completions?api-version=2024-12-01-preview","model": "gpt-5",
1 unmodified line23456781 unmodified lineuvicorn==0.30.0gradio==4.44.0chromadb==0.5.0sentence-transformers==3.0.0huggingface-hub==0.25.0langchain==0.3.0langchain-community==0.3.01 unmodified line2345671 unmodified lineuvicorn==0.30.0gradio==4.44.0chromadb==0.5.0huggingface-hub==0.25.0langchain==0.3.0langchain-community==0.3.0
123456display_name: "Sample Team Name"members: - name: "Quincy" email: "quincy@example.com" - name: "Alice" email: "alice@example.com"README.md+150-58
app.py+42-19
config.json+4
requirements.txt-1
team.ymlAdded+6