Spaces:
Runtime error
Runtime error
Download tests/conftest.py from capcut8128/docs-qa-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.3 kB
-
https://huggingface.co/spaces/capcut8128/docs-qa-backend/resolve/main/tests/conftest.py
- Command line
-
hf download hf://spaces/capcut8128/docs-qa-backend/tests/conftest.py
-
curl -L -o conftest.py https://huggingface.co/spaces/capcut8128/docs-qa-backend/resolve/main/tests/conftest.py
1.3 kB
| """Tests run with a fake embedder and a temp data dir: no model download, no Groq key needed.""" | |
| import os | |
| import re | |
| import tempfile | |
| import zlib | |
| _TMP = tempfile.mkdtemp(prefix="rag-test-") | |
| os.environ["DATA_DIR"] = _TMP | |
| os.environ["USE_RERANKER"] = "false" | |
| os.environ["USE_QUERY_REWRITE"] = "false" | |
| os.environ["GROQ_API_KEY"] = "test-key" | |
| import numpy as np # noqa: E402 | |
| import pytest # noqa: E402 | |
| from app import store # noqa: E402 | |
| from app.bm25 import bm25 # noqa: E402 | |
| from app.registry import get_registry # noqa: E402 | |
| class FakeEmbedder: | |
| """Hashed bag-of-words vectors: deterministic, similar words -> similar vectors.""" | |
| dim = 256 | |
| def get_sentence_embedding_dimension(self): | |
| return self.dim | |
| def encode(self, texts, normalize_embeddings=True, **kw): | |
| out = np.zeros((len(texts), self.dim), dtype="float32") | |
| for i, t in enumerate(texts): | |
| for tok in re.findall(r"\w+", t.lower()): | |
| out[i, zlib.crc32(tok.encode()) % self.dim] += 1 | |
| norms = np.linalg.norm(out, axis=1, keepdims=True) | |
| norms[norms == 0] = 1 | |
| return out / norms | |
| store.set_embedder(FakeEmbedder()) | |
| def clean_state(): | |
| store.clear_all() | |
| get_registry().clear() | |
| bm25.mark_dirty() | |
| yield | |