diff --git a/inventory/management/commands/__pycache__/translate.cpython-311.pyc b/inventory/management/commands/__pycache__/translate.cpython-311.pyc index d578167b..836ceffb 100644 Binary files a/inventory/management/commands/__pycache__/translate.cpython-311.pyc and b/inventory/management/commands/__pycache__/translate.cpython-311.pyc differ diff --git a/inventory/management/commands/translate.py b/inventory/management/commands/translate.py index 0ac7bd94..fb1a0a22 100644 --- a/inventory/management/commands/translate.py +++ b/inventory/management/commands/translate.py @@ -1,43 +1,48 @@ from openai import OpenAI from django.core.management.base import BaseCommand -from inventory.models import CarSerie, CarModel, CarMake, CarTrim, CarOption, CarSpecification +from inventory.models import CarModel from django.conf import settings class Command(BaseCommand): - help = 'Translates to Arabic and saves them in arabic_name field.' + help = 'Translates car model names to Arabic and saves them in the arabic_name field.' def handle(self, *args, **kwargs): client = OpenAI(api_key=settings.OPENAI_API_KEY) - en_value = CarModel.objects.all() + car_models = CarModel.objects.all() - total = en_value.count() + total = car_models.count() print(f'Translating {total} names...') - for index, en_value in enumerate(en_value, start=1): - if not en_value.arabic_name: - try: - completion = client.chat.completions.create( - model="gpt-4o", - messages=[ - { - "role": "system", - "content": ( - "You are an assistant that translates English to Arabic." - "You are an assistant specialized in cars and automotive terms." - "If the model name is a number just write it as is" - "You can get the arabic names for makes, models, series, trims, options, and specifications." - ) - }, - { - "role": "user", - "content": en_value.name - } - ], - temperature=0.2, - ) - translation = completion.choices[0].message.content.strip() - en_value.arabic_name = translation - en_value.save() - print(f"[{index}/{total}] .. Done") - except Exception as e: - print(f"Error translating '{en_value.name}': {e}") + + for index, car_model in enumerate(car_models, start=1): + if not car_model.arabic_name or car_model.arabic_name == '-': + if isinstance(car_model.name, int): + car_model.arabic_name = car_model.name + car_model.save() + print(f"[{index}/{total}] .. Skipped GPT (Numeric)") + else: + try: + completion = client.chat.completions.create( + model="gpt-4o", + messages=[ + { + "role": "system", + "content": ( + "You are an assistant that translates English car names to Arabic." + "If the name is purely numeric, keep it as is." + "For mixed names like 'D9', translate them as 'دي 9'." + ) + }, + { + "role": "user", + "content": car_model.name + } + ], + temperature=0.2, + ) + translation = completion.choices[0].message.content.strip() + car_model.arabic_name = translation + car_model.save() + print(f"[{index}/{total}] .. Done") + except Exception as e: + print(f"Error translating '{car_model.name}': {e}") \ No newline at end of file