Skip to content

Commit 2a4d498

Browse files
added a new linear regression model
1 parent 4153168 commit 2a4d498

File tree

5 files changed

+240
-5
lines changed

5 files changed

+240
-5
lines changed

Algolyzer/playground/templates/playground/home.html

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<h1 class="text-center my-10 font-bold font-poppins text-5xl">Playground</h1>
66
<h1 class="font-bold text-xl mb-5">Pre-Trained AIML Models</h1>
77

8-
{% comment %} Sentiment Analysis {% endcomment %}
98
<div class="flex flex-col gap-5">
109

10+
{% comment %} Sentiment Analysis {% endcomment %}
1111
<div class="rounded-xl border-2 border-solid border-primary p-5 hover:border-secondary">
1212
<div class="flex md:items-center justify-between md:flex-row flex-col">
1313
<h1 class="text-4xl my-2 p-4 text-primary">Sentiment Analysis</h1>
@@ -59,6 +59,57 @@ <h1 class="text-4xl my-2 p-4 text-primary">Sentiment Analysis</h1>
5959
</div>
6060
</div>
6161

62+
63+
{% comment %} Linear Regression {% endcomment %}
64+
<div class="rounded-xl border-2 border-solid border-primary p-5 hover:border-secondary">
65+
<div class="flex md:items-center justify-between md:flex-row flex-col">
66+
<h1 class="text-4xl my-2 p-4 text-primary">Linear Regression</h1>
67+
<div class="flex gap-4">
68+
<a href="{% url "linear_regression" %}" class="btn btn-accent font-bold font-poppins max-md:flex-1">Try Now</a>
69+
<a href="#" class="btn btn-primary btn-outline max-md:flex-1">Learn More</a>
70+
</div>
71+
</div>
72+
<div class="flex flex-col md:flex-row gap-4">
73+
{% comment %} Left Segment {% endcomment %}
74+
<div>
75+
<div class="stats shadow max-w-lg">
76+
<div class="stat">
77+
<div class="stat-title">Accuracy</div>
78+
<div class="stat-value text-primary">75%</div>
79+
<div class="stat-desc">↗︎ 400 (22%)</div>
80+
</div>
81+
82+
<div class="stat">
83+
<div class="stat-title">Ratings</div>
84+
<div class="stat-value">
85+
<form class="rating rating-md rating-half">
86+
<input type="radio" name="rating-10" class="mask mask-star-2 mask-half-1 bg-yellow-400" disabled/>
87+
<input type="radio" name="rating-10" class="mask mask-star-2 mask-half-2 bg-yellow-400" disabled/>
88+
<input
89+
type="radio"
90+
name="rating-10"
91+
class="mask mask-star-2 mask-half-1 bg-yellow-400" disabled/>
92+
<input type="radio" name="rating-10" class="mask mask-star-2 mask-half-2 bg-yellow-400" disabled/>
93+
<input type="radio" name="rating-10" class="mask mask-star-2 mask-half-1 bg-yellow-400" checked="checked" disabled/>
94+
<input type="radio" name="rating-10" class="mask mask-star-2 mask-half-2 bg-yellow-400" disabled/>
95+
<input type="radio" name="rating-10" class="mask mask-star-2 mask-half-1 bg-yellow-400" disabled/>
96+
<input type="radio" name="rating-10" class="mask mask-star-2 mask-half-2 bg-yellow-400" disabled/>
97+
<input type="radio" name="rating-10" class="mask mask-star-2 mask-half-1 bg-yellow-400" disabled/>
98+
<input type="radio" name="rating-10" class="mask mask-star-2 mask-half-2 bg-yellow-400" disabled/>
99+
</form>
100+
</div>
101+
<div class="stat-desc">↘︎ 90 (14%)</div>
102+
</div>
103+
</div>
104+
</div>
105+
106+
{% comment %} Right Segment {% endcomment %}
107+
<div>
108+
<p>{% lorem 1 b random %}
109+
</p>
110+
</div>
111+
</div>
112+
</div>
62113
{% comment %} Doodle Classifier {% endcomment %}
63114
<div class="rounded-xl border-2 border-solid border-primary p-5 hover:border-secondary">
64115
<div class="flex md:items-center justify-between md:flex-row flex-col">
@@ -101,7 +152,7 @@ <h1 class="text-4xl my-2 p-4 text-primary">Doodle Classifier</h1>
101152
</div>
102153
</div>
103154
</div>
104-
155+
105156
{% comment %} Right Segment {% endcomment %}
106157
<div>
107158
<p>{% lorem 1 b random %}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
{% extends "base.html" %}
2+
3+
{% block extra_head %}
4+
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
5+
{% endblock %}
6+
7+
{% block content %}
8+
<div class="max-w-5xl mx-auto my-5 p-2 bg-base-200 shadow-lg ">
9+
<div class="p-2 lg:p-5 rounded-xl border-2 border-solid border-primary">
10+
<h1 class="text-4xl font-bold text-center my-5">Linear Regression</h1>
11+
12+
13+
<form method="POST" class="space-y-4">
14+
{% csrf_token %}
15+
<label class="block text-lg font-semibold px-4">Enter values:</label>
16+
17+
{% for i in "12345" %}
18+
<div class="px-4">
19+
<label class="label">
20+
<span class="label-text">Value {{ forloop.counter }}</span>
21+
<span class="label-text-alt" id="slider-value-{{ forloop.counter }}">5</span>
22+
</label>
23+
<input
24+
type="range"
25+
name="value_{{ forloop.counter }}"
26+
min="0"
27+
max="10"
28+
value="5"
29+
class="range range-primary"
30+
oninput="document.getElementById('slider-value-{{ forloop.counter }}').textContent = this.value"
31+
/>
32+
</div>
33+
{% endfor %}
34+
35+
<button class="btn btn-primary w-full">Submit</button>
36+
</form>
37+
38+
{% if predicted_value %}
39+
<div class="px-4 py-4">
40+
<div class="alert alert-info">
41+
<p><strong>Input Values:</strong> {{ input_values|join:", " }}</p>
42+
<p><strong>Prediction:</strong> The next value is {{ predicted_value }}.</p>
43+
</div>
44+
</div>
45+
{% endif %}
46+
47+
{% if show_chart %}
48+
<div class="px-4 py-4">
49+
<div class="card bg-base-100 shadow-xl">
50+
<div class="card-body">
51+
<h2 class="card-title">Linear Regression Chart</h2>
52+
<canvas id="regressionChart"></canvas>
53+
</div>
54+
</div>
55+
</div>
56+
57+
{% block scripts %}
58+
<script>
59+
// Prepare data for Chart.js
60+
const inputValues = {{ input_values|safe }};
61+
const predictedValue = {{ predicted_value|safe }};
62+
const slope = {{ slope|safe }};
63+
const intercept = {{ intercept|safe }};
64+
65+
// Data points (indices 1 to 5 for inputs, 6 for prediction)
66+
const dataPoints = inputValues.map((val, idx) => ({
67+
x: idx + 1,
68+
y: val
69+
})).concat([{ x: 6, y: predictedValue }]);
70+
71+
// Regression line points (extend slightly for visibility)
72+
const linePoints = [
73+
{ x: 0.5, y: intercept + 0.5 * slope },
74+
{ x: 6.5, y: intercept + 6.5 * slope }
75+
];
76+
77+
// Initialize Chart.js
78+
const ctx = document.getElementById('regressionChart').getContext('2d');
79+
new Chart(ctx, {
80+
type: 'scatter',
81+
data: {
82+
datasets: [
83+
{
84+
label: 'Data Points',
85+
data: dataPoints,
86+
backgroundColor: 'rgb(59, 130, 246)', // DaisyUI primary color
87+
borderColor: 'rgb(59, 130, 246)',
88+
showLine: false,
89+
pointRadius: 6
90+
},
91+
{
92+
label: 'Regression Line',
93+
data: linePoints,
94+
type: 'line',
95+
borderColor: 'rgb(239, 68, 68)', // Contrast color (red)
96+
backgroundColor: 'transparent',
97+
fill: false,
98+
pointRadius: 0,
99+
borderWidth: 2
100+
}
101+
]
102+
},
103+
options: {
104+
responsive: true,
105+
scales: {
106+
x: {
107+
title: { display: true, text: 'Index' },
108+
min: 0,
109+
max: 7
110+
},
111+
y: {
112+
title: { display: true, text: 'Value' },
113+
min: 0,
114+
max: 12
115+
}
116+
},
117+
plugins: {
118+
legend: { display: true }
119+
}
120+
}
121+
});
122+
</script>
123+
{% endblock %}
124+
{% endif %}
125+
126+
</div>
127+
</div>
128+
{% endblock %}

