-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.txt
593 lines (460 loc) · 15.1 KB
/
prompt.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
./config/settings/__init__.py
==========
./config/settings/development.py
==========
from .base import *
STATIC_URL = "static/"
STATIC_ROOT = os.path.join(BASE_DIR, "../", "staticfiles")
./config/settings/base.py
==========
"""
Django settings for blacklight_api project.
Generated by 'django-admin startproject' using Django 4.1.2.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
import os
from pathlib import Path
from decouple import Csv, config
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-h88unk&dbm^bjio1b*301^=v0m4i3sz=xqs8)l5cal7%i#qv+w"
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config("DEBUG", default=False, cast=bool)
ALLOWED_HOSTS = config("ALLOWED_HOSTS", cast=Csv())
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"api",
"rest_framework",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = "config.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
WSGI_APPLICATION = "config.wsgi.application"
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": config("DB_NAME"),
"USER": config("DB_USERNAME"),
"PASSWORD": config("DB_PASSWORD"),
"HOST": config("DB_HOSTNAME"),
"PORT": config("DB_PORT", cast=int),
}
}
# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
CELERY_BROKER_URL = config("CELERY_BROKER_URL")
CELERY_RESULT_BACKEND = config("REDIS_BACKEND")
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
./config/settings/production.py
==========
from .base import *
./config/asgi.py
==========
"""
ASGI config for blacklight_api project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/
"""
import os
from decouple import config
from django.core.asgi import get_asgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", config('DJANGO_SETTINGS_MODULE'))
application = get_asgi_application()
./config/__init__.py
==========
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ('celery_app',)
./config/celery.py
==========
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
from decouple import config
os.environ.setdefault('DJANGO_SETTINGS_MODULE', config('DJANGO_SETTINGS_MODULE'))
app = Celery('config')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
./config/urls.py
==========
"""blacklight_api URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path('', include('api.urls')),
]
./config/wsgi.py
==========
"""
WSGI config for blacklight_api project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
application = get_wsgi_application()
./api/migrations/0002_blacklightneuralnetworkrun_delete_blacklightrun.py
==========
# Generated by Django 4.1.7 on 2023-03-28 18:47
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='BlacklightNeuralNetworkRun',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=200)),
('description', models.CharField(max_length=200)),
('status', models.CharField(default='pending', max_length=20)),
('model_path', models.CharField(blank=True, max_length=255, null=True)),
('result', models.TextField(blank=True, null=True)),
],
),
migrations.DeleteModel(
name='BlacklightRun',
),
]
./api/migrations/__init__.py
==========
./api/migrations/0001_initial.py
==========
# Generated by Django 4.1.2 on 2023-03-28 01:05
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="BlacklightRun",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=200)),
("description", models.CharField(max_length=200)),
("date", models.DateTimeField(verbose_name="date published")),
],
),
]
./api/tasks.py
==========
from celery import shared_task
from .models import BlacklightNeuralNetworkRun
import os
from django.conf import settings
from blacklight.autoML import FeedForward
@shared_task
def fit_neural_network(nn_id, X_train, y_train, X_test, y_test, options):
nn = BlacklightNeuralNetworkRun.objects.get(id=nn_id)
nn.status = "in progress"
nn.save()
# Initialize your custom FeedForward class
ff = FeedForward(
number_of_individuals=options["number_of_individuals"],
num_parents_mating=options["num_parents_mating"],
death_percentage=options["death_percentage"],
number_of_generations=options["number_of_generations"],
options=options["options"]
)
ff.fit(X_train, y_train, X_test, y_test)
# Save the model to a file
model_filename = f"model_{nn_id}.h5"
model_path = os.path.join(settings.MEDIA_ROOT, model_filename)
ff.model.save(model_path)
nn.status = "completed"
nn.result = "your_result_representation" # Serialize the result as needed
nn.save()
./api/models.py
==========
from django.db import models
# Create your models here.
class BlacklightNeuralNetworkRun(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200)
description = models.CharField(max_length=200)
status = models.CharField(max_length=20, default='pending')
model_path = models.CharField(max_length=255, blank=True, null=True)
result = models.TextField(blank=True, null=True)
def __str__(self):
return self.name
./api/serializers.py
==========
from rest_framework import serializers
from .models import BlacklightNeuralNetworkRun
class BlacklightRunSerializer(serializers.ModelSerializer):
class Meta:
model = BlacklightNeuralNetworkRun
fields = '__all__'
./api/__init__.py
==========
./api/apps.py
==========
from django.apps import AppConfig
class ApiConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "api"
./api/admin.py
==========
from django.contrib import admin
from .models import BlacklightNeuralNetworkRun
# Register your models here.
admin.site.register(BlacklightNeuralNetworkRun)
./api/tests.py
==========
from django.test import TestCase
# Create your tests here.
./api/urls.py
==========
from django.urls import include, path
from rest_framework import routers
from . import views
router = routers.DefaultRouter()
router.register(r'blacklightruns', views.BlacklightRunViewSet)
# Wire up our API using automatic URL routing.
urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
./api/views.py
==========
from django.shortcuts import render
# Create your views here.
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework import viewsets
from .serializers import BlacklightRunSerializer
from .models import BlacklightNeuralNetworkRun
from .tasks import fit_neural_network
from django.http import FileResponse
from django.shortcuts import get_object_or_404
import os
class BlacklightRunViewSet(viewsets.ModelViewSet):
queryset = BlacklightNeuralNetworkRun.objects.all()
serializer_class = BlacklightRunSerializer
def get_object(self):
queryset = self.get_queryset()
pk = self.kwargs['pk']
return get_object_or_404(queryset, pk=pk)
@action(detail=True, methods=['post'])
def fit(self, request, pk=None):
nn = self.get_object()
X_train = request.data.get('X_train')
y_train = request.data.get('y_train')
X_test = request.data.get('X_test')
y_test = request.data.get('y_test')
options = request.data.get('options')
fit_neural_network.delay(nn.id, X_train, y_train, X_test, y_test, options)
return Response({'status': 'fitting in progress'})
@action(detail=True, methods=['get'])
def download_model(self, request, pk=None):
nn = self.get_object()
if nn.status != "completed":
return Response({'status': 'Model is not ready'})
if not nn.model_path:
return Response({'status': 'Model file not found'})
response = FileResponse(open(nn.model_path, 'rb'), content_type='application/octet-stream')
response['Content-Disposition'] = f'attachment; filename="{os.path.basename(nn.model_path)}"'
return response
./manage.py
==========
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
from decouple import config
def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", config('DJANGO_SETTINGS_MODULE'))
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == "__main__":
main()
./Dockerfile
==============
FROM python:3.10.2-slim-bullseye
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /code
COPY ./requirements.txt .
RUN apt-get update -y && \
apt-get install -y netcat && \
apt-get install -y nodejs && \
apt-get install -y npm && \
pip install --upgrade pip && \
pip install -r requirements.txt
COPY ./entrypoint.sh .
RUN chmod +x /code/entrypoint.sh
COPY . .
ENTRYPOINT ["/code/entrypoint.sh"]
./docker-compose.yml
==============
version: "3.9"
services:
web:
build: .
command: gunicorn config.wsgi:application --bind 0.0.0.0:8000
volumes:
- .:/code
- static_volume:/code/staticfiles # for static files
env_file:
- ./.env
expose:
- 8000
depends_on:
- redis
- db
db:
image: postgres:13
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=${DB_USERNAME}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
nginx:
image: nginx:1.21
ports:
- 80:80
volumes:
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
- static_volume:/code/staticfiles
depends_on:
- web
redis:
image: redis:7
celery:
build: .
command: celery -A config worker -l info
volumes:
- .:/code
env_file:
- ./.env
depends_on:
- db
- redis
- web
volumes:
postgres_data:
static_volume:
./.env
==============
SECRET_KEY=django-insecure-h88unk&dbm^bjio1b*301^=v0m4i3sz=xqs8)l5cal7%i#qv+w
ALLOWED_HOSTS=.localhost, .herokuapp.com, .0.0.0.0
DEBUG=True
DJANGO_SETTINGS_MODULE=config.settings.development
# Database
DB_NAME=chatdb
DB_USERNAME=admin
DB_PASSWORD=adminpassword
DB_HOSTNAME=db
DB_PORT=5432
# Celery
CELERY_BROKER_URL=redis://redis:6379/0
# Redis
REDIS_BACKEND=redis://redis:6379/0
./.dockerignore
==============
.git
.gitignore
db.sqlite3