Spaces:
Sleeping
Sleeping
This file was not needed
Browse files- inference.py +0 -85
inference.py
DELETED
@@ -1,85 +0,0 @@
|
|
1 |
-
from fastapi import APIRouter, File, UploadFile, Form
|
2 |
-
from typing import Optional
|
3 |
-
from PIL import Image
|
4 |
-
import urllib.request
|
5 |
-
from io import BytesIO
|
6 |
-
from config import settings
|
7 |
-
import utils
|
8 |
-
import os
|
9 |
-
import json
|
10 |
-
from routers.donut_inference import process_document_donut
|
11 |
-
|
12 |
-
|
13 |
-
router = APIRouter()
|
14 |
-
|
15 |
-
def count_values(obj):
|
16 |
-
if isinstance(obj, dict):
|
17 |
-
count = 0
|
18 |
-
for value in obj.values():
|
19 |
-
count += count_values(value)
|
20 |
-
return count
|
21 |
-
elif isinstance(obj, list):
|
22 |
-
count = 0
|
23 |
-
for item in obj:
|
24 |
-
count += count_values(item)
|
25 |
-
return count
|
26 |
-
else:
|
27 |
-
return 1
|
28 |
-
|
29 |
-
|
30 |
-
@router.post("/inference")
|
31 |
-
async def run_inference(file: Optional[UploadFile] = File(None), image_url: Optional[str] = Form(None),
|
32 |
-
model_in_use: str = Form('donut')):
|
33 |
-
|
34 |
-
# if sparrow_key != settings.sparrow_key:
|
35 |
-
# return {"error": "Invalid Sparrow key."}
|
36 |
-
|
37 |
-
result = []
|
38 |
-
if file:
|
39 |
-
# Ensure the uploaded file is a JPG image
|
40 |
-
if file.content_type not in ["image/jpeg", "image/jpg"]:
|
41 |
-
return {"error": "Invalid file type. Only JPG images are allowed."}
|
42 |
-
|
43 |
-
image = Image.open(BytesIO(await file.read()))
|
44 |
-
processing_time = 0
|
45 |
-
if model_in_use == 'donut':
|
46 |
-
result, processing_time = process_document_donut(image)
|
47 |
-
utils.log_stats(settings.inference_stats_file, [processing_time, count_values(result), file.filename, settings.model])
|
48 |
-
print(f"Processing time inference: {processing_time:.2f} seconds")
|
49 |
-
elif image_url:
|
50 |
-
# test image url: https://raw.githubusercontent.com/katanaml/sparrow/main/sparrow-data/docs/input/invoices/processed/images/invoice_10.jpg
|
51 |
-
with urllib.request.urlopen(image_url) as response:
|
52 |
-
content_type = response.info().get_content_type()
|
53 |
-
if content_type in ["image/jpeg", "image/jpg"]:
|
54 |
-
image = Image.open(BytesIO(response.read()))
|
55 |
-
else:
|
56 |
-
return {"error": "Invalid file type. Only JPG images are allowed."}
|
57 |
-
|
58 |
-
processing_time = 0
|
59 |
-
if model_in_use == 'donut':
|
60 |
-
result, processing_time = process_document_donut(image)
|
61 |
-
# parse file name from url
|
62 |
-
file_name = image_url.split("/")[-1]
|
63 |
-
utils.log_stats(settings.inference_stats_file, [processing_time, count_values(result), file_name, settings.model])
|
64 |
-
print(f"Processing time inference: {processing_time:.2f} seconds")
|
65 |
-
else:
|
66 |
-
result = {"info": "No input provided"}
|
67 |
-
|
68 |
-
return result
|
69 |
-
|
70 |
-
|
71 |
-
@router.get("/statistics")
|
72 |
-
async def get_statistics():
|
73 |
-
file_path = settings.inference_stats_file
|
74 |
-
|
75 |
-
# Check if the file exists, and read its content
|
76 |
-
if os.path.exists(file_path):
|
77 |
-
with open(file_path, 'r') as file:
|
78 |
-
try:
|
79 |
-
content = json.load(file)
|
80 |
-
except json.JSONDecodeError:
|
81 |
-
content = []
|
82 |
-
else:
|
83 |
-
content = []
|
84 |
-
|
85 |
-
return content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|