Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# (optional) if Google Signup needed
GOOGLE_CLIENT_ID=YOUR-CLIENT-ID
GOOGLE_SECRET_KEY=YOUR-SECRET-KEY

# Where the app is being run
ALLOWED_HOSTS=localhost,127.0.0.1,192.168.1.13,localhost,127.0.0.1,192.168.1.13,192.168.1.150
CSRF_TRUSTED_ORIGINS=http://localhost,http://127.0.0.1,http://192.168.1.13,http://localhost:8000,http://127.0.0.1:8000,http://192.168.1.13:8000,http://192.168.1.150:8000

# default admin credentials
DJANGO_SUPERUSER_USERNAME=admin
[email protected]
DJANGO_SUPERUSER_PASSWORD=admin

# specify your redis url
CELERY_BROKER_URL=redis://redis:6379/0
26 changes: 0 additions & 26 deletions .github/workflows/cd.yml

This file was deleted.

5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,7 @@ node_modules
*output.css

# productions static files
staticfiles
staticfiles

# algolyzer playground aiml models
aiml_models
3 changes: 3 additions & 0 deletions Algolyzer/Algolyzer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .celery_app import celery_app

__all__ = ("celery_app",)
17 changes: 17 additions & 0 deletions Algolyzer/Algolyzer/celery_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os

from celery import Celery

# Use the existing DJANGO_SETTINGS_MODULE if it's already set, otherwise set a default
os.environ.setdefault(
"DJANGO_SETTINGS_MODULE",
os.getenv("DJANGO_SETTINGS_MODULE", "Algolyzer.settings.development"),
)

celery_app = Celery("Algolyzer")

# Load task modules from all registered Django app configs.
celery_app.config_from_object("django.conf:settings", namespace="CELERY")

# Auto-discover tasks in installed apps
celery_app.autodiscover_tasks()
14 changes: 14 additions & 0 deletions Algolyzer/Algolyzer/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"quiz",
"study",
"community",
"playground",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -94,3 +95,16 @@
STATIC_URL = "/static/"
STATICFILES_DIRS = [BASE_DIR / "static"]
STATIC_ROOT = BASE_DIR / "staticfiles"

# Celery Configurations
# settings.py

# Celery Settings
CELERY_BROKER_URL = os.getenv(
"CELERY_BROKER_URL", "redis://localhost:6379/0"
) # URL for Redis
CELERY_ACCEPT_CONTENT = ["json"] # Accept JSON formatted tasks
CELERY_TASK_SERIALIZER = "json" # Use JSON serialization

# AIML downloaded models directory
MODEL_DIR = "/aiml_models"
1 change: 1 addition & 0 deletions Algolyzer/Algolyzer/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
path("quiz/", include("quiz.urls")),
path("study/", include("study.urls")),
path("community/", include("community.urls")),
path("playground/", include("playground.urls")),
# allauth urls
path("accounts/", include("allauth.urls")),
]
Empty file.
3 changes: 3 additions & 0 deletions Algolyzer/playground/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions Algolyzer/playground/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class PlaygroundConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "playground"
38 changes: 38 additions & 0 deletions Algolyzer/playground/management/commands/download_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os

from django.core.management.base import BaseCommand
from transformers import AutoModel, AutoTokenizer

# Define models to download
MODELS = {
"sentiment-analysis": "distilbert-base-uncased-finetuned-sst-2-english",
"text-classification": "facebook/bart-large-mnli",
}

# Define directory for saving models
MODEL_DIR = os.path.join("playground", "aiml_models")


class Command(BaseCommand):
help = "Download Hugging Face models for use in Django views"

def handle(self, *args, **kwargs):
os.makedirs(MODEL_DIR, exist_ok=True)
for task, model_name in MODELS.items():
self.stdout.write(
self.style.SUCCESS(f"Downloading {task} model: {model_name}...")
)

# Download model and tokenizer
model = AutoModel.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Save them in the specified directory
model.save_pretrained(os.path.join(MODEL_DIR, model_name.replace("/", "_")))
tokenizer.save_pretrained(
os.path.join(MODEL_DIR, model_name.replace("/", "_"))
)

