Juslin Jay commited on
Commit ·
c2d817c
1
Parent(s): 8193dd6
Update qa.py
Browse files
app/qa.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
from fastapi import APIRouter, HTTPException
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
from app.model_loader import load_qa_pipeline
|
| 4 |
from app.utils import get_index_from_text
|
|
|
|
| 5 |
|
| 6 |
router = APIRouter()
|
| 7 |
qa_pipeline = load_qa_pipeline()
|
|
@@ -18,6 +19,14 @@ class AskRequest(BaseModel):
|
|
| 18 |
question: str
|
| 19 |
document: str
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@router.post("/ask")
|
| 22 |
async def ask_question(payload: AskRequest):
|
| 23 |
try:
|
|
@@ -59,4 +68,4 @@ async def ask_question(payload: AskRequest):
|
|
| 59 |
}
|
| 60 |
|
| 61 |
except Exception as e:
|
| 62 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
from fastapi import APIRouter, HTTPException
|
| 2 |
+
from pydantic import BaseModel, validator
|
| 3 |
from app.model_loader import load_qa_pipeline
|
| 4 |
from app.utils import get_index_from_text
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
router = APIRouter()
|
| 8 |
qa_pipeline = load_qa_pipeline()
|
|
|
|
| 19 |
question: str
|
| 20 |
document: str
|
| 21 |
|
| 22 |
+
@validator("document", pre=True)
|
| 23 |
+
def sanitize_document(cls, v):
|
| 24 |
+
if not isinstance(v, str):
|
| 25 |
+
raise ValueError("Document must be a string.")
|
| 26 |
+
# Replace problematic control characters with space (except \n)
|
| 27 |
+
v = re.sub(r"[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]", " ", v)
|
| 28 |
+
return v.strip()
|
| 29 |
+
|
| 30 |
@router.post("/ask")
|
| 31 |
async def ask_question(payload: AskRequest):
|
| 32 |
try:
|
|
|
|
| 68 |
}
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
+
raise HTTPException(status_code=500, detail=f"Internal Error: {str(e)}")
|