generated from SverreNystad/template_python_application
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from CogitoNTNU/8-flashcards-and-memory-aids
8 flashcards and memory aids
- Loading branch information
Showing
14 changed files
with
70 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
from django.urls import path | ||
|
||
from users.views import login, register_user | ||
from flashcards.views import generate_mock_flashcard | ||
from documents.views import create_flashcards | ||
from flashcards.views import create_flashcards, generate_mock_flashcard | ||
|
||
urlpatterns = [ | ||
path("create-user/", register_user, name="create-user"), | ||
path("login/", login, name="login"), | ||
path("generate-mock-flashcard/", generate_mock_flashcard, name="generate-mock-flashcards"), | ||
path("create-flashcards/", create_flashcards, name="create-flashcards"), | ||
path("generate-mock-flashcard/", generate_mock_flashcard, name="generate-mock-flashcard"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,82 @@ | ||
from django.core.files.storage import default_storage | ||
from django.http import JsonResponse | ||
from django.shortcuts import render | ||
from flashcards.textToFlashcards import generate_flashcards, parse_flashcard | ||
|
||
from rest_framework.decorators import api_view | ||
from rest_framework.decorators import api_view, parser_classes | ||
from rest_framework.parsers import MultiPartParser | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
|
||
from .textToFlashcards import generate_flashcards, parse_flashcard | ||
from .convert_pdf_to_txt import convert_pdf_to_txt | ||
|
||
from drf_yasg.utils import swagger_auto_schema | ||
from drf_yasg import openapi | ||
|
||
#Flashcard view | ||
get_mock_flashcard_error_response = openapi.Response( | ||
get_flashcard_error_response = openapi.Response( | ||
description="Error generating flashcards", | ||
examples={"application/json": {"message": "Error generating flashcards"}}, | ||
) | ||
|
||
get_mock_flashcard_success_response = openapi.Response( | ||
get_flashcard_success_response = openapi.Response( | ||
description="Flashcards generated successfully", | ||
examples={"application/json": [{"front": "What is the capital of India?", "back": "New Delhi"}]}, | ||
) | ||
|
||
@swagger_auto_schema( | ||
method="get", | ||
method="post", | ||
operation_description="Generate flashcards from a given text", | ||
tags=["Flashcards"], | ||
responses={200: get_mock_flashcard_error_response, 400: get_mock_flashcard_error_response}, | ||
) | ||
@api_view(["GET"]) | ||
def generate_mock_flashcard(request): | ||
flashcards = generate_flashcards() | ||
responses={200: get_flashcard_success_response, 400: get_flashcard_error_response}, | ||
) | ||
|
||
@api_view(["POST"]) | ||
def create_flashcards(request): | ||
# Check if the request has multipart content type | ||
|
||
|
||
if not request.content_type.startswith('multipart/form-data'): | ||
return Response( | ||
{"message": "The uploaded file is not in the correct format"}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
pdf_file = request.FILES["pdf"] | ||
if not pdf_file: | ||
print("No PDF file uploaded", flush=True) | ||
return Response( | ||
{"message": "No PDF file uploaded"}, | ||
status=status.HTTP_400_BAD_REQUEST, | ||
) | ||
|
||
print("PDF file uploaded successfully", flush=True) | ||
|
||
# Convert the pdf file to text | ||
text = convert_pdf_to_txt(pdf_file) | ||
|
||
# TODO: split the text into paragraphs before generating flashcards | ||
text = text[:100] | ||
|
||
flashcards = generate_flashcards(text) | ||
flashcards = parse_flashcard(flashcards) | ||
|
||
if flashcards == "": | ||
return Response( | ||
{"message": "Error generating flashcards"}, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
return Response(flashcards, status=status.HTTP_200_OK) | ||
|
||
|
||
@swagger_auto_schema( | ||
method="get", | ||
operation_description="Generate flashcards from a predefined text", | ||
tags=["Flashcards"], | ||
responses={200: get_flashcard_success_response, 400: get_flashcard_error_response}, | ||
) | ||
|
||
@api_view(["GET"]) | ||
def generate_mock_flashcard(request): | ||
flashcards = generate_flashcards() | ||
# flashcards = parse_flashcard(flashcards) | ||
|
||
return Response(flashcards, status=status.HTTP_200_OK) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,6 @@ | |
# Own applications | ||
"api", | ||
"users", | ||
"documents", | ||
"flashcards", | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters