37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
from openai import OpenAI
|
|
from django.core.management.base import BaseCommand
|
|
from inventory.models import CarSerie, CarModel, CarMake, CarTrim, CarOption, 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_option = CarOption.objects.all()[10300:]
|
|
total = car_option.count()
|
|
print(f'Translating {total} names...')
|
|
for index, car_option in enumerate(car_option, start=1):
|
|
try:
|
|
completion = client.chat.completions.create(
|
|
model="gpt-4o",
|
|
messages=[
|
|
{
|
|
"role": "system",
|
|
"content": "You are an assistant that translates English to Arabic."
|
|
},
|
|
{
|
|
"role": "user",
|
|
"content": car_option.name
|
|
}
|
|
],
|
|
temperature=0.2,
|
|
)
|
|
translation = completion.choices[0].message.content.strip()
|
|
car_option.arabic_name = translation
|
|
car_option.save()
|
|
print(f"[{index}/{total}] Translated '{car_option.name}' to '{translation}'")
|
|
except Exception as e:
|
|
print(f"Error translating '{car_option.name}': {e}")
|