Spaces:
Build error
Build error
Update api.py
Browse files
api.py
CHANGED
|
@@ -1,123 +1,124 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
-
import cv2
|
| 4 |
-
import os
|
| 5 |
-
import pandas as pd
|
| 6 |
-
import numpy as np
|
| 7 |
-
from typing import List, Any
|
| 8 |
-
from pydantic import BaseModel
|
| 9 |
-
from pymongo import MongoClient
|
| 10 |
-
import dns.resolver
|
| 11 |
-
from pymongo.server_api import ServerApi
|
| 12 |
-
from datetime import datetime
|
| 13 |
-
import base64
|
| 14 |
-
from detect import detect_with_yolo
|
| 15 |
-
from tableocr import get_list
|
| 16 |
-
from deskew import deskew
|
| 17 |
-
from read_data import get_text
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
|
| 21 |
-
|
| 22 |
-
app = FastAPI()
|
| 23 |
-
|
| 24 |
-
dns.resolver.default_resolver=dns.resolver.Resolver(configure=False)
|
| 25 |
-
dns.resolver.default_resolver.nameservers=['8.8.8.8']
|
| 26 |
-
|
| 27 |
-
uri = "mongodb+srv://senel1806:dEWqI0FVFMDRpiFl@cluster0.f2kg7rn.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
|
| 28 |
-
client = MongoClient(uri, server_api=ServerApi('1'))
|
| 29 |
-
db = client['invoices']
|
| 30 |
-
collection = db['invoices']
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
class ProcessedData(BaseModel):
|
| 34 |
-
company_name: str
|
| 35 |
-
invoice_number: str
|
| 36 |
-
invoice_date: str
|
| 37 |
-
table_data: List[Any]
|
| 38 |
-
|
| 39 |
-
class ImageData(BaseModel):
|
| 40 |
-
image: str
|
| 41 |
-
|
| 42 |
-
class BatchProcessedData(BaseModel):
|
| 43 |
-
results: List[ProcessedData]
|
| 44 |
-
|
| 45 |
-
class Invoice(BaseModel):
|
| 46 |
-
code: str
|
| 47 |
-
timestamp: str
|
| 48 |
-
date: str
|
| 49 |
-
isTransferred: bool
|
| 50 |
-
images: List[Any]
|
| 51 |
-
data: List[Any]
|
| 52 |
-
|
| 53 |
-
def extractData(input_img):
|
| 54 |
-
result_img, table_img, company_img, number_img, date_img = detect_with_yolo(input_img)
|
| 55 |
-
|
| 56 |
-
# Şirket adı
|
| 57 |
-
company_name = "not detected"
|
| 58 |
-
if company_img
|
| 59 |
-
company_name = get_text(company_img)
|
| 60 |
-
|
| 61 |
-
# Fatura numarası
|
| 62 |
-
invoice_number = "not detected"
|
| 63 |
-
if number_img
|
| 64 |
-
invoice_number = get_text(number_img)
|
| 65 |
-
|
| 66 |
-
# Fatura tarihi
|
| 67 |
-
invoice_date = "not detected"
|
| 68 |
-
if date_img
|
| 69 |
-
invoice_date = get_text(date_img)
|
| 70 |
-
|
| 71 |
-
# Eğer tablo tespit edilmediyse, data boş olacak
|
| 72 |
-
if table_img
|
| 73 |
-
table_img = deskew(table_img)
|
| 74 |
-
data = get_list(table_img)
|
| 75 |
-
max_columns = max(len(row) for row in data)
|
| 76 |
-
column_names = [f"Column{i+1}" for i in range(max_columns)] # Column1, Column2, ... şeklinde sütun adları oluştur
|
| 77 |
-
df = pd.DataFrame(data, columns=column_names)
|
| 78 |
-
else:
|
| 79 |
-
df = pd.DataFrame() # Eğer tablo tespit edilmezse boş bir DataFrame döndür
|
| 80 |
-
|
| 81 |
-
return df, company_name, invoice_number, invoice_date
|
| 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 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import cv2
|
| 4 |
+
import os
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import numpy as np
|
| 7 |
+
from typing import List, Any
|
| 8 |
+
from pydantic import BaseModel
|
| 9 |
+
from pymongo import MongoClient
|
| 10 |
+
import dns.resolver
|
| 11 |
+
from pymongo.server_api import ServerApi
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
import base64
|
| 14 |
+
from detect import detect_with_yolo
|
| 15 |
+
from tableocr import get_list
|
| 16 |
+
from deskew import deskew
|
| 17 |
+
from read_data import get_text
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
|
| 21 |
+
|
| 22 |
+
app = FastAPI()
|
| 23 |
+
|
| 24 |
+
dns.resolver.default_resolver=dns.resolver.Resolver(configure=False)
|
| 25 |
+
dns.resolver.default_resolver.nameservers=['8.8.8.8']
|
| 26 |
+
|
| 27 |
+
uri = "mongodb+srv://senel1806:dEWqI0FVFMDRpiFl@cluster0.f2kg7rn.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
|
| 28 |
+
client = MongoClient(uri, server_api=ServerApi('1'))
|
| 29 |
+
db = client['invoices']
|
| 30 |
+
collection = db['invoices']
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class ProcessedData(BaseModel):
|
| 34 |
+
company_name: str
|
| 35 |
+
invoice_number: str
|
| 36 |
+
invoice_date: str
|
| 37 |
+
table_data: List[Any]
|
| 38 |
+
|
| 39 |
+
class ImageData(BaseModel):
|
| 40 |
+
image: str
|
| 41 |
+
|
| 42 |
+
class BatchProcessedData(BaseModel):
|
| 43 |
+
results: List[ProcessedData]
|
| 44 |
+
|
| 45 |
+
class Invoice(BaseModel):
|
| 46 |
+
code: str
|
| 47 |
+
timestamp: str
|
| 48 |
+
date: str
|
| 49 |
+
isTransferred: bool
|
| 50 |
+
images: List[Any]
|
| 51 |
+
data: List[Any]
|
| 52 |
+
|
| 53 |
+
def extractData(input_img):
|
| 54 |
+
result_img, table_img, company_img, number_img, date_img = detect_with_yolo(input_img)
|
| 55 |
+
|
| 56 |
+
# Şirket adı
|
| 57 |
+
company_name = "not detected"
|
| 58 |
+
if isinstance(company_img, np.ndarray) and company_img.size > 0:
|
| 59 |
+
company_name = get_text(company_img)
|
| 60 |
+
|
| 61 |
+
# Fatura numarası
|
| 62 |
+
invoice_number = "not detected"
|
| 63 |
+
if isinstance(number_img, np.ndarray) and number_img.size > 0:
|
| 64 |
+
invoice_number = get_text(number_img)
|
| 65 |
+
|
| 66 |
+
# Fatura tarihi
|
| 67 |
+
invoice_date = "not detected"
|
| 68 |
+
if isinstance(date_img, np.ndarray) and date_img.size > 0:
|
| 69 |
+
invoice_date = get_text(date_img)
|
| 70 |
+
|
| 71 |
+
# Eğer tablo tespit edilmediyse, data boş olacak
|
| 72 |
+
if isinstance(table_img, np.ndarray) and table_img.size > 0:
|
| 73 |
+
table_img = deskew(table_img)
|
| 74 |
+
data = get_list(table_img)
|
| 75 |
+
max_columns = max(len(row) for row in data)
|
| 76 |
+
column_names = [f"Column{i+1}" for i in range(max_columns)] # Column1, Column2, ... şeklinde sütun adları oluştur
|
| 77 |
+
df = pd.DataFrame(data, columns=column_names)
|
| 78 |
+
else:
|
| 79 |
+
df = pd.DataFrame() # Eğer tablo tespit edilmezse boş bir DataFrame döndür
|
| 80 |
+
|
| 81 |
+
return df, company_name, invoice_number, invoice_date
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
@app.post("/process-images/", response_model=BatchProcessedData)
|
| 85 |
+
async def process_images(image_data_list: List[ImageData]):
|
| 86 |
+
if not image_data_list:
|
| 87 |
+
raise HTTPException(status_code=400, detail="Image data list cannot be empty.")
|
| 88 |
+
|
| 89 |
+
results = []
|
| 90 |
+
for image_data in image_data_list:
|
| 91 |
+
image_bytes = base64.b64decode(image_data.image)
|
| 92 |
+
nparr = np.frombuffer(image_bytes, np.uint8)
|
| 93 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 94 |
+
|
| 95 |
+
df, company_name, invoice_number, invoice_date = extractData(img)
|
| 96 |
+
|
| 97 |
+
if df.empty:
|
| 98 |
+
table_data = "not detected" # Eğer tablo tespit edilmediyse 'not detected' döndür
|
| 99 |
+
else:
|
| 100 |
+
table_data = df.to_dict(orient='records')
|
| 101 |
+
|
| 102 |
+
response_data = ProcessedData(
|
| 103 |
+
company_name=company_name,
|
| 104 |
+
invoice_number=invoice_number,
|
| 105 |
+
invoice_date=invoice_date,
|
| 106 |
+
table_data=table_data
|
| 107 |
+
)
|
| 108 |
+
results.append(response_data.model_dump()) # Use model_dump instead of dict
|
| 109 |
+
|
| 110 |
+
return JSONResponse(content=results)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
@app.post("/save-invoice")
|
| 114 |
+
async def save_invoice(invoice: Invoice, request: Request):
|
| 115 |
+
try:
|
| 116 |
+
invoice_data = await request.json()
|
| 117 |
+
print("Received payload:", invoice_data) # Log the payload
|
| 118 |
+
invoice = Invoice(**invoice_data) # Validate the payload using Pydantic
|
| 119 |
+
collection.insert_one(invoice.model_dump())
|
| 120 |
+
return {"message": "Invoice saved successfully"}
|
| 121 |
+
except ValidationError as e:
|
| 122 |
+
raise HTTPException(status_code=422, detail=e.errors())
|
| 123 |
+
except Exception as e:
|
| 124 |
raise HTTPException(status_code=500, detail=str(e))
|