Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 4 |
+
from fastapi.responses import JSONResponse
|
| 5 |
+
from services.extract_text import extract_text_from_file
|
| 6 |
+
from services.vector_store import get_entry, upsert_entry
|
| 7 |
+
from services.s3_utils import upload_to_s3
|
| 8 |
+
|
| 9 |
+
api = FastAPI()
|
| 10 |
+
|
| 11 |
+
def save_temp_file(file: UploadFile) -> str:
|
| 12 |
+
tmp = tempfile.NamedTemporaryFile(delete=False)
|
| 13 |
+
tmp.write(file.file.read())
|
| 14 |
+
tmp.flush()
|
| 15 |
+
upload_to_s3(tmp.name, f"documents/{file.filename}")
|
| 16 |
+
print(f"📤 Uploaded {file.filename} to S3")
|
| 17 |
+
return tmp.name
|
| 18 |
+
|
| 19 |
+
@api.post("/api/text")
|
| 20 |
+
async def extract_text_api(
|
| 21 |
+
file: UploadFile = File(...),
|
| 22 |
+
filename: str = Form(...),
|
| 23 |
+
start_page: int = Form(...),
|
| 24 |
+
end_page: int = Form(...)
|
| 25 |
+
):
|
| 26 |
+
cache = get_entry(filename) or {}
|
| 27 |
+
if "text" in cache:
|
| 28 |
+
return {"text": cache["text"]}
|
| 29 |
+
|
| 30 |
+
path = save_temp_file(file)
|
| 31 |
+
|
| 32 |
+
with open(path, "rb") as fh:
|
| 33 |
+
cache["text"] = extract_text_from_file(fh, start_page, end_page, filename)
|
| 34 |
+
|
| 35 |
+
os.remove(path)
|
| 36 |
+
cache.pop("filename", None)
|
| 37 |
+
upsert_entry(filename, **cache)
|
| 38 |
+
return {"text": cache["text"]}
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
import uvicorn
|
| 42 |
+
uvicorn.run("app:api", host="0.0.0.0", port=3001, reload=True)
|