raghavNCI commited on
Commit
ccf9b0b
·
1 Parent(s): 011e38f

integrating headlines to wa

Browse files
Files changed (2) hide show
  1. nuse_modules/classifier.py +2 -1
  2. routes/question.py +28 -1
nuse_modules/classifier.py CHANGED
@@ -20,7 +20,8 @@ QUESTION_TYPES = {
20
  "science_health": 10,
21
  "fact_check": 11,
22
  "compare_entities": 12,
23
- "small_talk": 13
 
24
  }
25
 
26
  REVERSE_MAP = {v: k for k, v in QUESTION_TYPES.items()}
 
20
  "science_health": 10,
21
  "fact_check": 11,
22
  "compare_entities": 12,
23
+ "asking_for_headlines": 13,
24
+ "small_talk": 14
25
  }
26
 
27
  REVERSE_MAP = {v: k for k, v in QUESTION_TYPES.items()}
routes/question.py CHANGED
@@ -1,5 +1,6 @@
1
  import os
2
  import requests
 
3
  import json
4
  from fastapi import APIRouter
5
  from pydantic import BaseModel
@@ -20,7 +21,7 @@ class QuestionInput(BaseModel):
20
 
21
 
22
  def should_extract_keywords(type_id: int) -> bool:
23
- return type_id in {1, 2, 3, 4, 5, 6, 7, 10}
24
 
25
 
26
  def extract_answer_after_label(text: str) -> str:
@@ -45,6 +46,32 @@ async def ask_question(input: QuestionInput):
45
  context = ""
46
  sources = []
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  # Step 2: Keyword extraction and news search (if needed)
49
  if should_extract_keywords(qid):
50
  keywords = keywords_extractor(question)
 
1
  import os
2
  import requests
3
+ import datetime
4
  import json
5
  from fastapi import APIRouter
6
  from pydantic import BaseModel
 
21
 
22
 
23
  def should_extract_keywords(type_id: int) -> bool:
24
+ return type_id in {1, 2, 3, 4, 5, 6, 7, 10, 11, 12}
25
 
26
 
27
  def extract_answer_after_label(text: str) -> str:
 
46
  context = ""
47
  sources = []
48
 
49
+ if qid == 13:
50
+ date_str = datetime.datetime.utcnow().strftime("%Y-%m-%d")
51
+ categories = ["world", "india", "finance", "sports", "entertainment"]
52
+ all_headlines = []
53
+
54
+ for cat in categories:
55
+ key = f"headlines:{date_str}:{cat}"
56
+ cached = _r.get(key)
57
+ if cached:
58
+ articles = json.loads(cached)
59
+ for art in articles:
60
+ all_headlines.append({
61
+ "title": art["title"],
62
+ "summary": art["summary"],
63
+ "url": art["url"],
64
+ "image": art.get("image"),
65
+ "category": cat,
66
+ })
67
+
68
+ return {
69
+ "question": question,
70
+ "answer": "Here are today’s top headlines:",
71
+ "headlines": all_headlines # include this for frontend/bot formatting
72
+ }
73
+
74
+
75
  # Step 2: Keyword extraction and news search (if needed)
76
  if should_extract_keywords(qid):
77
  keywords = keywords_extractor(question)