raghavNCI
commited on
Commit
·
fbd5aba
1
Parent(s):
20238ed
changes v4
Browse files- question.py +43 -24
- 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
|
10 |
import re
|
|
|
11 |
|
12 |
load_dotenv()
|
13 |
|
14 |
GNEWS_API_KEY = os.getenv("GNEWS_API_KEY")
|
15 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
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 |
-
#
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
query_string = " AND ".join(f'"{kw}"' for kw in keywords[:7])
|
|
|
31 |
|
32 |
-
|
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]
|
47 |
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
#
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
)
|
63 |
|
64 |
return {
|
65 |
"question": question,
|
66 |
-
"answer":
|
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
|