asyn await for cache gen
Browse files- pipeline/news_ingest.py +62 -14
pipeline/news_ingest.py
CHANGED
|
@@ -36,7 +36,48 @@ def write_articles_jsonl(articles: List[Dict], file_path: str):
|
|
| 36 |
for article in articles:
|
| 37 |
f.write(json.dumps(article, ensure_ascii=False) + "\n")
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return [
|
| 41 |
Document(
|
| 42 |
text=entry["content"],
|
|
@@ -50,16 +91,17 @@ def build_documents(data: List[Dict]) -> List[Document]:
|
|
| 50 |
for entry in data
|
| 51 |
]
|
| 52 |
|
| 53 |
-
|
|
|
|
| 54 |
if not API_KEY or not CSE_ID:
|
| 55 |
raise EnvironmentError("Missing GOOGLE_API_KEY or GOOGLE_CX_ID in environment.")
|
| 56 |
|
| 57 |
-
print("
|
| 58 |
|
| 59 |
all_articles = []
|
| 60 |
|
| 61 |
for query in QUERIES:
|
| 62 |
-
print(f"
|
| 63 |
try:
|
| 64 |
results = fetch_google_news(query, API_KEY, CSE_ID, num_results=10)
|
| 65 |
print(f" β Found {len(results)} links for '{query}'.")
|
|
@@ -71,7 +113,7 @@ if __name__ == "__main__":
|
|
| 71 |
if not url or not title:
|
| 72 |
continue
|
| 73 |
|
| 74 |
-
print(f"
|
| 75 |
article_text = scrape_url(url)
|
| 76 |
|
| 77 |
if article_text:
|
|
@@ -90,15 +132,21 @@ if __name__ == "__main__":
|
|
| 90 |
|
| 91 |
if not all_articles:
|
| 92 |
print("β οΈ No content scraped. Exiting.")
|
| 93 |
-
|
| 94 |
-
print(f"π Writing {len(all_articles)} articles to {RAW_JSON}...")
|
| 95 |
-
write_articles_jsonl(all_articles, RAW_JSON)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
get_or_build_index_from_docs(documents)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
print(f"β
Indexed, headlines generated, and stored at: {INDEX_DIR}")
|
|
|
|
| 36 |
for article in articles:
|
| 37 |
f.write(json.dumps(article, ensure_ascii=False) + "\n")
|
| 38 |
|
| 39 |
+
import sys
|
| 40 |
+
import os
|
| 41 |
+
import json
|
| 42 |
+
import asyncio
|
| 43 |
+
from typing import List, Dict
|
| 44 |
+
|
| 45 |
+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 46 |
+
|
| 47 |
+
from components.indexers.news_indexer import get_or_build_index_from_docs
|
| 48 |
+
from components.fetchers.google_search import fetch_google_news
|
| 49 |
+
from components.fetchers.scraper import scrape_url
|
| 50 |
+
from components.generators.daily_feed import generate_and_cache_daily_feed
|
| 51 |
+
from llama_index.core.settings import Settings
|
| 52 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 53 |
+
from llama_index.core.schema import Document
|
| 54 |
+
|
| 55 |
+
# β
Set up local embedding model
|
| 56 |
+
Settings.embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/paraphrase-MiniLM-L3-v2")
|
| 57 |
+
|
| 58 |
+
# π Environment variables
|
| 59 |
+
API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 60 |
+
CSE_ID = os.environ.get("GOOGLE_CX_ID")
|
| 61 |
+
|
| 62 |
+
# β
News topics to fetch
|
| 63 |
+
QUERIES = [
|
| 64 |
+
"India news", "World news", "Tech news", "Finance news", "Sports news"
|
| 65 |
+
]
|
| 66 |
+
|
| 67 |
+
# β
Paths
|
| 68 |
+
INDEX_DIR = "storage/index"
|
| 69 |
+
DATA_DIR = "data/news"
|
| 70 |
+
RAW_JSON = os.path.join(DATA_DIR, "news.jsonl")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def write_articles_jsonl(articles: List[Dict], file_path: str):
|
| 74 |
+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
| 75 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
| 76 |
+
for article in articles:
|
| 77 |
+
f.write(json.dumps(article, ensure_ascii=False) + "\n")
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
async def build_documents(data: List[Dict]) -> List[Document]:
|
| 81 |
return [
|
| 82 |
Document(
|
| 83 |
text=entry["content"],
|
|
|
|
| 91 |
for entry in data
|
| 92 |
]
|
| 93 |
|
| 94 |
+
|
| 95 |
+
async def main():
|
| 96 |
if not API_KEY or not CSE_ID:
|
| 97 |
raise EnvironmentError("Missing GOOGLE_API_KEY or GOOGLE_CX_ID in environment.")
|
| 98 |
|
| 99 |
+
print("π Fetching news URLs from Google...")
|
| 100 |
|
| 101 |
all_articles = []
|
| 102 |
|
| 103 |
for query in QUERIES:
|
| 104 |
+
print(f"π Searching for: {query}")
|
| 105 |
try:
|
| 106 |
results = fetch_google_news(query, API_KEY, CSE_ID, num_results=10)
|
| 107 |
print(f" β Found {len(results)} links for '{query}'.")
|
|
|
|
| 113 |
if not url or not title:
|
| 114 |
continue
|
| 115 |
|
| 116 |
+
print(f"π Scraping: {url}")
|
| 117 |
article_text = scrape_url(url)
|
| 118 |
|
| 119 |
if article_text:
|
|
|
|
| 132 |
|
| 133 |
if not all_articles:
|
| 134 |
print("β οΈ No content scraped. Exiting.")
|
| 135 |
+
return
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
print(f"π Writing {len(all_articles)} articles to {RAW_JSON}...")
|
| 138 |
+
write_articles_jsonl(all_articles, RAW_JSON)
|
|
|
|
| 139 |
|
| 140 |
+
print("π§ Building index...")
|
| 141 |
+
documents = await build_documents(all_articles)
|
| 142 |
+
get_or_build_index_from_docs(documents)
|
| 143 |
+
|
| 144 |
+
print("β‘ Generating daily feed...")
|
| 145 |
+
await generate_and_cache_daily_feed(documents)
|
| 146 |
+
|
| 147 |
+
print(f"β
Indexed, headlines generated, and stored at: {INDEX_DIR}")
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
if __name__ == "__main__":
|
| 151 |
+
asyncio.run(main())
|
| 152 |
|
|
|