raghavNCI commited on
Commit
fbd5aba
·
1 Parent(s): 20238ed

changes v4

Browse files
Files changed (2) hide show
  1. question.py +43 -24
  2. requirements.txt +1 -0
question.py CHANGED
@@ -6,18 +6,17 @@ from pydantic import BaseModel
6
  from typing import List
7
  from redis_client import redis_client as r
8
  from dotenv import load_dotenv
9
- from huggingface_hub import InferenceClient
10
  import re
 
11
 
12
  load_dotenv()
13
 
14
  GNEWS_API_KEY = os.getenv("GNEWS_API_KEY")
15
- HF_TOKEN = os.getenv("HF_TOKEN") # Hugging Face token for private models if needed
16
 
17
  askMe = APIRouter()
18
 
19
- client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3", token=os.getenv("HF_TOKEN"))
20
-
21
  class QuestionInput(BaseModel):
22
  question: str
23
 
@@ -25,13 +24,16 @@ class QuestionInput(BaseModel):
25
  async def ask_question(input: QuestionInput):
26
  question = input.question
27
 
28
- # Extract keywords (simple version)
29
- keywords = re.findall(r"\b\w{4,}\b", question)
 
 
 
 
30
  query_string = " AND ".join(f'"{kw}"' for kw in keywords[:7])
 
31
 
32
- print("Keywords are", query_string)
33
-
34
- gnews_url = f"https://gnews.io/api/v4/search?q={query_string}&lang=en&max=3&expand=content&token={GNEWS_API_KEY}"
35
  try:
36
  response = requests.get(gnews_url, timeout=10)
37
  response.raise_for_status()
@@ -39,31 +41,48 @@ async def ask_question(input: QuestionInput):
39
  except Exception as e:
40
  return {"error": f"GNews API error: {str(e)}"}
41
 
42
- # Combine article content for context
43
  context = "\n\n".join([
44
  article.get("content") or article.get("description") or ""
45
  for article in articles
46
- ])[:1500] # truncate to keep model input size safe
47
 
48
- print("And context is", context)
 
 
 
 
 
49
 
50
- # Build prompt
51
- prompt = f"<s>[INST] Use the context below to answer the question. If the context is insufficient, say 'Cannot answer'.\n\nContext:\n{context}\n\nQuestion: {question} [/INST]"
 
 
 
 
52
 
53
- # result = client.text_generation(prompt, max_new_tokens=256, temperature=0.7)
 
 
 
 
 
 
 
 
 
 
54
 
55
- result = client.conversational(
56
- messages=[
57
- {"role": "system", "content": "You are a helpful assistant that uses provided news context to answer questions."},
58
- {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}"}
59
- ],
60
- max_new_tokens=256,
61
- temperature=0.7
62
- )
63
 
64
  return {
65
  "question": question,
66
- "answer": result.strip(),
67
  "sources": [
68
  {"title": a["title"], "url": a["url"]}
69
  for a in articles
 
6
  from typing import List
7
  from redis_client import redis_client as r
8
  from dotenv import load_dotenv
9
+ from urllib.parse import quote_plus
10
  import re
11
+ import json
12
 
13
  load_dotenv()
14
 
15
  GNEWS_API_KEY = os.getenv("GNEWS_API_KEY")
16
+ HF_TOKEN = os.getenv("HF_TOKEN")
17
 
18
  askMe = APIRouter()
19
 
 
 
20
  class QuestionInput(BaseModel):
21
  question: str
22
 
 
24
  async def ask_question(input: QuestionInput):
25
  question = input.question
26
 
27
+ # Basic keyword extraction with stopword filtering
28
+ STOPWORDS = {"what", "about", "which", "would", "could", "this", "that"}
29
+ keywords = [
30
+ kw for kw in re.findall(r"\b\w{4,}\b", question.lower())
31
+ if kw not in STOPWORDS
32
+ ]
33
  query_string = " AND ".join(f'"{kw}"' for kw in keywords[:7])
34
+ encoded_query = quote_plus(query_string)
35
 
36
+ gnews_url = f"https://gnews.io/api/v4/search?q={encoded_query}&lang=en&max=3&expand=content&token={GNEWS_API_KEY}"
 
 
37
  try:
38
  response = requests.get(gnews_url, timeout=10)
39
  response.raise_for_status()
 
41
  except Exception as e:
42
  return {"error": f"GNews API error: {str(e)}"}
43
 
 
44
  context = "\n\n".join([
45
  article.get("content") or article.get("description") or ""
46
  for article in articles
47
+ ])[:1500]
48
 
49
+ if not context.strip():
50
+ return {
51
+ "question": question,
52
+ "answer": "Cannot answer – no relevant context found.",
53
+ "sources": []
54
+ }
55
 
56
+ # Call HF Inference API manually
57
+ hf_api_url = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
58
+ headers = {
59
+ "Authorization": f"Bearer {HF_TOKEN}",
60
+ "Content-Type": "application/json"
61
+ }
62
 
63
+ payload = {
64
+ "inputs": {
65
+ "past_user_inputs": [],
66
+ "generated_responses": [],
67
+ "text": f"Context:\n{context}\n\nQuestion: {question}"
68
+ },
69
+ "parameters": {
70
+ "max_new_tokens": 256,
71
+ "temperature": 0.7
72
+ }
73
+ }
74
 
75
+ try:
76
+ response = requests.post(hf_api_url, headers=headers, data=json.dumps(payload), timeout=30)
77
+ response.raise_for_status()
78
+ hf_response = response.json()
79
+ answer = hf_response.get("generated_text", "Cannot answer.")
80
+ except Exception as e:
81
+ return {"error": f"Hugging Face API error: {str(e)}"}
 
82
 
83
  return {
84
  "question": question,
85
+ "answer": answer.strip(),
86
  "sources": [
87
  {"title": a["title"], "url": a["url"]}
88
  for a in articles
requirements.txt CHANGED
@@ -4,6 +4,7 @@ requests
4
  python-dotenv
5
  redis
6
  transformers
 
7
  torch
8
  hf_xet
9
  huggingface_hub
 
4
  python-dotenv
5
  redis
6
  transformers
7
+ accelerate
8
  torch
9
  hf_xet
10
  huggingface_hub