Cognition Creators documentation

Home page

views.py

This module contains the view functions for rendering various pages and handling language settings for the web application.

Functions:

  • index(request):

    Renders the home page of the application.

  • cognition(request):

    Renders the cognition page of the application.

  • team(request):

    Renders the team page of the application.

  • privacy_policy(request):

    Renders the privacy policy page of the application.

  • documentation(request):

    Renders the documentation page of the application.

  • presentation_of_the_project(request):

    Renders the project presentation page of the application.

  • set_language(request):

    Sets the preferred language for the user session and updates the session to remember the language.

home.views.cognition(request)

Render the cognition page of the application.

Parameters:

request – HttpRequest object.

Returns:

Rendered template for the cognition page.

home.views.documentation(request)

Render the documentation page of the application.

Parameters:

request – HttpRequest object.

Returns:

Rendered template for the documentation page.

home.views.index(request)

Render the index (home) page of the application.

Parameters:

request – HttpRequest object.

Returns:

Rendered template for the home page.

home.views.presentation_of_the_project(request)

Render the project presentation page of the application.

Parameters:

request – HttpRequest object.

Returns:

Rendered template for the project presentation page.

home.views.privacy_policy(request)

Render the privacy policy page of the application.

Parameters:

request – HttpRequest object.

Returns:

Rendered template for the privacy policy page.

home.views.set_language(request)

Set the preferred language for the user session.

This function allows users to change the display language of the site. It retrieves the language from the request, activates the language, and updates the session to remember the selected language.

Parameters:

request – HttpRequest object.

Returns:

Redirects the user back to the previous page or the home page.

home.views.team(request)

Render the team page of the application.

Parameters:

request – HttpRequest object.

Returns:

Rendered template for the team page.

Authorization and Profile Page

views.py

This module defines the core views for the faceid app, which include user registration, login, logout, and profile management. It also handles the display of user photos and victories in the profile page.

accounts.views.login(request)

Handles user login and renders the login page.

Parameters:

request – The HTTP request object.

Returns:

Rendered HTML page for user login.

accounts.views.logout(request)

Handles user logout and renders the logout page.

Parameters:

request – The HTTP request object.

Returns:

Rendered HTML page for user logout.

accounts.views.signup(request)

Handles user registration and renders the signup page.

Parameters:

request – The HTTP request object.

Returns:

Rendered HTML page for user signup.

accounts.views.user_profile(request, username)

Retrieves and displays the user’s profile page, including user photo and victories.

Parameters:
  • request – The HTTP request object.

  • username – The username of the user whose profile is being accessed.

Returns:

Rendered HTML page displaying the user’s profile information.

Feature: Face ID

views.py

This module defines views for user registration, login, and image resizing functionality using face recognition. It also handles the logic for user session management, including automatic login after registration and facial recognition-based login.

faceid.views.index(request)

Renders the signup page.

Parameters:

request – The HTTP request object.

Return HttpResponse:

Rendered signup page.

faceid.views.login(request)

Handles user login through face recognition.

Parameters:

request – The HTTP request object.

Return JsonResponse:

Success or failure status of the login process.

faceid.views.register(request)

Handles user registration, image upload, and auto-login.

Parameters:

request – The HTTP request object.

Return JsonResponse:

Success or failure status of the registration process.

faceid.views.resize_image(image_content, size=(400, 300))

Resizes the uploaded image to the specified dimensions.

Parameters:
  • image_content – (bytes): The image content in bytes.

  • size – (tuple): Desired dimensions (width, height).

Return bytes:

Resized image content in JPEG format.

faceid.views.success(request)

Redirects the user to the game after successful login.

Parameters:

request – The HTTP request object.

Return HttpResponse:

Redirect to the game page.

faceid.views.user_logout(request)

Logs out the user and deletes their session.

Parameters:

request – The HTTP request object.

Return HttpResponse:

Redirects to the home page.

Feature: Game

views.py

This module contains the view functions for the image classification game based on the CIFAR-10 model. It includes functionalities such as playing the game, processing user inputs, generating predictions from the model, and handling game sessions.

Functions:

  • play_game(request):

    Main function for the game. It handles random image selection, model prediction processing, compares the results with the user’s input, and updates the score. The function also determines the winner once either the user or the model reaches 7 points.

  • reset_game(request):

    Resets the game by clearing the session data and redirects the user to the start of the game.

Dependencies:

  • tensorflow.keras.models: Used to load the pre-trained CIFAR-10 model for image classification.

  • PIL.Image: For loading and processing the images.

  • numpy: Used for handling image arrays and model predictions.

Variables:

  • CLASSES: A list of class names corresponding to the CIFAR-10 dataset categories.

  • model: The pre-trained CIFAR-10 model loaded from a saved Keras model file.

Session Data:

  • user_score: Tracks the user’s score during the game.

  • model_score: Tracks the model’s score during the game.

  • current_image_id: Stores the ID of the current image being classified.

game2.views.play_game(request)

