rakib72642 commited on
Commit
4d7fcdf
·
1 Parent(s): 13a39e5

upload all for testing

Browse files
AI_Model/testModel.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8f6cdb9e17e9bf1494749a6d249a51d9418c5ab4cf9e5dc55f028274e603b385
3
+ size 195232657
__pycache__/test_main.cpython-311.pyc ADDED
Binary file (3.11 kB). View file
 
data/__pycache__/model.cpython-311.pyc ADDED
Binary file (302 Bytes). View file
 
data/model.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from ultralytics import YOLO
2
+
3
+ testModel = YOLO('AI_Model/testModel.pt')
test_api.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ import asyncio
4
+ from typing import List, Union
5
+ from test_main import mainDet
6
+ import uvicorn
7
+ import logging
8
+ from datetime import datetime
9
+ import pytz
10
+ import torch
11
+ import json
12
+
13
+ app = FastAPI()
14
+
15
+ class Item(BaseModel):
16
+ url: str
17
+
18
+ def get_bd_time():
19
+ bd_timezone = pytz.timezone("Asia/Dhaka")
20
+ time_now = datetime.now(bd_timezone)
21
+ current_time = time_now.strftime("%I:%M:%S %p")
22
+ return current_time
23
+
24
+
25
+ async def process_item(item: Item):
26
+ try:
27
+ result = await mainDet(item.url)
28
+ result = json.loads(result)
29
+ return result
30
+ finally:
31
+ torch.cuda.empty_cache()
32
+ pass
33
+
34
+
35
+ @app.get("/status")
36
+ async def status():
37
+ return "AI Server in running"
38
+
39
+ @app.post("/Test_bkash_sticker_count")
40
+ async def create_items(items: Union[Item, List[Item]]):
41
+ try:
42
+ results = await process_item(items)
43
+ print("Result Sent to User:", results)
44
+ print("###################################################################################################")
45
+ print(items)
46
+ print("Last Execution Time : ", get_bd_time())
47
+ return results
48
+ except Exception as e:
49
+ return {"AI": f"Error: {str(e)}"}
50
+ finally:
51
+ torch.cuda.empty_cache()
52
+ pass
53
+
54
+ if __name__ == "__main__":
55
+ try:
56
+ uvicorn.run(app, host="127.0.0.1", port=8000)
57
+ finally:
58
+ torch.cuda.empty_cache()
59
+
test_main.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import pandas as pd
3
+ from PIL import Image
4
+ from aiohttp import ClientSession
5
+ from io import BytesIO
6
+ import asyncio
7
+ from data.model import testModel
8
+
9
+ async def getImage(img_url, session):
10
+ async with session.get(img_url) as response:
11
+ img_data = await response.read()
12
+ return BytesIO(img_data)
13
+
14
+
15
+ async def detection(model,img_content,confidence):
16
+ img = Image.open(img_content)
17
+ # result = model(img)
18
+ result = model(source=img,device=0,conf=confidence)
19
+ detection = {}
20
+ data = json.loads(result[0].tojson())
21
+ if len(data) == 0:
22
+ res = {"AI": "No Detection"}
23
+ detection.update(res)
24
+ else:
25
+ df = pd.DataFrame(data)
26
+ name_counts = df['name'].value_counts().sort_index()
27
+
28
+ for name, count in name_counts.items():
29
+ res = {name: count}
30
+ detection.update(res)
31
+ return detection
32
+
33
+ async def mainDet(url):
34
+ async with ClientSession() as session:
35
+ image = await asyncio.create_task(getImage(url, session))
36
+ nbrtuDict = await asyncio.create_task(detection(testModel, image,0.6))
37
+
38
+ nagad_result = json.dumps({"sticker count test":nbrtuDict})
39
+
40
+
41
+ return nagad_result