raghavNCI commited on
Commit
2c42748
·
1 Parent(s): 2812e86

hf token and llm change

Browse files
Files changed (3) hide show
  1. .env +0 -2
  2. .gitignore +1 -0
  3. question.py +10 -8
.env DELETED
@@ -1,2 +0,0 @@
1
- GNEWS_API_KEY=6c61f5da1b24fa83fbf964f8b280c438
2
- UPSTASH_REDIS_URL=rediss://:AU6-AAIjcDEyZDY4Njk3OGUwNzg0NTczODUxYmRmMDUyZDlmZWNiZXAxMA@clever-turtle-20158.upstash.io:6379
 
 
 
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
question.py CHANGED
@@ -1,21 +1,22 @@
1
  # app/routes/question.py
2
  import os
3
  import requests
4
- from fastapi import APIRouter, Query
5
  from pydantic import BaseModel
6
  from typing import List
7
  from redis_client import redis_client as r
8
- from transformers import pipeline
9
- import re
10
  from dotenv import load_dotenv
 
 
11
 
12
  load_dotenv()
13
 
14
  GNEWS_API_KEY = os.getenv("GNEWS_API_KEY")
 
15
 
16
  askMe = APIRouter()
17
 
18
- qa_model = pipeline("text2text-generation", model="google/flan-t5-base") # replace with your preferred model
19
 
20
  class QuestionInput(BaseModel):
21
  question: str
@@ -42,13 +43,14 @@ async def ask_question(input: QuestionInput):
42
  context = "\n\n".join([
43
  article.get("content") or article.get("description") or ""
44
  for article in articles
45
- ])
46
 
47
  print("And context is", context)
48
 
49
  # Build prompt
50
- prompt = f"Context:\n{context}\n\nQuestion: {question}\nAnswer:"
51
- result = qa_model(prompt, max_length=256, do_sample=False)[0]['generated_text']
 
52
 
53
  return {
54
  "question": question,
@@ -57,4 +59,4 @@ async def ask_question(input: QuestionInput):
57
  {"title": a["title"], "url": a["url"]}
58
  for a in articles
59
  ]
60
- }
 
1
  # app/routes/question.py
2
  import os
3
  import requests
4
+ from fastapi import APIRouter
5
  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", token=HF_TOKEN)
20
 
21
  class QuestionInput(BaseModel):
22
  question: str
 
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 'I don't know'.\n\nContext:\n{context}\n\nQuestion: {question} [/INST]"
52
+
53
+ result = client.text_generation(prompt, max_new_tokens=256, temperature=0.7)
54
 
55
  return {
56
  "question": question,
 
59
  {"title": a["title"], "url": a["url"]}
60
  for a in articles
61
  ]
62
+ }