Juslin Jay commited on
Commit
a07eca7
·
1 Parent(s): c2d817c

Update qa.py

Browse files
Files changed (1) hide show
  1. app/qa.py +22 -13
app/qa.py CHANGED
@@ -1,7 +1,8 @@
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()
@@ -19,17 +20,25 @@ class AskRequest(BaseModel):
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:
 
 
 
 
 
 
 
 
 
 
 
 
33
  index = get_index_from_text(payload.document)
34
  query_engine = index.as_query_engine(similarity_top_k=3)
35
  context_response = query_engine.query(payload.question)
@@ -44,7 +53,6 @@ async def ask_question(payload: AskRequest):
44
 
45
  # Fallback if confidence is low or no good answer
46
  if confidence < 0.2 or not final_answer or final_answer.lower() == payload.question.lower():
47
- # Try glossary match
48
  for key in glossary:
49
  if key.lower() in payload.question.lower():
50
  return {
@@ -53,7 +61,6 @@ async def ask_question(payload: AskRequest):
53
  "context": "Definition pulled from built-in glossary."
54
  }
55
 
56
- # Otherwise return a helpful suggestion
57
  return {
58
  "answer": "The document does not provide a clear answer to this question.",
59
  "confidence": confidence,
@@ -67,5 +74,7 @@ async def ask_question(payload: AskRequest):
67
  "context": str(context_response)
68
  }
69
 
 
 
70
  except Exception as e:
71
- raise HTTPException(status_code=500, detail=f"Internal Error: {str(e)}")
 
1
+ from fastapi import APIRouter, HTTPException, Request
2
+ from pydantic import BaseModel, ValidationError
3
  from app.model_loader import load_qa_pipeline
4
  from app.utils import get_index_from_text
5
+ import json
6
  import re
7
 
8
  router = APIRouter()
 
20
  question: str
21
  document: str
22
 
23
+ def clean_input_string(raw: str) -> str:
24
+ # Replace invalid control characters except newline and tab
25
+ return re.sub(r'[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]', '', raw)
 
 
 
 
26
 
27
  @router.post("/ask")
28
+ async def ask_question(request: Request):
29
  try:
30
+ raw_body = await request.body()
31
+ try:
32
+ # Try normal JSON parsing
33
+ payload = AskRequest.parse_raw(raw_body)
34
+ except ValidationError as ve:
35
+ # Try cleaning and parsing again
36
+ cleaned_str = clean_input_string(raw_body.decode("utf-8"))
37
+ try:
38
+ payload = AskRequest.parse_raw(cleaned_str)
39
+ except Exception:
40
+ raise HTTPException(status_code=422, detail="Invalid JSON format, possibly due to unescaped characters or broken structure.")
41
+
42
  index = get_index_from_text(payload.document)
43
  query_engine = index.as_query_engine(similarity_top_k=3)
44
  context_response = query_engine.query(payload.question)
 
53
 
54
  # Fallback if confidence is low or no good answer
55
  if confidence < 0.2 or not final_answer or final_answer.lower() == payload.question.lower():
 
56
  for key in glossary:
57
  if key.lower() in payload.question.lower():
58
  return {
 
61
  "context": "Definition pulled from built-in glossary."
62
  }
63
 
 
64
  return {
65
  "answer": "The document does not provide a clear answer to this question.",
66
  "confidence": confidence,
 
74
  "context": str(context_response)
75
  }
76
 
77
+ except HTTPException as e:
78
+ raise e
79
  except Exception as e:
80
+ raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")