Как запустить ML-модель с помощью Django и Celery
У меня есть проект Django, который использует модель("deepset/roberta-base-squad2") для составления некоторых прогнозов. Сервер получает запрос с параметрами, которые вызывают функцию очереди. Эта функция и делает предсказания.
views.py
class BotView(GenericAPIView):
serializer_class = BotSerializer
def post(self, request, *args, **kwargs):
try:
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
print(serializer.data)
return Response(data=serializer.data, status=status.HTTP_200_OK)
except Exception as e:
print(str(e))
return Response(data=str(e), status=status.HTTP_400_BAD_REQUEST)
serializers.py
class BotSerializer(serializers.Serializer):
questions = serializers.ListField(required=True, write_only=True)
user_info = serializers.CharField(required=True, write_only=True)
merchant = serializers.CharField(required=True, write_only=True)
user_id = serializers.IntegerField(required=True, write_only=True)
def create(self, validated_data):
# call ai and run async
upload_to_ai.delay(validated_data['questions'], validated_data['user_info'], validated_data['merchant'], validated_data['user_id'])
return "successful"
tasks.py
@shared_task()
def upload_to_ai(questions:list, user_info:str, merchant:str, user_id:int):
model_predictions = predict(questions, BotConfig.MODEL, user_info)
print(model_predictions)
return
QA_models.py
from haystack import Document
import pandas as pd
def predict(query:list, model, context):
'''
This function predicts the answer to question passed as query
Arguments:
query: This is/are the question you intend to ask
model: This is the model for the prediction
context: This is the data from which the model will find it's answers
'''
result = model.run_batch(queries=query,
documents=[Document(content=context)])
response = convert_to_dict(result['answers'], query)
return response
Каждый раз, когда я отправляю запрос, модели начинают работать, как показано на картинке, но никогда не переходят за 0%.
ЭТО ПОВЕДЕНИЕ ПРОИСХОДИТ ТОЛЬКО НА ХОСТИНГЕ, НО НИКОГДА В МОЕМ ЛОКАЛЬНОМ. DOCKER В МОЕМ ЛОКАЛЬНОМ ИМЕЕТ ТУ ЖЕ ПРОБЛЕМУ.