diff --git a/.github/workflows/django.yml b/.github/workflows/django.yml index c232f0c9..ecb8d13b 100644 --- a/.github/workflows/django.yml +++ b/.github/workflows/django.yml @@ -14,6 +14,8 @@ jobs: steps: - uses: actions/checkout@v3 - name: Build docker image and run tests + env: + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} run: | cd backend docker compose build diff --git a/README.md b/README.md index 2146b84b..0e08db14 100644 --- a/README.md +++ b/README.md @@ -115,8 +115,16 @@ For secure and efficient management of environment-specific variables, TutorAI u ``` 2. **Add Environment Variables:** + You will need to add the following environment variables to the `.env` file: + - OPENAI_API_KEY: Your OpenAI API key + - POSTGRES_DB: The name of the database + - POSTGRES_USER: The username of the database + - POSTGRES_PASSWORD: The password of the database + - POSTGRES_HOST: The host of the database + - POSTGRES_PORT: The port of the database + ```bash - echo "API_KEY=YOUR_API_KEY" > .env # Remember to change YOUR_API_KEY to your actual API key + echo "OPENAI_API_KEY=YOUR_API_KEY" > .env # Remember to change YOUR_API_KEY to your actual API key ``` 3. **Obtaining an API Key:** diff --git a/backend/README.md b/backend/README.md index f81ef492..22109fbb 100644 --- a/backend/README.md +++ b/backend/README.md @@ -65,6 +65,33 @@ http://127.0.0.1:8000/admin/ Enjoy! +## When making changes to the models + +When making changes to the models, you need to create a migration file and migrate the database. + +```bash +docker-compose run tutorai python manage.py makemigrations +docker-compose run tutorai python manage.py migrate +``` + +## If the migrations are not working + +One need to delete migrations files in migrations folders. Shut down the docker-compose and remove the database volume. Then run the following commands: + +```bash +docker-compose down +docker volume rm backend_postgres_data +``` +Then you can apply the migrations again. +```bash +docker-compose run tutorai python manage.py makemigrations +docker-compose run tutorai python manage.py migrate +``` +and then run the docker-compose again. +```bash +docker-compose up +``` + ## How to create a new application in the backend To create a new application This command creates a new directory named "api" within your "backend" directory, along with the basic files needed for a Django app. diff --git a/backend/api/urls.py b/backend/api/urls.py index eb12448c..d9ebce91 100644 --- a/backend/api/urls.py +++ b/backend/api/urls.py @@ -3,10 +3,12 @@ from users.views import login, register_user from documents.views import upload_pdf from flashcards.views import generate_mock_flashcard +from documents.views import create_flashcards urlpatterns = [ path("create-user/", register_user, name="create-user"), path("login/", login, name="login"), path("upload/", upload_pdf, name="upload"), - path("generate-mock-flashcard/", generate_mock_flashcard, name="generate-mock.flashcards"), + path("generate-mock-flashcard/", generate_mock_flashcard, name="generate-mock-flashcards"), + path("create-flashcards/", create_flashcards, name="create-flashcards"), ] diff --git a/backend/docker-compose.yaml b/backend/docker-compose.yaml index a330368d..689e5410 100644 --- a/backend/docker-compose.yaml +++ b/backend/docker-compose.yaml @@ -19,6 +19,7 @@ services: - DATABASE_NAME=tutoraidb - DATABASE_USER=tutoraiuser - DATABASE_PASSWORD=tutoraipassword + - OPENAI_API_KEY=${OPENAI_API_KEY} db: image: postgres:13 diff --git a/backend/documents/views.py b/backend/documents/views.py index fa36bea0..a66b308d 100644 --- a/backend/documents/views.py +++ b/backend/documents/views.py @@ -2,32 +2,65 @@ from rest_framework import status from rest_framework.response import Response from rest_framework.decorators import api_view +from drf_yasg.utils import swagger_auto_schema +from drf_yasg import openapi +# Flashcard creation view +create_flashcards_success_response = openapi.Response( + description="Create flashcards successfully", + examples={ + "application/json": { + "message": "Flashcards created successfully", + "flashcards": [ + { + "front": "What is the capital of India?", + "back": "New Delhi", + }, + { + "front": "What is the capital of USA?", + "back": "Washington DC", + }, + ], + } + }, +) + +create_flashcards_error_response = openapi.Response( + description="There was a problem creating flashcards", + examples={ + "application/json": {"message": "There was a problem creating flashcards"} + }, +) + + +@swagger_auto_schema( + method="post", + operation_description="Login to the application", + tags=["Document Management"], + response_description="Returns the list of flashcards created", + responses={ + 200: create_flashcards_success_response, + 400: create_flashcards_error_response, + }, +) @api_view(["POST"]) -def upload_pdf(request): +def create_flashcards(request): print(request.data, flush=True) - if "pdf" in request.FILES: - pdf_file = request.FILES.get("pdf") - print(request.FILES.get("pdf"), flush=True) - print(pdf_file.content_type, flush=True) - # Check if the uploaded file is a PDF - if pdf_file.content_type != "multipart/form-data": - return Response( - {"message": "The uploaded file is not a PDF"}, - status=status.HTTP_400_BAD_REQUEST, - ) - - print(pdf_file, flush=True) - # Process or save pdf_file as needed - # ... - + # Check if the uploaded file is a PDF + if request.content_type != "application/pdf": return Response( - {"message": "PDF uploaded successfully!"}, status=status.HTTP_201_CREATED - ) - else: - return Response( - {"message": "No PDF file found in request"}, + {"message": "The uploaded file is not a PDF"}, status=status.HTTP_400_BAD_REQUEST, ) + + pdf_file = request.FILES.get("pdf") + + print(pdf_file, flush=True) + # Process or save pdf_file as needed + # ... + + return Response( + {"message": "PDF uploaded successfully!"}, status=status.HTTP_201_CREATED + ) diff --git a/frontend/src/routes/routesDefinitions.ts b/frontend/src/routes/routesDefinitions.ts index 7e1d7298..c68e4e04 100644 --- a/frontend/src/routes/routesDefinitions.ts +++ b/frontend/src/routes/routesDefinitions.ts @@ -1,7 +1,7 @@ -const baseAPIUrl: string = "http://127.0.0.1:8000/api"; +const baseAPIUrl: string = "http://localhost:8000/api"; const apiRoutes = { - upload: `${baseAPIUrl}/upload/`, + createFlashcards: `${baseAPIUrl}/create-flashcards/`, login: `${baseAPIUrl}/login/`, signup: `${baseAPIUrl}/create-user/`, }; diff --git a/frontend/src/services/uploadService.ts b/frontend/src/services/uploadService.ts index a103286e..7f4e7b01 100644 --- a/frontend/src/services/uploadService.ts +++ b/frontend/src/services/uploadService.ts @@ -11,7 +11,7 @@ const uploadPDF = async (file: File): Promise => { } const response = await axios - .post(apiRoutes.upload, { + .post(apiRoutes.createFlashcards, { method: "POST", body: formData, // Add headers if necessary (e.g., for authentication) diff --git a/frontend/src/types/Documents.ts b/frontend/src/types/Documents.ts new file mode 100644 index 00000000..41478685 --- /dev/null +++ b/frontend/src/types/Documents.ts @@ -0,0 +1,6 @@ +type Flashcard = { + front: string; + back: string; +}; + +export type { Flashcard };