-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
151 lines (114 loc) · 5.2 KB
/
Copy pathapi.py
File metadata and controls
151 lines (114 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import io
import logging
import zipfile
from pathlib import Path
from typing import Literal
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field
from starlette.concurrency import run_in_threadpool
from src.config import (MAX_DOC_CHARS, MAX_HISTORY_TURNS, MAX_MESSAGE_CHARS,
MAX_TURN_CHARS, MAX_UPLOAD_BYTES)
from src.engine import get_ipbot_response
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger("ipbot")
ROOT = Path(__file__).resolve().parent
FRONTEND = ROOT / "frontend"
ASSETS = ROOT / "assets"
app = FastAPI(title="IPBot", docs_url=None, redoc_url=None)
@app.middleware("http")
async def security_headers(request, call_next):
response = await call_next(request)
# everything is served from this origin, so nothing needs to reach out except the map embed
# this is also what stops injected markup from phoning home
response.headers["Content-Security-Policy"] = (
"default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; "
"img-src 'self' data:; connect-src 'self'; "
"frame-src https://www.openstreetmap.org; frame-ancestors 'none'; base-uri 'none'"
)
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["Referrer-Policy"] = "no-referrer"
return response
class Turn(BaseModel):
role: Literal["user", "assistant"]
content: str = Field(default="", max_length=MAX_TURN_CHARS)
class ChatIn(BaseModel):
message: str = Field(default="", max_length=MAX_MESSAGE_CHARS)
history: list[Turn] = Field(default_factory=list, max_length=MAX_HISTORY_TURNS)
doc_text: str = Field(default="", max_length=MAX_DOC_CHARS)
@app.post("/api/chat")
def chat(body: ChatIn):
question = body.message.strip()
if not question and not body.doc_text.strip():
return {"answer": "Ask me anything about IPB and I'll look it up.", "sources": []}
history = [{"role": turn.role, "content": turn.content} for turn in body.history]
try:
return get_ipbot_response(question, history, doc_text=body.doc_text)
except Exception:
log.exception("chat failed")
return {"answer": "⚠️ Something went wrong. Please try again.", "sources": []}
TEXT_SUFFIXES = {"txt", "md", "csv"}
ACCEPTED = TEXT_SUFFIXES | {"pdf", "docx"}
def _read_pdf(data):
from pypdf import PdfReader
reader = PdfReader(io.BytesIO(data))
if len(reader.pages) > 300:
raise ValueError("too many pages")
parts, size = [], 0
for page in reader.pages:
parts.append(page.extract_text() or "")
size += len(parts[-1])
# only the first MAX_DOC_CHARS are ever used, so there is no point walking a page tree built to keep us busy
if size > MAX_DOC_CHARS * 4:
break
return "\n".join(parts)
def _read_docx(data):
from docx import Document
with zipfile.ZipFile(io.BytesIO(data)) as archive:
if "word/document.xml" not in archive.namelist():
raise ValueError("not a word document")
if sum(item.file_size for item in archive.infolist()) > 50_000_000:
raise ValueError("archive expands too far")
return "\n".join(p.text for p in Document(io.BytesIO(data)).paragraphs)
def _extract(suffix, data):
# the extension picked the parser, so check the bytes agree with it
if suffix == "pdf":
if not data.startswith(b"%PDF"):
raise ValueError("not a pdf")
return _read_pdf(data)
if suffix == "docx":
if not data.startswith(b"PK"):
raise ValueError("not a docx")
return _read_docx(data)
text = data.decode("utf-8", errors="replace")
if text.count("�") > len(text) * 0.1:
raise ValueError("not readable text")
return text
@app.post("/api/upload")
async def upload(file: UploadFile = File(...)):
name = Path(file.filename or "document").name[:100]
suffix = name.lower().rsplit(".", 1)[-1] if "." in name else ""
if suffix not in ACCEPTED:
return {"name": name, "text": "", "error": "Upload a PDF, Word or text file."}
# read in slices and stop early, so an oversized upload is refused rather than pulled into memory in one piece and measured afterwards
buf = bytearray()
while chunk := await file.read(1 << 20):
buf += chunk
if len(buf) > MAX_UPLOAD_BYTES:
return {"name": name, "text": "", "error": "File too large (max 5 MB)."}
try:
# parsing is blocking and CPU-bound, so keep it off the event loop
text = await run_in_threadpool(_extract, suffix, bytes(buf))
except Exception:
log.exception("could not read %s", name)
return {"name": name, "text": "", "error": "Could not read this file."}
text = text.strip()
if not text:
return {"name": name, "text": "", "error": "No readable text in this file."}
return {"name": name, "text": text[:MAX_DOC_CHARS]}
app.mount("/assets", StaticFiles(directory=str(ASSETS)), name="assets")
app.mount("/static", StaticFiles(directory=str(FRONTEND)), name="static")
@app.get("/")
def index():
return FileResponse(str(FRONTEND / "index.html"))