Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -926,9 +926,9 @@ class BillRequest(BaseModel):
|
|
| 926 |
|
| 927 |
class BillResponse(BaseModel):
|
| 928 |
is_success: bool
|
| 929 |
-
error: Optional[str] = None
|
| 930 |
-
data: Dict[str, Any]
|
| 931 |
token_usage: Dict[str, int]
|
|
|
|
|
|
|
| 932 |
|
| 933 |
@app.post("/extract-bill-data", response_model=BillResponse)
|
| 934 |
async def extract_bill_data(payload: BillRequest):
|
|
@@ -944,9 +944,9 @@ async def extract_bill_data(payload: BillRequest):
|
|
| 944 |
except Exception as e:
|
| 945 |
return BillResponse(
|
| 946 |
is_success=False,
|
| 947 |
-
error=f"Local file read failed: {e}",
|
| 948 |
-
data={"pagewise_line_items": [], "total_item_count": 0},
|
| 949 |
token_usage={"total_tokens": 0, "input_tokens": 0, "output_tokens": 0},
|
|
|
|
|
|
|
| 950 |
)
|
| 951 |
else:
|
| 952 |
try:
|
|
@@ -955,25 +955,25 @@ async def extract_bill_data(payload: BillRequest):
|
|
| 955 |
if resp.status_code != 200:
|
| 956 |
return BillResponse(
|
| 957 |
is_success=False,
|
| 958 |
-
error=f"Download failed (status={resp.status_code})",
|
| 959 |
-
data={"pagewise_line_items": [], "total_item_count": 0},
|
| 960 |
token_usage={"total_tokens": 0, "input_tokens": 0, "output_tokens": 0},
|
|
|
|
|
|
|
| 961 |
)
|
| 962 |
file_bytes = resp.content
|
| 963 |
except Exception as e:
|
| 964 |
return BillResponse(
|
| 965 |
is_success=False,
|
| 966 |
-
error=f"HTTP error: {e}",
|
| 967 |
-
data={"pagewise_line_items": [], "total_item_count": 0},
|
| 968 |
token_usage={"total_tokens": 0, "input_tokens": 0, "output_tokens": 0},
|
|
|
|
|
|
|
| 969 |
)
|
| 970 |
|
| 971 |
if not file_bytes:
|
| 972 |
return BillResponse(
|
| 973 |
is_success=False,
|
| 974 |
-
error="No file bytes",
|
| 975 |
-
data={"pagewise_line_items": [], "total_item_count": 0},
|
| 976 |
token_usage={"total_tokens": 0, "input_tokens": 0, "output_tokens": 0},
|
|
|
|
|
|
|
| 977 |
)
|
| 978 |
|
| 979 |
logger.info(f"Processing with engine: {OCR_ENGINE}")
|
|
@@ -991,11 +991,12 @@ async def extract_bill_data(payload: BillRequest):
|
|
| 991 |
|
| 992 |
return BillResponse(
|
| 993 |
is_success=True,
|
|
|
|
| 994 |
data={
|
| 995 |
"pagewise_line_items": pages_dict,
|
| 996 |
"total_item_count": total_items,
|
| 997 |
},
|
| 998 |
-
|
| 999 |
)
|
| 1000 |
|
| 1001 |
@app.get("/")
|
|
|
|
| 926 |
|
| 927 |
class BillResponse(BaseModel):
|
| 928 |
is_success: bool
|
|
|
|
|
|
|
| 929 |
token_usage: Dict[str, int]
|
| 930 |
+
data: Dict[str, Any]
|
| 931 |
+
|
| 932 |
|
| 933 |
@app.post("/extract-bill-data", response_model=BillResponse)
|
| 934 |
async def extract_bill_data(payload: BillRequest):
|
|
|
|
| 944 |
except Exception as e:
|
| 945 |
return BillResponse(
|
| 946 |
is_success=False,
|
|
|
|
|
|
|
| 947 |
token_usage={"total_tokens": 0, "input_tokens": 0, "output_tokens": 0},
|
| 948 |
+
data={"pagewise_line_items": [], "total_item_count": 0},
|
| 949 |
+
|
| 950 |
)
|
| 951 |
else:
|
| 952 |
try:
|
|
|
|
| 955 |
if resp.status_code != 200:
|
| 956 |
return BillResponse(
|
| 957 |
is_success=False,
|
|
|
|
|
|
|
| 958 |
token_usage={"total_tokens": 0, "input_tokens": 0, "output_tokens": 0},
|
| 959 |
+
data={"pagewise_line_items": [], "total_item_count": 0},
|
| 960 |
+
|
| 961 |
)
|
| 962 |
file_bytes = resp.content
|
| 963 |
except Exception as e:
|
| 964 |
return BillResponse(
|
| 965 |
is_success=False,
|
|
|
|
|
|
|
| 966 |
token_usage={"total_tokens": 0, "input_tokens": 0, "output_tokens": 0},
|
| 967 |
+
data={"pagewise_line_items": [], "total_item_count": 0},
|
| 968 |
+
|
| 969 |
)
|
| 970 |
|
| 971 |
if not file_bytes:
|
| 972 |
return BillResponse(
|
| 973 |
is_success=False,
|
|
|
|
|
|
|
| 974 |
token_usage={"total_tokens": 0, "input_tokens": 0, "output_tokens": 0},
|
| 975 |
+
data={"pagewise_line_items": [], "total_item_count": 0},
|
| 976 |
+
|
| 977 |
)
|
| 978 |
|
| 979 |
logger.info(f"Processing with engine: {OCR_ENGINE}")
|
|
|
|
| 991 |
|
| 992 |
return BillResponse(
|
| 993 |
is_success=True,
|
| 994 |
+
token_usage={"total_tokens": 0, "input_tokens": 0, "output_tokens": 0},
|
| 995 |
data={
|
| 996 |
"pagewise_line_items": pages_dict,
|
| 997 |
"total_item_count": total_items,
|
| 998 |
},
|
| 999 |
+
|
| 1000 |
)
|
| 1001 |
|
| 1002 |
@app.get("/")
|