self.stdout.write(
self.style.SUCCESS(f"✔ Model {model_name} downloaded successfully!\n")
)
56 changes: 56 additions & 0 deletions Algolyzer/playground/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Generated by Django 5.1.4 on 2025-02-10 16:23

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="PlaygroundTask",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("celery_task_id", models.CharField(max_length=255, unique=True)),
("model_name", models.CharField(max_length=100)),
("input_data", models.TextField()),
(
"status",
models.CharField(
choices=[
("PENDING", "Pending"),
("PROCESSING", "Processing"),
("COMPLETED", "Completed"),
("FAILED", "Failed"),
],
default="PENDING",
max_length=20,
),
),
("result", models.TextField(blank=True, null=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
("completed_at", models.DateTimeField(blank=True, null=True)),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
Empty file.
23 changes: 23 additions & 0 deletions Algolyzer/playground/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.contrib.auth.models import User
from django.db import models


class PlaygroundTask(models.Model):
STATUS_CHOICES = [
("PENDING", "Pending"),
("PROCESSING", "Processing"),
("COMPLETED", "Completed"),
("FAILED", "Failed"),
]

user = models.ForeignKey(User, on_delete=models.CASCADE)
celery_task_id = models.CharField(max_length=255, unique=True) # Celery's Task ID
model_name = models.CharField(max_length=100) # Selected ML model
input_data = models.TextField() # Store user input (can be JSON)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="PENDING")
result = models.TextField(blank=True, null=True) # ML output
created_at = models.DateTimeField(auto_now_add=True)
completed_at = models.DateTimeField(blank=True, null=True)

def __str__(self):
return f"Task {self.celery_task_id} - {self.status}"
57 changes: 57 additions & 0 deletions Algolyzer/playground/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os

from celery import shared_task
from django.conf import settings
from django.utils.timezone import now
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline

from .models import PlaygroundTask

# Load ML models at startup (avoid reloading for each request)
MODEL_REGISTRY = {
"model_1": "distilbert-base-uncased-finetuned-sst-2-english", # Replace with actual model paths
"model_2": "model_2",
}


@shared_task(bind=True)
def process_ml_task(self, task_db_id, model_name, input_data):
"""Celery task to process ML model input."""
try:
# Fetch the task from the database
task = PlaygroundTask.objects.get(id=task_db_id)
task.status = "PROCESSING"
task.save()

# Load the selected model
model_path = MODEL_REGISTRY.get(model_name)
MODEL_DIR = os.path.join(
settings.BASE_DIR, "playground", "aiml_models", model_path
)

# Load model from saved directory
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_DIR)
sentiment_pipeline = pipeline(
"sentiment-analysis", model=model, tokenizer=tokenizer
)

if not model:
raise ValueError(f"Model '{model_name}' not found.")

# Process the input with the model
result = sentiment_pipeline(input_data)[0]

# Update the task in the database
task.status = "COMPLETED"
task.result = str(result) # Convert to JSON-friendly format if needed
task.completed_at = now()
task.save()

except Exception as e:
task.status = "FAILED"
task.result = str(e)
task.save()
# raise e

return task.result # Celery stores the result
23 changes: 23 additions & 0 deletions Algolyzer/playground/templates/playground/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends "base.html" %}

{% block content %}
<div class="container mx-auto">
<h1 class="text-center">This is Playground App</h1>
{% if error %}
<p style="color: red;">{{ error }}</p>
{% endif %}
<form method="post" action="{% url 'submit_task_view' %}">
{% csrf_token %}
<label for="input_data">Enter Input:</label>
<input type="text" name="input_data" required>

<label for="model_name">Select Model:</label>
<select name="model_name">
<option value="model_1">Model 1</option>
<option value="model_2">Model 2</option>
</select>

<button type="submit">Submit Task</button>
</form>
</div>
{% endblock %}
20 changes: 20 additions & 0 deletions Algolyzer/playground/templates/playground/task_status.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends "base.html" %}

{% block content %}
<h1>Task Status</h1>

{% if error %}
<p style="color: red;">{{ error }}</p>
{% else %}
<p><strong>Task ID:</strong> {{ task_id }}</p>
<p><strong>Status:</strong> {{ status }}</p>

{% if result %}
<p><strong>Result:</strong> {{ result }}</p>
{% else %}
<p>Processing... Refresh the page to check status.</p>
{% endif %}
{% endif %}

<a href="{% url 'playground_home' %}">Submit another task</a>
{% endblock%}
3 changes: 3 additions & 0 deletions Algolyzer/playground/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions Algolyzer/playground/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path

from .views import check_task_status_view, playground_home, submit_task_view

urlpatterns = [
path("", playground_home, name="playground_home"),
path("submit_task/", submit_task_view, name="submit_task_view"),
path("task_status/<str:task_id>/", check_task_status_view, name="task_status_view"),
]
Loading