update
This commit is contained in:
parent
7423fa9878
commit
f72f3ff9d1
Binary file not shown.
@ -1,43 +1,48 @@
|
|||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
from django.core.management.base import BaseCommand
|
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
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
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):
|
def handle(self, *args, **kwargs):
|
||||||
client = OpenAI(api_key=settings.OPENAI_API_KEY)
|
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...')
|
print(f'Translating {total} names...')
|
||||||
for index, en_value in enumerate(en_value, start=1):
|
|
||||||
if not en_value.arabic_name:
|
for index, car_model in enumerate(car_models, start=1):
|
||||||
try:
|
if not car_model.arabic_name or car_model.arabic_name == '-':
|
||||||
completion = client.chat.completions.create(
|
if isinstance(car_model.name, int):
|
||||||
model="gpt-4o",
|
car_model.arabic_name = car_model.name
|
||||||
messages=[
|
car_model.save()
|
||||||
{
|
print(f"[{index}/{total}] .. Skipped GPT (Numeric)")
|
||||||
"role": "system",
|
else:
|
||||||
"content": (
|
try:
|
||||||
"You are an assistant that translates English to Arabic."
|
completion = client.chat.completions.create(
|
||||||
"You are an assistant specialized in cars and automotive terms."
|
model="gpt-4o",
|
||||||
"If the model name is a number just write it as is"
|
messages=[
|
||||||
"You can get the arabic names for makes, models, series, trims, options, and specifications."
|
{
|
||||||
)
|
"role": "system",
|
||||||
},
|
"content": (
|
||||||
{
|
"You are an assistant that translates English car names to Arabic."
|
||||||
"role": "user",
|
"If the name is purely numeric, keep it as is."
|
||||||
"content": en_value.name
|
"For mixed names like 'D9', translate them as 'دي 9'."
|
||||||
}
|
)
|
||||||
],
|
},
|
||||||
temperature=0.2,
|
{
|
||||||
)
|
"role": "user",
|
||||||
translation = completion.choices[0].message.content.strip()
|
"content": car_model.name
|
||||||
en_value.arabic_name = translation
|
}
|
||||||
en_value.save()
|
],
|
||||||
print(f"[{index}/{total}] .. Done")
|
temperature=0.2,
|
||||||
except Exception as e:
|
)
|
||||||
print(f"Error translating '{en_value.name}': {e}")
|
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}")
|
||||||
Loading…
x
Reference in New Issue
Block a user