File size: 2,626 Bytes
75d9abf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0816a09
75d9abf
 
 
 
 
0816a09
75d9abf
 
 
 
 
 
5bde31b
75d9abf
 
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
from fastapi import FastAPI
from pydantic import BaseModel
import asyncio
from typing import List, Union
from ocr_main import *
import uvicorn
import logging
from datetime import datetime
import pytz
import torch
import json

logging.basicConfig(filename="ArabicOCR.log",
                    filemode='w')
logger = logging.getLogger("OCR")
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler("ArabicOCR.log")
logger.addHandler(file_handler)
total_done = 0
total_error = 0

app = FastAPI()

class Item(BaseModel):
    url: str

def get_bd_time():
    bd_timezone = pytz.timezone("Asia/Dhaka")
    time_now = datetime.now(bd_timezone)
    current_time = time_now.strftime("%I:%M:%S %p")
    return current_time


async def process_item(item: Item):
    try:
        result = await mainDet(item.url)
        result = json.loads(result)
        return result
    finally:
        torch.cuda.empty_cache()
        pass

async def process_items(items: Union[Item, List[Item]]):
    print(type(items))
    if type(items)==list:
        coroutines = [process_item(item) for item in items]
        results = await asyncio.gather(*coroutines)
        print("multi : ",results)
    else:
        results = await process_item(items)
        print("single : ", results)
    return results



@app.get("/status")
async def status():
    return "AI Server in running"

@app.post("/arabicOCR")
async def create_items(items: Union[Item, List[Item]]):
    try:
        # global total_done
        # total_done +=1
        results = await process_items(items)
        print("Result Sent to User:", results)
        print("###################################################################################################")
        print(items)
        print("Last Execution Time : ", get_bd_time())
        # logger.info(f"Time:{get_bd_time()}, Execution Done and Total Successfull Execution : {total_done}")
        return results
    except Exception as e:
        global total_error
        total_error += 1
        logger.info(f"Time:{get_bd_time()}, Execution Failed and Total Failed Execution : {total_error}, Payload:{items}, Error:{str(e)}")
        logger.error(str(e))
        return {"AI": f"Error: {str(e)}"}
    finally:
        global total_done
        total_done +=1
        logger.info(f"Time:{get_bd_time()}, Execution Done and Total Successfull Execution : {total_done}, Payload:{items}, Result:{results}")
        torch.cuda.empty_cache()
        pass

if __name__ == "__main__":
    try:
        del OCR_model
        uvicorn.run(app, host="127.0.0.1", port=8000)
    finally:
        torch.cuda.empty_cache()