from openai import OpenAI from django.core.management.base import BaseCommand from inventory.models import CarSerie, CarModel, CarMake, CarTrim, CarSpecification, CarSpecificationValue from django.conf import settings class Command(BaseCommand): help = 'Translates to Arabic and saves them in arabic_name field.' def handle(self, *args, **kwargs): client = OpenAI(api_key=settings.OPENAI_API_KEY) car_model = CarModel.objects.all()[0:1000] total = car_model.count() print(f'Translating {total} names...') for index, car_model in enumerate(car_model, start=1): try: completion = client.chat.completions.create( model="gpt-4o", messages=[ { "role": "system", "content": "You are an assistant that finds the Arabic Names for car models." "Do not translate just write the arabic names." "If the name is a number just write it as is." "If you can't find the arabic name just write -" }, { "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}] Translated '{car_model.name}' to '{translation}'") except Exception as e: print(f"Error translating '{car_model.name}': {e}")