Update buffalo_rag/api/main.py
Browse files- buffalo_rag/api/main.py +1 -33
buffalo_rag/api/main.py
CHANGED
|
@@ -13,6 +13,7 @@ from buffalo_rag.scraper.scraper import BuffaloScraper
|
|
| 13 |
from buffalo_rag.embeddings.chunker import DocumentChunker
|
| 14 |
from buffalo_rag.vector_store.db import VectorStore
|
| 15 |
from buffalo_rag.model.rag import BuffaloRAG
|
|
|
|
| 16 |
|
| 17 |
# Initialize FastAPI app
|
| 18 |
app = FastAPI(
|
|
@@ -54,39 +55,6 @@ class ScrapeResponse(BaseModel):
|
|
| 54 |
status: str
|
| 55 |
message: str
|
| 56 |
|
| 57 |
-
# Background tasks
|
| 58 |
-
def run_scraper(seed_url: str, max_pages: int):
|
| 59 |
-
"""Run the web scraper in the background."""
|
| 60 |
-
scraper = BuffaloScraper(seed_url=seed_url)
|
| 61 |
-
scraper.scrape(max_pages=max_pages)
|
| 62 |
-
|
| 63 |
-
# After scraping, update the embeddings and index
|
| 64 |
-
chunker = DocumentChunker()
|
| 65 |
-
chunks = chunker.create_chunks()
|
| 66 |
-
chunker.create_embeddings(chunks)
|
| 67 |
-
|
| 68 |
-
# Reload the vector store
|
| 69 |
-
global vector_store
|
| 70 |
-
vector_store = VectorStore()
|
| 71 |
-
|
| 72 |
-
# Update the RAG model
|
| 73 |
-
global rag
|
| 74 |
-
rag = BuffaloRAG(vector_store=vector_store)
|
| 75 |
-
|
| 76 |
-
def refresh_index():
|
| 77 |
-
"""Refresh the vector index in the background."""
|
| 78 |
-
chunker = DocumentChunker()
|
| 79 |
-
chunks = chunker.create_chunks()
|
| 80 |
-
chunker.create_embeddings(chunks)
|
| 81 |
-
|
| 82 |
-
# Reload the vector store
|
| 83 |
-
global vector_store
|
| 84 |
-
vector_store = VectorStore()
|
| 85 |
-
|
| 86 |
-
# Update the RAG model
|
| 87 |
-
global rag
|
| 88 |
-
rag = BuffaloRAG(vector_store=vector_store)
|
| 89 |
-
|
| 90 |
# Setup static files directory
|
| 91 |
static_dir = os.path.join(os.path.dirname(__file__), "static")
|
| 92 |
os.makedirs(static_dir, exist_ok=True)
|
|
|
|
| 13 |
from buffalo_rag.embeddings.chunker import DocumentChunker
|
| 14 |
from buffalo_rag.vector_store.db import VectorStore
|
| 15 |
from buffalo_rag.model.rag import BuffaloRAG
|
| 16 |
+
from buffalo_rag.api.background_tasks import run_scraper, refresh_index
|
| 17 |
|
| 18 |
# Initialize FastAPI app
|
| 19 |
app = FastAPI(
|
|
|
|
| 55 |
status: str
|
| 56 |
message: str
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# Setup static files directory
|
| 59 |
static_dir = os.path.join(os.path.dirname(__file__), "static")
|
| 60 |
os.makedirs(static_dir, exist_ok=True)
|