File size: 6,300 Bytes
8900ccf 529bb74 8900ccf 9f89dfb 14da8ca 9f89dfb 14da8ca 8900ccf 9f89dfb 61e0630 0a2fe6c 14da8ca 9f89dfb 14da8ca 4de74d3 14da8ca 4a7a44e 14da8ca 9f89dfb 0a2fe6c 9f89dfb 8900ccf 9f89dfb 8900ccf 9f89dfb 8900ccf 97b559e 8900ccf 9f89dfb 14da8ca 0a2fe6c 14da8ca 8900ccf 14da8ca 8900ccf 14da8ca 8900ccf 14da8ca 9f89dfb 0a2fe6c 5a92fca 0a2fe6c |
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# import os
# import tempfile
# from typing import List
# from fastapi import FastAPI, UploadFile, File, Form
# from fastapi.responses import JSONResponse
# from pydantic import BaseModel
# from fastapi import Body
# import traceback
# from typing import TypedDict, Dict, Any
# # === LangGraph ===
# from langgraph.graph import StateGraph
# # from langgraph.checkpoint import MemorySaver
# # === Service Imports ===
# from services.extract_text import extract_text_from_file, extract_images_with_fitz
# from services.extract_table import extract_tables_from_file
# from services.vector_store import get_entry, upsert_entry
# from services.s3_utils import upload_to_s3
# # === FastAPI Init ===
# api = FastAPI()
# # === Shared helpers ===
# def save_temp_file(file: UploadFile) -> str:
# tmp = tempfile.NamedTemporaryFile(delete=False)
# tmp.write(file.file.read())
# tmp.flush()
# upload_to_s3(tmp.name, f"documents/{file.filename}")
# print(f"π€ Uploaded {file.filename} to S3")
# return tmp.name
# # === LangGraph Nodes ===
# def extract_text_node(state):
# filename = state["filename"]
# path = state["temp_files"][filename]
# start_page = state.get("start_page")
# end_page = state.get("end_page")
# with open(path, "rb") as fh:
# state["text"] = extract_text_from_file(fh, start_page, end_page, filename)
# return state
# def extract_tables_node(state):
# filename = state["filename"]
# path = state["temp_files"][filename]
# start_page = state.get("start_page")
# end_page = state.get("end_page")
# with open(path, "rb") as fh:
# state["tables"] = extract_tables_from_file(fh, start_page, end_page, filename)
# return state
# node_map = {
# "text": extract_text_node,
# "table": extract_tables_node
# }
# # === Individual APIs ===
# @api.post("/api/text")
# async def extract_text_api(
# file: UploadFile = File(...),
# filename: str = Form(...),
# start_page: int = Form(...),
# end_page: int = Form(...)
# ):
# cache = get_entry(filename) or {}
# if "text" in cache:
# return {"text": cache["text"]}
# path = save_temp_file(file)
# with open(path, "rb") as fh:
# cache["text"] = extract_text_from_file(fh, start_page, end_page, filename)
# os.remove(path)
# cache.pop("filename", None)
# upsert_entry(filename, **cache)
# return {"text": cache["text"]}
# @api.post("/api/tables")
# async def extract_table_api(
# file: UploadFile = File(...),
# filename: str = Form(...),
# start_page: int = Form(...),
# end_page: int = Form(...)
# ):
# cache = get_entry(filename) or {}
# if "tables" in cache:
# return {"tables": cache["tables"]}
# path = save_temp_file(file)
# with open(path, "rb") as fh:
# cache["tables"] = extract_tables_from_file(fh, start_page, end_page, filename)
# os.remove(path)
# cache.pop("filename", None)
# upsert_entry(filename, **cache)
# return {"tables": cache["tables"]}
# if __name__ == "__main__":
# import uvicorn
# uvicorn.run("app:api", host="0.0.0.0", port=7860, reload=True)
import os
import tempfile
from typing import List
from fastapi import FastAPI, UploadFile, File, Form
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from fastapi import Body
import traceback
from typing import TypedDict, Dict, Any
# === LangGraph ===
from langgraph.graph import StateGraph
# from langgraph.checkpoint import MemorySaver
# === Service Imports ===
from services.extract_text import extract_text_from_file, extract_images_with_fitz
from services.extract_table import extract_tables_from_file
# from services.vector_store import get_entry, upsert_entry # β Disabled cache
from services.s3_utils import upload_to_s3
# === FastAPI Init ===
api = FastAPI()
# === Root Health Check ===
@api.get("/")
async def root():
return {"status": "ok", "message": "Space is running"}
# === Shared helpers ===
def save_temp_file(file: UploadFile) -> str:
tmp = tempfile.NamedTemporaryFile(delete=False)
tmp.write(file.file.read())
tmp.flush()
upload_to_s3(tmp.name, f"documents/{file.filename}")
print(f"π€ Uploaded {file.filename} to S3")
return tmp.name
# === LangGraph Nodes ===
def extract_text_node(state):
filename = state["filename"]
path = state["temp_files"][filename]
start_page = state.get("start_page")
end_page = state.get("end_page")
with open(path, "rb") as fh:
state["text"] = extract_text_from_file(fh, start_page, end_page, filename)
return state
def extract_tables_node(state):
filename = state["filename"]
path = state["temp_files"][filename]
start_page = state.get("start_page")
end_page = state.get("end_page")
with open(path, "rb") as fh:
state["tables"] = extract_tables_from_file(fh, start_page, end_page, filename)
return state
node_map = {
"text": extract_text_node,
"table": extract_tables_node
}
# === Individual APIs ===
@api.post("/api/text")
async def extract_text_api(
file: UploadFile = File(...),
filename: str = Form(...),
start_page: int = Form(...),
end_page: int = Form(...),
):
# cache = get_entry(filename) or {} # β disabled
path = save_temp_file(file)
with open(path, "rb") as fh:
text = extract_text_from_file(fh, start_page, end_page, filename)
os.remove(path)
# cache.pop("filename", None)
# upsert_entry(filename, **cache) # β disabled
return {"text": text}
@api.post("/api/tables")
async def extract_table_api(
file: UploadFile = File(...),
filename: str = Form(...),
start_page: int = Form(...),
end_page: int = Form(...),
):
# cache = get_entry(filename) or {} # β disabled
path = save_temp_file(file)
with open(path, "rb") as fh:
tables = extract_tables_from_file(fh, start_page, end_page, filename)
os.remove(path)
# cache.pop("filename", None)
# upsert_entry(filename, **cache) # β disabled
return {"tables": tables}
if __name__ == "__main__":
import uvicorn
# β¬οΈ Removed reload=True (causing shutdowns in Hugging Face Spaces)
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|