Skip to content

Commit

Permalink
Merge branch 'main' into 8-flashcards-and-memory-aids
Browse files Browse the repository at this point in the history
  • Loading branch information
SverreNystad authored Jan 14, 2024
2 parents 2e3d4f0 + d00a860 commit f9c1534
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down
27 changes: 27 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion backend/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
]
1 change: 1 addition & 0 deletions backend/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ services:
- DATABASE_NAME=tutoraidb
- DATABASE_USER=tutoraiuser
- DATABASE_PASSWORD=tutoraipassword
- OPENAI_API_KEY=${OPENAI_API_KEY}

db:
image: postgres:13
Expand Down
75 changes: 54 additions & 21 deletions backend/documents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions frontend/src/routes/routesDefinitions.ts
Original file line number Diff line number Diff line change
@@ -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/`,
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/services/uploadService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const uploadPDF = async (file: File): Promise<Response> => {
}

const response = await axios
.post(apiRoutes.upload, {
.post(apiRoutes.createFlashcards, {
method: "POST",
body: formData,
// Add headers if necessary (e.g., for authentication)
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/types/Documents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type Flashcard = {
front: string;
back: string;
};

export type { Flashcard };

0 comments on commit f9c1534

Please sign in to comment.