ragV98 commited on
Commit
e465159
Β·
1 Parent(s): f312f0d
Files changed (1) hide show
  1. components/generators/daily_feed.py +15 -15
components/generators/daily_feed.py CHANGED
@@ -7,34 +7,34 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
7
 
8
  import redis
9
  from typing import List, Dict
10
- from llama_index.core.settings import Settings
11
  from llama_index.core import VectorStoreIndex
12
  from llama_index.core.query_engine import RetrieverQueryEngine
13
  from llama_index.core.schema import Document
14
- from components.indexers.news_indexer import load_news_index
15
 
 
16
  Settings.llm = None
17
 
18
- # Load 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") # Inference endpoint URL
22
- HF_TOKEN = os.environ.get("HF_TOKEN") # Hugging Face endpoint token
23
 
24
- # Connect to Redis
25
  redis_client = redis.Redis.from_url(REDIS_URL, decode_responses=True)
26
 
27
- # Topics to query
28
  TOPICS = ["India news", "World news", "Tech news", "Finance news", "Sports news"]
29
 
30
- # Prompt to summarize topic
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 inference endpoint
38
  def call_mistral(prompt: str) -> str:
39
  headers = {
40
  "Authorization": f"Bearer {HF_TOKEN}",
@@ -53,7 +53,7 @@ def call_mistral(prompt: str) -> str:
53
  print(f"⚠️ Mistral error: {e}")
54
  return None
55
 
56
- # Generate summary for topic using Mistral
57
  def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
58
  feed = []
59
  for doc in docs[:5]:
@@ -67,10 +67,11 @@ def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
67
  })
68
  return feed
69
 
70
- # Main generation pipeline
71
  def generate_and_cache_daily_feed(documents: List[Document]):
72
  index = VectorStoreIndex.from_documents(documents)
73
- query_engine = RetrieverQueryEngine.from_args(index)
 
74
 
75
  final_feed = []
76
  for topic in TOPICS:
@@ -84,13 +85,12 @@ def generate_and_cache_daily_feed(documents: List[Document]):
84
  "feed": topic_feed
85
  })
86
 
87
- # Cache to Redis
88
  redis_client.set(REDIS_KEY, json.dumps(final_feed, ensure_ascii=False))
89
  print(f"βœ… Cached daily feed under key '{REDIS_KEY}'")
90
  return final_feed
91
 
92
- # Redis fetch for API
93
-
94
  def get_cached_daily_feed():
95
  cached = redis_client.get(REDIS_KEY)
96
  return json.loads(cached) if cached else []
 
7
 
8
  import redis
9
  from typing import List, Dict
 
10
  from llama_index.core import VectorStoreIndex
11
  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 OpenAI LLM fallback
16
  Settings.llm = None
17
 
18
+ # πŸ” Load 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") # Mistral inference endpoint
22
+ HF_TOKEN = os.environ.get("HF_TOKEN") # Hugging Face access token
23
 
24
+ # βœ… Connect to Redis
25
  redis_client = redis.Redis.from_url(REDIS_URL, decode_responses=True)
26
 
27
+ # πŸ” Topics to query and summarize
28
  TOPICS = ["India news", "World news", "Tech news", "Finance news", "Sports news"]
29
 
30
+ # 🧠 Prompt builder for summarization
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 API
38
  def call_mistral(prompt: str) -> str:
39
  headers = {
40
  "Authorization": f"Bearer {HF_TOKEN}",
 
53
  print(f"⚠️ Mistral error: {e}")
54
  return None
55
 
56
+ # βœ‚οΈ Summarize a list of documents into a short news feed
57
  def summarize_topic(docs: List[str], topic: str) -> List[Dict]:
58
  feed = []
59
  for doc in docs[:5]:
 
67
  })
68
  return feed
69
 
70
+ # πŸš€ Main pipeline
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:
 
85
  "feed": topic_feed
86
  })
87
 
88
+ # πŸ’Ύ Cache to Redis
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
+ # πŸ§ͺ Redis fetch (for use in APIs)
 
94
  def get_cached_daily_feed():
95
  cached = redis_client.get(REDIS_KEY)
96
  return json.loads(cached) if cached else []