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, 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_make = CarMake.objects.all()
|
|
total = car_make.count()
|
|
print(f'Translating {total} names...')
|
|
for index, car_make in enumerate(car_make, start=1):
|
|
try:
|
|
completion = client.chat.completions.create(
|
|
model="gpt-4",
|
|
messages=[
|
|
{
|
|
"role": "system",
|
|
"content": "You are a translation assistant that translates English to Arabic."
|
|
},
|
|
{
|
|
"role": "user",
|
|
"content": car_make.name
|
|
}
|
|
],
|
|
temperature=0.3,
|
|
)
|
|
translation = completion.choices[0].message.content.strip()
|
|
car_make.arabic_name = translation
|
|
car_make.save()
|
|
print(f"[{index}/{total}] Translated '{car_make.name}' to '{translation}'")
|
|
except Exception as e:
|
|
print(f"Error translating '{car_make.name}': {e}")
|