Update api.py
Browse files
api.py
CHANGED
|
@@ -13,7 +13,8 @@ import numpy as np
|
|
| 13 |
import faiss
|
| 14 |
from fastapi import FastAPI, HTTPException
|
| 15 |
from fastapi.middleware.cors import CORSMiddleware
|
| 16 |
-
from fastapi.responses import StreamingResponse
|
|
|
|
| 17 |
from pydantic import BaseModel
|
| 18 |
from sentence_transformers import SentenceTransformer
|
| 19 |
from groq import Groq
|
|
@@ -24,7 +25,7 @@ if not GROQ_API_KEY:
|
|
| 24 |
raise RuntimeError("β GROQ_API_KEY not set β add it to HF Space secrets")
|
| 25 |
|
| 26 |
EMBED_MODEL = "all-MiniLM-L6-v2"
|
| 27 |
-
GROQ_MODEL
|
| 28 |
TOP_K = 5
|
| 29 |
|
| 30 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -72,9 +73,10 @@ class QueryRequest(BaseModel):
|
|
| 72 |
|
| 73 |
# βββββββββββββββββββββββ HELPERS ββββββββββββββββββββββββ
|
| 74 |
SYSTEM_PROMPT = """You are CovidKosha Assistant, an expert on COVID-19 research.
|
| 75 |
-
|
|
|
|
| 76 |
- Cite source numbers [1], [2] etc. from the context.
|
| 77 |
-
- If the context
|
| 78 |
- Be concise and accurate. Use bullet points where helpful.
|
| 79 |
- Mention journal names or PMIDs when available."""
|
| 80 |
|
|
@@ -121,6 +123,11 @@ def build_context(results):
|
|
| 121 |
|
| 122 |
# βββββββββββββββββββββββ ROUTES βββββββββββββββββββββββββ
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
@app.get("/health")
|
| 125 |
def health():
|
| 126 |
return {"status": "ok", "chunks": faiss_index.ntotal}
|
|
|
|
| 13 |
import faiss
|
| 14 |
from fastapi import FastAPI, HTTPException
|
| 15 |
from fastapi.middleware.cors import CORSMiddleware
|
| 16 |
+
from fastapi.responses import StreamingResponse, HTMLResponse
|
| 17 |
+
from fastapi.staticfiles import StaticFiles
|
| 18 |
from pydantic import BaseModel
|
| 19 |
from sentence_transformers import SentenceTransformer
|
| 20 |
from groq import Groq
|
|
|
|
| 25 |
raise RuntimeError("β GROQ_API_KEY not set β add it to HF Space secrets")
|
| 26 |
|
| 27 |
EMBED_MODEL = "all-MiniLM-L6-v2"
|
| 28 |
+
GROQ_MODEL = "llama-3.3-70b-versatile"
|
| 29 |
TOP_K = 5
|
| 30 |
|
| 31 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 73 |
|
| 74 |
# βββββββββββββββββββββββ HELPERS ββββββββββββββββββββββββ
|
| 75 |
SYSTEM_PROMPT = """You are CovidKosha Assistant, an expert on COVID-19 research.
|
| 76 |
+
You are given retrieved research context. Use it to answer the question as fully as possible.
|
| 77 |
+
- ALWAYS provide a useful answer based on whatever context is available β never return only a disclaimer.
|
| 78 |
- Cite source numbers [1], [2] etc. from the context.
|
| 79 |
+
- If the context only partially covers the question, answer what you can from it, then briefly note what is not covered.
|
| 80 |
- Be concise and accurate. Use bullet points where helpful.
|
| 81 |
- Mention journal names or PMIDs when available."""
|
| 82 |
|
|
|
|
| 123 |
|
| 124 |
# βββββββββββββββββββββββ ROUTES βββββββββββββββββββββββββ
|
| 125 |
|
| 126 |
+
@app.get("/", response_class=HTMLResponse)
|
| 127 |
+
def root():
|
| 128 |
+
with open(os.path.join(BASE_DIR, "index.html"), encoding="utf-8") as f:
|
| 129 |
+
return f.read()
|
| 130 |
+
|
| 131 |
@app.get("/health")
|
| 132 |
def health():
|
| 133 |
return {"status": "ok", "chunks": faiss_index.ntotal}
|