Skip to content

Commit

Permalink
Merge pull request #57 from CogitoNTNU/8-flashcards-and-memory-aids
Browse files Browse the repository at this point in the history
8 flashcards and memory aids
  • Loading branch information
SverreNystad authored Feb 29, 2024
2 parents dcd3966 + 21a21d5 commit 51c19c8
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 132 deletions.
4 changes: 2 additions & 2 deletions backend/api/urls.py
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"),
]
2 changes: 1 addition & 1 deletion backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#a class for defining the config variables
class Config():
def __init__(self, path='config.env', gpt_model="gpt-3.5-turbo"):
def __init__(self, path='.env', gpt_model="gpt-3.5-turbo"):
self.path = path
self.GPT_MODEL = gpt_model
load_dotenv(dotenv_path=path)
Expand Down
Empty file removed backend/documents/__init__.py
Empty file.
3 changes: 0 additions & 3 deletions backend/documents/admin.py

This file was deleted.

6 changes: 0 additions & 6 deletions backend/documents/apps.py

This file was deleted.

Empty file.
3 changes: 0 additions & 3 deletions backend/documents/models.py

This file was deleted.

35 changes: 0 additions & 35 deletions backend/documents/tests.py

This file was deleted.

66 changes: 0 additions & 66 deletions backend/documents/views.py

This file was deleted.

11 changes: 8 additions & 3 deletions backend/flashcards/convert_pdf_to_txt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from pdfminer.high_level import extract_text
import chardet

def convert_pdf_to_txt(pdf_file):
"""Convert a PDF file to text and return the path to the text file.
Expand All @@ -10,6 +10,11 @@ def convert_pdf_to_txt(pdf_file):
Returns:
str: Text content of PDF file.
"""
# Extract text from the PDF file
text = extract_text(pdf_file, codec='utf-8')
# The pdf_file from the input is a django.core.files.uploadedfile.InMemoryUploadedFile.
# Extract the text content of the file.

file_content_bytes = pdf_file.read()
encoding = chardet.detect(file_content_bytes)["encoding"]
text = file_content_bytes.decode(encoding)

return text
67 changes: 56 additions & 11 deletions backend/flashcards/views.py
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)
2 changes: 2 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ anyio==4.2.0
asgiref==3.7.2
certifi==2023.11.17
cffi==1.16.0
chardet==5.2.0
charset-normalizer==3.3.2
colorama==0.4.6
cryptography==41.0.7
Expand All @@ -13,6 +14,7 @@ django-rest-framework==0.1.0
djangorestframework==3.14.0
djangorestframework-simplejwt==5.3.1
drf-yasg==1.21.7
exceptiongroup==1.2.0
h11==0.14.0
httpcore==1.0.2
httpx==0.26.0
Expand Down
1 change: 0 additions & 1 deletion backend/tutorai/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
# Own applications
"api",
"users",
"documents",
"flashcards",
]

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/routes/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const AppRoutes = () => {
return (
<Routes>
<Route path="/" element={<Login/>} />
<Route path="/upload" element={<Upload/>} />
<Route path="/create-flashcards" element={<Upload/>} />
<Route path="/login" element={<Login/>} />
<Route path="/signup" element={<Signup/>} />
<Route path="*" element={<Page404 />} />
Expand Down

0 comments on commit 51c19c8

Please sign in to comment.