Skip to content

Commit

Permalink
Settings editor working
Browse files Browse the repository at this point in the history
  • Loading branch information
ViperX7 committed Apr 15, 2023
1 parent ddfd61b commit 2394bc1
Show file tree
Hide file tree
Showing 5 changed files with 496 additions and 49 deletions.
14 changes: 12 additions & 2 deletions turbo_server/ai_model_manager/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
from django.contrib import admin
from .models import AIModel, AIModelSettings
from .models import AIModel, AIModelFormat, AIModelSetting

# Register your models here.
data = [AIModelSettings,AIModel]
data = [AIModelSetting]
_ = [admin.site.register(mod) for mod in data]


class AIModelFormatAdmin(admin.ModelAdmin):
list_display = ('name', 'extension')

class AIModelAdmin(admin.ModelAdmin):
list_display = ('name', 'version', 'is_configured', 'is_broken')

admin.site.register(AIModelFormat, AIModelFormatAdmin)
admin.site.register(AIModel, AIModelAdmin)
94 changes: 81 additions & 13 deletions turbo_server/ai_model_manager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
from django.db import models


class AIModelFormats(models.Model):
class AIModelFormat(models.Model):
name = models.CharField(max_length=255)
extension = models.CharField(max_length=255)

def __str__(self):
return self.name

class AIModelSettings(models.Model):

class AIModelSetting(models.Model):
temperature = models.FloatField()
top_p = models.FloatField()
top_k = models.IntegerField()
max_length = models.IntegerField()
repetition_penalty = models.FloatField()
num_return_sequences = models.IntegerField()
n_predict = models.IntegerField()
repeat_last_n = models.IntegerField()
seed = models.IntegerField()
Expand All @@ -26,11 +27,12 @@ class AIModel(models.Model):
path = models.FilePathField(path="models")
name = models.CharField(max_length=255)
source = models.URLField(blank=True, null=True)
author = models.CharField(max_length=255, blank=True, null=True)
model_format = models.ForeignKey(
"AIModelFormats", on_delete=models.CASCADE, blank=True, null=True
"AIModelFormat", on_delete=models.CASCADE, blank=True, null=True
)
settings = models.ForeignKey(
"AIModelSettings", on_delete=models.CASCADE, blank=True, null=True
"AIModelSetting", on_delete=models.CASCADE, blank=True, null=True
)
version = models.CharField(max_length=255, blank=True, null=True)
is_configured = models.BooleanField(default=False)
Expand All @@ -40,18 +42,84 @@ class AIModel(models.Model):
def list_all():
return AIModel.objects.all()

@property
def model_size(self):
size = str(os.path.getsize(self.path) / (1024 * 1024 * 1024))

return size[: size.find(".") + 3] + " GB"

@staticmethod
def add_models_from_dir(dir_path):
model_formats = AIModelFormats.objects.values_list("extension", flat=True)
model_formats = AIModelFormat.objects.values_list("extension", flat=True)
model_formats = list(model_formats)
new_models = []
for root, dirs, files in os.walk(dir_path):
for file in files:
file_path = os.path.join(root, file)
file_ext = os.path.splitext(file)[1]
if file_ext in model_formats:
file_name = os.path.splitext(file)[0]
model_format = AIModelFormats.objects.get(extension=file_ext)
AIModel.objects.create(
path=file_path,
name=file_name,
model_format=model_format,
)
model_format = AIModelFormat.objects.get(extension=file_ext)
if not AIModel.objects.filter(path=file_path).exists():
obj = AIModel.objects.create(
path=file_path,
name=file_name,
model_format=model_format,
)
new_models.append(obj)
_ = [obj.save() for obj in new_models]
return [obj.path for obj in new_models]

def set_settings(
self,
temperature,
top_p,
top_k,
repetition_penalty,
n_predict,
repeat_last_n,
seed,
batch_size,
):
"""
Sets the settings of the AI model.
If a settings object already exists, it will update the existing object.
If not, it will create a new settings object.
"""

print(temperature)
print("||||||||||||||||||||||||||||||||||||||")
# Check if a settings object already exists for this AI model.
print(self.settings)

if self.settings:
print("kkk")
settings = self.settings
# Set the settings fields.
settings.temperature = temperature
settings.top_p = top_p
settings.top_k = top_k
settings.repetition_penalty = repetition_penalty
settings.n_predict = n_predict
settings.repeat_last_n = repeat_last_n
settings.seed = seed
settings.batch_size = batch_size
else:
# Create a new settings object.
settings = AIModelSetting.objects.create(
temperature=temperature,
top_p=top_p,
top_k=top_k,
repetition_penalty=repetition_penalty,
n_predict=n_predict,
repeat_last_n=repeat_last_n,
seed=seed,
batch_size=batch_size,
)

# Save the settings object.
settings.save()

# Associate the settings with this AI model.
self.settings = settings
self.save()
3 changes: 3 additions & 0 deletions turbo_server/alpaca_turbo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self):
self.DEBUG = "-d" in sys.argv

self.threads = 4

self.top_k = 200
self.top_p = 0.99
self.temp = 0.7
Expand All @@ -36,6 +37,8 @@ def __init__(self):
self.seed = 888777
self.n_predict = 1000
self.repeat_last_n = 512


self.use_bos = True
self.antiprompt = "### Human:"

Expand Down
4 changes: 2 additions & 2 deletions turbo_server/chatbot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_all_conversations():
@property
def lastidx(self):
"""returns the index of the last element"""
index = None
index = 0
last_message = (
Message.objects.filter(conversation=self).order_by("-index").first()
)
Expand Down Expand Up @@ -110,7 +110,7 @@ def add_message(
def append(self, message):
"""Append a new message to the conversation."""
message.conversation = self
message.index = self.lastidx +1
message.index = self.lastidx + 1
message.save()
if message.index == 0:
self.title = message.user_request
Expand Down
Loading

0 comments on commit 2394bc1

Please sign in to comment.