Algolyzer/playground/urls.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
from django.urls import path
22

3-
from .views import doodle_classifier, playground_home, sentiment_analysis
3+
from .views import (
4+
doodle_classifier,
5+
linear_regression,
6+
playground_home,
7+
sentiment_analysis,
8+
)
49

510
urlpatterns = [
611
path("", playground_home, name="playground_home"),
12+
path("linear_regression", linear_regression, name="linear_regression"),
713
path("sentiment_analysis/", sentiment_analysis, name="sentiment_analysis"),
814
path("doodle_classifier/", doodle_classifier, name="doodle_classifier"),
915
]

Algolyzer/playground/views.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
import json
33
import os
44

5+
import numpy as np
56
from django.conf import settings
67
from django.contrib.auth.decorators import login_required
78
from django.core.files.base import ContentFile
89
from django.shortcuts import redirect, render
910
from django.utils.timezone import now
1011
from home.decorators import profile_required
1112
from PIL import Image
13+
from sklearn.linear_model import LinearRegression
1214
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
1315

1416
from .models import PlaygroundTask
@@ -159,3 +161,48 @@ def doodle_classifier(request):
159161

160162
context = {"previous_results": previous_results}
161163
return render(request, "playground/doodle_classifier.html", context=context)
164+
165+
166+
@login_required
167+
def linear_regression(request):
168+
if request.method == "POST":
169+
# Collect form data (values from 5 sliders)
170+
values = [
171+
float(request.POST.get("value_1", 0)),
172+
float(request.POST.get("value_2", 0)),
173+
float(request.POST.get("value_3", 0)),
174+
float(request.POST.get("value_4", 0)),
175+
float(request.POST.get("value_5", 0)),
176+
]
177+
178+
# Prepare data for linear regression
179+
X = np.array(range(1, 6)).reshape(-1, 1) # Indices 1 to 5
180+
y = np.array(values) # Slider values
181+
182+
# Train linear regression model
183+
model = LinearRegression()
184+
model.fit(X, y)
185+
186+
# Predict the next value (for index 6)
187+
next_index = np.array([[6]])
188+
predicted_value = model.predict(next_index)[0]
189+
190+
# Get regression line parameters for plotting
191+
slope = model.coef_[0]
192+
intercept = model.intercept_
193+
194+
# Pass data to template
195+
return render(
196+
request,
197+
"playground/linear_regression.html",
198+
{
199+
"predicted_value": round(predicted_value, 2),
200+
"input_values": values,
201+
"slope": slope,
202+
"intercept": intercept,
203+
"show_chart": True, # Flag to display chart
204+
},
205+
)
206+
207+
else:
208+
return render(request, "playground/linear_regression.html")

