File size: 1,810 Bytes
07d2322 | 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 | from django.shortcuts import render
# Create your views here.
from django.http import JsonResponse
from django.views import View
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from typing import Union, List
from rest_framework.parsers import JSONParser
from api.serializers import ItemSerializer
from main.main import mainDet
from api.models import ImageURL
import json
import asyncio
import torch
async def process_item(item):
try:
result = await mainDet(item)
result = json.loads(result)
return result
finally:
torch.cuda.empty_cache()
pass
async def process_items(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
@method_decorator(csrf_exempt, name="dispatch")
class status(View):
async def get(self, request):
return JsonResponse({"Status":"AI Server is Running"}, status=200)
@method_decorator(csrf_exempt, name="dispatch")
class body(View):
async def post(self, request):
try:
data = json.loads(request.body.decode("utf-8"))
# print(data)
except json.JSONDecodeError:
return JsonResponse({"ERROR":"Invalid Body"}, status=400)
serializer = ItemSerializer(data=data)
if serializer.is_valid():
# print(serializer)
result = await process_items(serializer.validated_data["url"])
return JsonResponse(result, safe=False, status=200)
else:
return JsonResponse(serializer.errors,status=400)
|