god knows
Browse files- components/generators/daily_feed.py +19 -13
- pipeline/news_ingest.py +10 -51
components/generators/daily_feed.py
CHANGED
|
@@ -12,30 +12,34 @@ from llama_index.core.query_engine import RetrieverQueryEngine
|
|
| 12 |
from llama_index.core.schema import Document
|
| 13 |
from llama_index.core.settings import Settings
|
| 14 |
|
| 15 |
-
# β
Disable
|
| 16 |
Settings.llm = None
|
| 17 |
|
| 18 |
-
# π
|
| 19 |
REDIS_URL = os.environ.get("UPSTASH_REDIS_URL", "redis://localhost:6379")
|
| 20 |
REDIS_KEY = os.environ.get("UPSTASH_REDIS_TOKEN")
|
| 21 |
-
MISTRAL_URL = os.environ.get("MISTRAL_URL") #
|
| 22 |
-
HF_TOKEN = os.environ.get("HF_TOKEN") # Hugging Face
|
| 23 |
|
| 24 |
-
# β
|
| 25 |
redis_client = redis.Redis.from_url(REDIS_URL, decode_responses=True)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
TOPICS = ["India news", "World news", "Tech news", "Finance news", "Sports news"]
|
| 29 |
|
| 30 |
-
#
|
| 31 |
def build_prompt(content: str, topic: str) -> str:
|
| 32 |
return (
|
| 33 |
f"You are a news summarizer. Summarize the following content in 25-30 words. "
|
| 34 |
f"Make it engaging and informative. Include appropriate emojis. Topic: {topic}\n\n{content}"
|
| 35 |
)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
def call_mistral(prompt: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
headers = {
|
| 40 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 41 |
"Content-Type": "application/json"
|
|
@@ -45,6 +49,7 @@ def call_mistral(prompt: str) -> str:
|
|
| 45 |
{"role": "user", "content": prompt}
|
| 46 |
]
|
| 47 |
}
|
|
|
|
| 48 |
try:
|
| 49 |
response = requests.post(MISTRAL_URL, headers=headers, json=payload, timeout=20)
|
| 50 |
response.raise_for_status()
|
|
@@ -53,7 +58,7 @@ def call_mistral(prompt: str) -> str:
|
|
| 53 |
print(f"β οΈ Mistral error: {e}")
|
| 54 |
return None
|
| 55 |
|
| 56 |
-
# βοΈ Summarize
|
| 57 |
def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
|
| 58 |
feed = []
|
| 59 |
for doc in docs[:5]:
|
|
@@ -67,30 +72,31 @@ def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
|
|
| 67 |
})
|
| 68 |
return feed
|
| 69 |
|
| 70 |
-
#
|
| 71 |
def generate_and_cache_daily_feed(documents: List[Document]):
|
| 72 |
index = VectorStoreIndex.from_documents(documents)
|
| 73 |
retriever = index.as_retriever()
|
| 74 |
query_engine = RetrieverQueryEngine(retriever=retriever)
|
| 75 |
|
| 76 |
final_feed = []
|
|
|
|
| 77 |
for topic in TOPICS:
|
| 78 |
print(f"\nπ Generating for: {topic}")
|
| 79 |
response = query_engine.query(topic)
|
| 80 |
docs = [str(node.get_content()) for node in response.source_nodes]
|
| 81 |
-
|
| 82 |
topic_feed = summarize_topic(docs, topic)
|
| 83 |
final_feed.append({
|
| 84 |
"topic": topic.lower().replace(" news", ""),
|
| 85 |
"feed": topic_feed
|
| 86 |
})
|
| 87 |
|
| 88 |
-
# πΎ Cache
|
| 89 |
redis_client.set(REDIS_KEY, json.dumps(final_feed, ensure_ascii=False))
|
| 90 |
print(f"β
Cached daily feed under key '{REDIS_KEY}'")
|
| 91 |
return final_feed
|
| 92 |
|
| 93 |
-
#
|
| 94 |
def get_cached_daily_feed():
|
| 95 |
cached = redis_client.get(REDIS_KEY)
|
| 96 |
return json.loads(cached) if cached else []
|
|
|
|
| 12 |
from llama_index.core.schema import Document
|
| 13 |
from llama_index.core.settings import Settings
|
| 14 |
|
| 15 |
+
# β
Disable implicit LLM usage (prevents OpenAI fallback)
|
| 16 |
Settings.llm = None
|
| 17 |
|
| 18 |
+
# π Environment variables
|
| 19 |
REDIS_URL = os.environ.get("UPSTASH_REDIS_URL", "redis://localhost:6379")
|
| 20 |
REDIS_KEY = os.environ.get("UPSTASH_REDIS_TOKEN")
|
| 21 |
+
MISTRAL_URL = os.environ.get("MISTRAL_URL") # Hugging Face endpoint
|
| 22 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") # Hugging Face token
|
| 23 |
|
| 24 |
+
# β
Redis client
|
| 25 |
redis_client = redis.Redis.from_url(REDIS_URL, decode_responses=True)
|
| 26 |
|
| 27 |
+
# π° Topics
|
| 28 |
TOPICS = ["India news", "World news", "Tech news", "Finance news", "Sports news"]
|
| 29 |
|
| 30 |
+
# βοΈ Build summarization prompt
|
| 31 |
def build_prompt(content: str, topic: str) -> str:
|
| 32 |
return (
|
| 33 |
f"You are a news summarizer. Summarize the following content in 25-30 words. "
|
| 34 |
f"Make it engaging and informative. Include appropriate emojis. Topic: {topic}\n\n{content}"
|
| 35 |
)
|
| 36 |
|
| 37 |
+
# π§ Call Mistral via Hugging Face endpoint
|
| 38 |
def call_mistral(prompt: str) -> str:
|
| 39 |
+
if not prompt or len(prompt.strip()) < 10:
|
| 40 |
+
print(f"β οΈ Skipping empty or invalid prompt:\n{prompt}\n")
|
| 41 |
+
return None
|
| 42 |
+
|
| 43 |
headers = {
|
| 44 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 45 |
"Content-Type": "application/json"
|
|
|
|
| 49 |
{"role": "user", "content": prompt}
|
| 50 |
]
|
| 51 |
}
|
| 52 |
+
|
| 53 |
try:
|
| 54 |
response = requests.post(MISTRAL_URL, headers=headers, json=payload, timeout=20)
|
| 55 |
response.raise_for_status()
|
|
|
|
| 58 |
print(f"β οΈ Mistral error: {e}")
|
| 59 |
return None
|
| 60 |
|
| 61 |
+
# βοΈ Summarize documents for a given topic
|
| 62 |
def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
|
| 63 |
feed = []
|
| 64 |
for doc in docs[:5]:
|
|
|
|
| 72 |
})
|
| 73 |
return feed
|
| 74 |
|
| 75 |
+
# π Main pipeline: generate and cache feed
|
| 76 |
def generate_and_cache_daily_feed(documents: List[Document]):
|
| 77 |
index = VectorStoreIndex.from_documents(documents)
|
| 78 |
retriever = index.as_retriever()
|
| 79 |
query_engine = RetrieverQueryEngine(retriever=retriever)
|
| 80 |
|
| 81 |
final_feed = []
|
| 82 |
+
|
| 83 |
for topic in TOPICS:
|
| 84 |
print(f"\nπ Generating for: {topic}")
|
| 85 |
response = query_engine.query(topic)
|
| 86 |
docs = [str(node.get_content()) for node in response.source_nodes]
|
| 87 |
+
|
| 88 |
topic_feed = summarize_topic(docs, topic)
|
| 89 |
final_feed.append({
|
| 90 |
"topic": topic.lower().replace(" news", ""),
|
| 91 |
"feed": topic_feed
|
| 92 |
})
|
| 93 |
|
| 94 |
+
# πΎ Cache in Redis
|
| 95 |
redis_client.set(REDIS_KEY, json.dumps(final_feed, ensure_ascii=False))
|
| 96 |
print(f"β
Cached daily feed under key '{REDIS_KEY}'")
|
| 97 |
return final_feed
|
| 98 |
|
| 99 |
+
# π¦ For API or debugging
|
| 100 |
def get_cached_daily_feed():
|
| 101 |
cached = redis_client.get(REDIS_KEY)
|
| 102 |
return json.loads(cached) if cached else []
|
pipeline/news_ingest.py
CHANGED
|
@@ -1,43 +1,5 @@
|
|
| 1 |
-
import sys
|
| 2 |
import os
|
| 3 |
-
import json
|
| 4 |
-
from typing import List, Dict
|
| 5 |
-
|
| 6 |
-
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
| 7 |
-
|
| 8 |
-
from components.indexers.news_indexer import get_or_build_index_from_docs
|
| 9 |
-
from components.fetchers.google_search import fetch_google_news
|
| 10 |
-
from components.fetchers.scraper import scrape_url
|
| 11 |
-
from components.generators.daily_feed import generate_and_cache_daily_feed
|
| 12 |
-
from llama_index.core.settings import Settings
|
| 13 |
-
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 14 |
-
from llama_index.core.schema import Document
|
| 15 |
-
|
| 16 |
-
# β
Set up local embedding model
|
| 17 |
-
Settings.embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/paraphrase-MiniLM-L3-v2")
|
| 18 |
-
|
| 19 |
-
# π Environment variables
|
| 20 |
-
API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 21 |
-
CSE_ID = os.environ.get("GOOGLE_CX_ID") # β
fixed typo
|
| 22 |
-
|
| 23 |
-
# β
News topics to fetch
|
| 24 |
-
QUERIES = [
|
| 25 |
-
"India news", "World news", "Tech news", "Finance news", "Sports news"
|
| 26 |
-
]
|
| 27 |
-
|
| 28 |
-
# β
Paths
|
| 29 |
-
INDEX_DIR = "storage/index"
|
| 30 |
-
DATA_DIR = "data/news"
|
| 31 |
-
RAW_JSON = os.path.join(DATA_DIR, "news.jsonl")
|
| 32 |
-
|
| 33 |
-
def write_articles_jsonl(articles: List[Dict], file_path: str):
|
| 34 |
-
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
| 35 |
-
with open(file_path, "w", encoding="utf-8") as f:
|
| 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
|
|
@@ -52,31 +14,29 @@ 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 |
-
# β
|
| 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 |
-
#
|
| 63 |
-
QUERIES = [
|
| 64 |
-
"India news", "World news", "Tech news", "Finance news", "Sports news"
|
| 65 |
-
]
|
| 66 |
|
| 67 |
-
#
|
| 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(
|
|
@@ -91,7 +51,7 @@ async def build_documents(data: List[Dict]) -> List[Document]:
|
|
| 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.")
|
|
@@ -142,11 +102,10 @@ async def main():
|
|
| 142 |
get_or_build_index_from_docs(documents)
|
| 143 |
|
| 144 |
print("β‘ Generating daily feed...")
|
| 145 |
-
|
| 146 |
|
| 147 |
print(f"β
Indexed, headlines generated, and stored at: {INDEX_DIR}")
|
| 148 |
|
| 149 |
-
|
| 150 |
if __name__ == "__main__":
|
| 151 |
asyncio.run(main())
|
| 152 |
-
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import sys
|
|
|
|
| 3 |
import json
|
| 4 |
import asyncio
|
| 5 |
from typing import List, Dict
|
|
|
|
| 14 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 15 |
from llama_index.core.schema import Document
|
| 16 |
|
| 17 |
+
# β
Use local embedding model
|
| 18 |
Settings.embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/paraphrase-MiniLM-L3-v2")
|
| 19 |
|
| 20 |
# π Environment variables
|
| 21 |
API_KEY = os.environ.get("GOOGLE_API_KEY")
|
| 22 |
CSE_ID = os.environ.get("GOOGLE_CX_ID")
|
| 23 |
|
| 24 |
+
# π° Topics
|
| 25 |
+
QUERIES = ["India news", "World news", "Tech news", "Finance news", "Sports news"]
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# ποΈ Paths
|
|
|
|
| 28 |
DATA_DIR = "data/news"
|
| 29 |
RAW_JSON = os.path.join(DATA_DIR, "news.jsonl")
|
| 30 |
+
INDEX_DIR = "storage/index"
|
| 31 |
|
| 32 |
+
# πΎ Save articles to disk
|
| 33 |
def write_articles_jsonl(articles: List[Dict], file_path: str):
|
| 34 |
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
| 35 |
with open(file_path, "w", encoding="utf-8") as f:
|
| 36 |
for article in articles:
|
| 37 |
f.write(json.dumps(article, ensure_ascii=False) + "\n")
|
| 38 |
|
| 39 |
+
# π Convert raw scraped data into Document objects
|
| 40 |
async def build_documents(data: List[Dict]) -> List[Document]:
|
| 41 |
return [
|
| 42 |
Document(
|
|
|
|
| 51 |
for entry in data
|
| 52 |
]
|
| 53 |
|
| 54 |
+
# π Main pipeline runner
|
| 55 |
async def main():
|
| 56 |
if not API_KEY or not CSE_ID:
|
| 57 |
raise EnvironmentError("Missing GOOGLE_API_KEY or GOOGLE_CX_ID in environment.")
|
|
|
|
| 102 |
get_or_build_index_from_docs(documents)
|
| 103 |
|
| 104 |
print("β‘ Generating daily feed...")
|
| 105 |
+
generate_and_cache_daily_feed(documents) # β
SYNC CALL
|
| 106 |
|
| 107 |
print(f"β
Indexed, headlines generated, and stored at: {INDEX_DIR}")
|
| 108 |
|
| 109 |
+
# π Entrypoint
|
| 110 |
if __name__ == "__main__":
|
| 111 |
asyncio.run(main())
|
|
|