File size: 2,826 Bytes
f67d0b5 ac598ad f67d0b5 ac598ad f67d0b5 ac598ad f67d0b5 ac598ad f67d0b5 ac598ad |
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 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import asyncio
from typing import List, Union
from face_main import *
from datetime import datetime
from face_main import *
import uvicorn
import logging
import pytz
import torch
import json
logging.basicConfig(filename="HDML-FaceDetection.log",
filemode='w')
logger = logging.getLogger("HDML")
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler("HDML-FaceDetection.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
except Exception as e:
raise ValueError(f"process_item ERROR : {str(e)}")
finally:
torch.cuda.empty_cache()
async def process_items(items: Union[Item, List[Item]]):
try:
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
except Exception as e:
raise ValueError(f"process_items ERROR : {str(e)}")
finally:
torch.cuda.empty_cache()
@app.get("/status")
async def status():
return "AI Server in running"
@app.post("/tech")
async def create_items(items: Union[Item, List[Item]]):
try:
results = await process_items(items)
print("Result Sent to User:", results)
print("###################################################################################################")
print(items)
print("Last Execution Time : ", get_bd_time())
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))
raise ValueError(f"process_item 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()
if __name__ == "__main__":
try:
del faceModel
uvicorn.run(app, host="127.0.0.1", port=8585)
except Exception as e:
raise ValueError(f"face_api ERROR : {str(e)}")
finally:
torch.cuda.empty_cache() |