From f72f3ff9d18c7cd0dbb3a430d0b4aace79a52a73 Mon Sep 17 00:00:00 2001 From: Marwan Alwali Date: Tue, 18 Feb 2025 12:10:51 +0300 Subject: [PATCH] update --- .../__pycache__/translate.cpython-311.pyc | Bin 2779 -> 2931 bytes inventory/management/commands/translate.py | 69 ++++++++++-------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/inventory/management/commands/__pycache__/translate.cpython-311.pyc b/inventory/management/commands/__pycache__/translate.cpython-311.pyc index d578167ba8a61aae3fe531e72d070479577e9afc..836ceffb4e77242132256e1401b3875589acfa5e 100644 GIT binary patch delta 1740 zcmZ`(U2M}<6uwSkJ3mcGNWx5M%B3VuU6Pbv5lC09D=iI$04p!RSm(v2IL+T|cNCPa z>|s*1@zBl>s|ILAVvK1_+XD|gqD|~Mt}He4RB4mgyAdEEo_6h!s-4*J@#pWJd+s^s z`X2kw@Utzx@4a3(K+<>g!Nd*cf{()9TZqToFu(!MQ6x&_IdY*K!JG@%V;)R*4zl8j z3da{0UL4zc;*7|ar6Mcy63nwAESVFq%$Af9Y`a~)#48su#+J4#3)qv{dF(>`eH#EX z#0*&@3obLKDPr6D5l2iS9{}bt(IplqF#zr&?;oudrcFG!-mgZEuVty( z$tACH+P)`4)`%N`bJlQ#u3PwB3^wJiX^1=rV%UO}klz5#AI{`(rvqT`=&f9jscu5WL$ z%h+Y$pSGU`moa2yI4}m7u;O*>OY2zj6?Xm=)_b-60!*dg*rZUc@*F&R>J048O_g|2 z$TL@oiK?93U(pHaiX`);sXIU!bM(%deS;RG7n~30M-$UhB;`2T`YV>PhHSpGVP7NNc2hwUF ztp(C^#A-OAg<~sWW;x8L;aysI*PQz=fBT9bF8iVC@6!BT%4cN9>_3pX*Yj|o4*ONu zufcu;Y84 zmKqK!jH1ML-8rwt`c`6t%dx>mSC2t}?|*9GcGG4; z&`Wn39e_y~I{bN;aq+-}=7ssZ_T{b=RrxRVq^NJiHV`fb`z$C!#8q(Wp$_(wD2-q6WZF`RmbI z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2 zF$8tqV58LnI=iiQ5PTDRoCCGNLr)QGoVL0^dyf%3cK;nCIAjDzjo{QyFtQzt6oXMC z7~MEiIy$)Vwh`zq1&(Y+u150FYtfD9ZlIyiIQW?W0qWLs3*BO1$OsG-Y<;$Sn6&@~ zxxa`oHCJeyxKDrWfd=Y>wz4e-CXK*k;a~a;iseLQkTYPv5$9j;OGSU;hbK=l9@RFE_Y;r$3Fyy+36qY;(b>o2rYjzY6P(e1Wg5;8!nC{K2}GbAM}WQ_Hc-$} zdmvmn_?6s~SH>;8jX=-xa#T)K@@K+cJ?@&ERY(*;)f5^5`g|y8UQ9Qi$O~C$1pKLjV8( 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