Main function for the game. It handles random image selection, model prediction processing, compares the results with the user’s input, and updates the score.

game2.views.reset_game(request)

Function to reset the game. Clears all session data and redirects to the start of the game.

Feature: Chat

views.py

This module handles the views for the GPT-4 interaction page, including rendering the chat interface and processing requests to the OpenAI API to generate responses.

Functions:

  • chat_page(request):

    Renders the chat page where users can interact with the GPT-4 model. This view is protected by the login_required decorator to ensure that only authenticated users can access it.

  • chat_view(request):

    Handles POST requests from the chat interface, sends user input to the GPT-4 model via the OpenAI API, and returns the AI-generated response as a JSON object.

Dependencies:

  • requests: Used to interact with external APIs (OpenAI GPT-4 API).

  • environ: For handling environment variables, particularly loading the API key from a .env file.

  • os: To work with file paths and environment settings.

  • csrf_exempt: To allow the chat interaction view to bypass CSRF validation.

  • login_required: Ensures that only logged-in users can access the chat page.

Variables:

  • API_KEY: The API key required to interact with the OpenAI GPT-4 model. Loaded from the .env file.

gpt_response.views.chat_page(request)

Renders the chat page where users can interact with the GPT-4 model.

Parameters:

request – (HttpRequest): The HTTP request object.

Return HttpResponse:

The rendered HTML page for the chat interface.

This function uses the ‘gpt_response/chat.html’ template and injects the page title, page id, and app name. It is protected by the login_required decorator, so only authenticated users can access the page.

gpt_response.views.chat_view(request)

Processes POST requests for interacting with the OpenAI GPT API to generate responses.

Parameters:

request – (HttpRequest): The HTTP request object. The function expects a POST request containing user input via the ‘input’ field.

Return JsonResponse:

A JSON response containing the AI-generated text or an error message.

This view handles requests to interact with the GPT model using the OpenAI API. It receives user input, sends it to the OpenAI API, and returns the generated response as a JSON object.

Feature: Recognition

views.py

This module contains views and helper functions for image classification and recognition using models like VGG16, Faster R-CNN, and Mask R-CNN.

recognition.views.index(request)

Handles image upload and processing based on the selected recognition type. Saves the uploaded image and its processing results in the database.

Parameters:

request – (HttpRequest): The HTTP request object.

Return HttpResponse:

Redirects to the result page after processing the image if the request is POST. Otherwise, renders the upload form.

Raises:

ValueError – If the recognition type is invalid or any other processing issue occurs.

recognition.views.load_model_from_google_drive(file_id, model_name, download_if_exists=False)

Downloads a model file from Google Drive and loads it into memory using PyTorch.

Parameters:
  • file_id – (str): The unique identifier for the file on Google Drive.

  • model_name – (str): The name of the model file to save on the local disk.

  • download_if_exists – (bool): If True, the model will be re-downloaded even if it already exists locally. If False, the model will be loaded from the local disk if it exists.

Return torch.nn.Module:

The PyTorch model loaded from the specified file.

Raises:

RuntimeError – If the model file cannot be loaded.

recognition.views.recognize_with_faster_rcnn(img, confidence_threshold)

Recognizes objects in an image using Faster R-CNN and then classifies each detected object using a VGG16 model.

Parameters:
  • img – (PIL.Image.Image): The input image to be processed.

  • confidence_threshold – (float): The minimum confidence score for a detection to be considered.

Return tuple:

A tuple containing: - str: The recognition results for all detected objects. - ContentFile: An image file with bounding boxes and recognition results drawn on it.

Raises:

ValueError – If an error occurs during image processing or model inference.

recognition.views.recognize_with_mask_rcnn(img, confidence_threshold)

Recognizes objects in an image using Mask R-CNN, then classifies each detected object using a VGG16 model, and visualizes the segmentation masks with contours.

Parameters:
  • img – (PIL.Image.Image): The input image to be processed.

  • confidence_threshold – (float): The minimum confidence score for a detection to be considered.

Return tuple:

A tuple containing: - str: The recognition results for all detected objects. - ContentFile: An image file with bounding boxes, segmentation masks, and recognition results drawn on it.

Raises:

ValueError – If an error occurs during image processing or model inference.

recognition.views.recognize_with_vgg16(img)

Recognizes the class of an image using a pre-trained VGG16 model fine-tuned on the CIFAR-10 dataset.

Parameters:

img – (PIL.Image.Image): The input image to be recognized.

Return str:

The name of the predicted class and its probability as a percentage.

Raises:

ValueError – If the image is not in RGB mode or if any error occurs during processing.

recognition.views.result(request, image_id)

Retrieves an uploaded image from the database using its ID and renders a template to display the image.

Parameters:
  • request – (HttpRequest): The HTTP request object.

  • image_id – (int): The unique identifier of the uploaded image in the database.

Return HttpResponse:

The rendered HTML page to display the image.

Raises:

Http404 – If no image is found with the given ID.

Indices and tables