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 rest_framework.parsers import JSONParser from .serializers import ItemSerializer from .logic import main_detection from .models import ImageUrl import json @method_decorator(csrf_exempt, name='dispatch') class StatusView(View): def get(self, request): return JsonResponse({"status": "AI Server is running"}, status=200) @method_decorator(csrf_exempt, name='dispatch') class TechView(View): async def post(self, request): try: data = json.loads(request.body.decode('utf-8')) # Correct way to parse JSON data except json.JSONDecodeError: return JsonResponse({'error': 'Invalid JSON'}, status=400) serializer = ItemSerializer(data=data) if serializer.is_valid(): # Assuming `main_detection` is an asynchronous function you've set up result = await main_detection(serializer.validated_data['url']) return JsonResponse(result, safe=False, status=200) else: return JsonResponse(serializer.errors, status=400)