add markov RPC method for learning a line
This commit is contained in:
parent
9e4bc595a4
commit
802072caed
@ -2,11 +2,12 @@
|
||||
from django.urls import path
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from markov.views import context_index, rpc_generate_line_for_context
|
||||
from markov.views import context_index, rpc_generate_line_for_context, rpc_learn_line_for_context
|
||||
|
||||
urlpatterns = [
|
||||
path('', TemplateView.as_view(template_name='index.html'), name='markov_index'),
|
||||
path('context/<int:context_id>/', context_index, name='markov_context_index'),
|
||||
|
||||
path('rpc/context/<context>/generate/', rpc_generate_line_for_context, name='markov_rpc_generate_line'),
|
||||
path('rpc/context/<context>/learn/', rpc_learn_line_for_context, name='markov_rpc_learn_line'),
|
||||
]
|
||||
|
@ -61,3 +61,22 @@ def rpc_generate_line_for_context(request, context):
|
||||
'context': context, 'topics': topics,
|
||||
'generated_line': ' '.join(generated_words), 'generated_words': generated_words
|
||||
})
|
||||
|
||||
|
||||
@api_view(['POST'])
|
||||
@authentication_classes((BasicAuthentication, ))
|
||||
@permission_classes((IsAuthenticated, ))
|
||||
def rpc_learn_line_for_context(request, context):
|
||||
"""Learn a line for a given context."""
|
||||
if request.method != 'POST':
|
||||
return Response({'detail': "Supported method: POST."}, status=405)
|
||||
|
||||
try:
|
||||
markov_data = json.loads(request.body)
|
||||
line = markov_data.get('line', [])
|
||||
except (json.decoder.JSONDecodeError, KeyError):
|
||||
return Response({'detail': "Request body must be JSON with a 'line' parameter."}, status=400)
|
||||
|
||||
context_id = markovlib.get_or_create_target_context(context)
|
||||
markovlib.learn_line(line, context_id)
|
||||
return Response({'status': "OK", 'context': context, 'line': line})
|
||||
|
Loading…
Reference in New Issue
Block a user