requirements.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ identify==2.6.4
3333
idna==3.10
3434
isort==5.13.2
3535
Jinja2==3.1.5
36+
joblib==1.4.2
3637
keras==3.8.0
3738
kombu==5.4.2
3839
libclang==18.1.1
@@ -58,7 +59,6 @@ nvidia-curand-cu12==10.3.5.147
5859
nvidia-cusolver-cu12==11.6.1.9
5960
nvidia-cusparse-cu12==12.3.1.170
6061
nvidia-cusparselt-cu12==0.6.2
61-
nvidia-nccl-cu12==2.21.5
6262
nvidia-nvjitlink-cu12==12.4.127
6363
nvidia-nvtx-cu12==12.4.127
6464
opt_einsum==3.4.0
@@ -83,22 +83,25 @@ regex==2024.11.6
8383
requests==2.32.3
8484
rich==13.9.4
8585
safetensors==0.5.2
86+
scikit-learn==1.6.1
87+
scipy==1.15.2
8688
setuptools==75.8.0
8789
six==1.17.0
8890
sqlparse==0.5.3
8991
sympy==1.13.1
9092
tensorboard==2.18.0
9193
tensorboard-data-server==0.7.2
9294
tensorflow==2.18.0
95+
tensorflow_intel==2.18.0
9396
termcolor==2.5.0
9497
tf_keras==2.18.0
98+
threadpoolctl==3.6.0
9599
tokenizers==0.21.0
96100
torch==2.6.0
97101
torchaudio==2.6.0
98102
torchvision==0.21.0
99103
tqdm==4.67.1
100104
transformers==4.48.3
101-
triton==3.2.0
102105
typing_extensions==4.12.2
103106
tzdata==2025.1
104107
urllib3==2.3.0

0 commit comments

Comments
 (0)