Conversion_Kitchen_Code/kitchen_counter/conversion/translation/newconversion.py

643 lines
28 KiB
Python
Executable File

import copy
import sys
import os
from .detection import language_detector, script_det
from conversion.translation.translation_function import translate_comparison, ui_option3_and_4, convert_to_pdf, translate_comparison2
from conversion.translation.translation_variables import get_doc_file, get_pdf_save_dir, is_in_translation_list, \
get_language_script_code, script_dict, language_script, code_2_language, language_2_code
from conversion.translation.final_transliteration_only2 import makeTransliteration_only, add_dual_dialogue
from MNF.settings import BasePath
from mnfapp.models import MNFScriptDatabase, ScriptTranslations
from centralisedFileSystem.models import Script
from django.core.files.base import ContentFile
from scriptAudit.models import ScriptAuditModel, States
from scriptAudit.mnf_script_audit import NeutralAudit
basePath = BasePath()
def ScriptTranslation(*args, **kwargs):
"""
Translates a file using a audited dataframe
:param args:
:param kwargs:
:return None:
"""
# args
# 0 - full_dialogue_option_choices
# 1 - sentence_dialogue_option_choices
# 2 - sluglines and other choices
# kwargs
# dataframe -
# non_dial_src_lang -
# non_dial_src_script -
# non_dial_dest_lang -
# non_dial_dest_script -
# dial_src_lang -
# dial_src_script -
# dial_dest_lang -
# dial_dest_script -
# script_id -
# translation_id
translation_instance = ScriptTranslations.objects.get(translation_id=kwargs['translation_id'])
original_script = MNFScriptDatabase.objects.get(script_id=kwargs['script_id'])
# Free 5 Scene
if translation_instance.free_output:
breakpoint_5scenes = len(kwargs['dataframe'])
scene = 0
for idx, line in enumerate(kwargs['dataframe']):
if line[3] == "slugline":
scene += 1
if scene > 5:
breakpoint_5scenes = idx
break
dataframe = copy.deepcopy(kwargs['dataframe'][:breakpoint_5scenes])
# creating a copy of original dataframe
translated_dataframe = copy.deepcopy(dataframe)
else:
dataframe = copy.deepcopy(kwargs['dataframe'])
# creating a copy of original dataframe
translated_dataframe = copy.deepcopy(dataframe)
for index, data in enumerate(translated_dataframe):
if data[3] == 'action':
if kwargs['non_dial_src_lang'] != kwargs['non_dial_dest_lang']:
translated_dataframe[index][2] = translate_comparison2(data[2], kwargs['non_dial_src_lang'],
kwargs['non_dial_dest_lang'])
current_lang = kwargs['non_dial_dest_lang']
current_script = language_script[code_2_language[kwargs['non_dial_dest_lang']]]
else:
current_lang = kwargs['non_dial_src_lang']
current_script = kwargs['non_dial_src_script']
if current_script != kwargs['non_dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": current_lang,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
elif data[3] == 'dialogue':
line_language = language_detector(data[2])
line_script = script_det(data[2])
# case 1
if line_language == kwargs['dial_dest_lang'] and line_script == kwargs['dial_dest_script']:
if args[0][0]:
translated_dataframe[index][2] = translate_comparison2(data[2], line_language,
kwargs['dial_dest_lang'], kwargs,
args[1], True)
current_script = script_det(data[2])
if args[0][1] and current_script != kwargs['dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
# case 2
elif line_language == kwargs['dial_dest_lang'] and line_script == kwargs['non_dial_dest_script']:
if args[0][2]:
translated_dataframe[index][2] = translate_comparison2(data[2], line_language,
kwargs['dial_dest_lang'], kwargs,
args[1], True)
current_script = script_det(data[2])
if args[0][3] and current_script != kwargs['dial_dest_script']:
kwargs2 = {
"dest_script": line_script,
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
elif not args[0][3] and current_script != kwargs['non_dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
# case 3
elif line_language == kwargs['non_dial_src_lang'] and line_script == kwargs['dial_dest_script']:
if args[0][4]:
translated_dataframe[index][2] = translate_comparison2(data[2], line_language,
kwargs['dial_dest_lang'], kwargs,
args[1], True)
current_lang = language_detector(data[2])
current_script = script_det(data[2])
if current_script != kwargs['dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": current_lang,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
# case 4
elif line_language == kwargs['non_dial_src_lang'] and line_script == kwargs['non_dial_dest_script']:
if args[0][6]:
translated_dataframe[index][2] = translate_comparison2(data[2], line_language,
kwargs['dial_dest_lang'], kwargs,
args[1], True)
current_lang = language_detector(data[2])
current_script = script_det(data[2])
if args[0][7] and current_script != kwargs['dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": current_lang,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
elif not args[0][7] and current_script != kwargs['non_dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": current_lang,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
# case 5
elif line_language == kwargs['non_dial_dest_lang'] and line_script == kwargs['dial_dest_script']:
if args[0][8]:
translated_dataframe[index][2] = translate_comparison2(data[2], line_language,
kwargs['dial_dest_lang'], kwargs,
args[1], True)
current_lang = language_detector(data[2])
current_script = script_det(data[2])
if args[0][9] and current_script != kwargs['dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": current_lang,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
elif not args[0][9] and current_script != kwargs['non_dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": current_lang,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
# case 6
elif line_language == kwargs['non_dial_dest_lang'] and line_script == kwargs['non_dial_dest_script']:
if args[0][10]:
translated_dataframe[index][2] = translate_comparison2(data[2], line_language,
kwargs['dial_dest_lang'], kwargs,
args[1], True)
current_lang = language_detector(data[2])
current_script = script_det(data[2])
if args[0][11] and current_script != kwargs['dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": current_lang,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
elif not args[0][11] and current_script != kwargs['non_dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": current_lang,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
# case 7
else:
if args[0][12]:
translated_dataframe[index][2] = translate_comparison2(data[2], line_language,
kwargs['dial_dest_lang'], kwargs,
args[1], True)
current_lang = kwargs['dial_dest_lang']
current_script = language_script[code_2_language[current_lang]]
else:
current_lang = line_language
current_script = line_script
if args[0][13] and current_script != kwargs['dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": current_lang,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
elif not args[0][13] and current_script != line_script:
kwargs2 = {
"dest_script": line_script,
"dual_dial_script": "No",
"line": str(data[2]),
"lang": current_lang,
"src_script": current_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
elif data[3] == 'slugline':
if args[2][0]:
translated_dataframe[index][2] = translate_comparison2(data[2], kwargs['non_dial_src_lang'],
kwargs['non_dial_dest_lang'])
line_language = kwargs['non_dial_dest_lang']
line_script = language_script[code_2_language[kwargs['non_dial_dest_lang']]]
else:
line_language = kwargs['non_dial_src_lang']
line_script = kwargs['non_dial_src_script']
if args[2][1]:
if line_script != kwargs['non_dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": line_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
else:
if line_script != kwargs['non_dial_src_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_src_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": line_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
elif data[3] == 'parenthetical':
if args[2][2]:
translated_dataframe[index][2] = translate_comparison2(data[2], kwargs['non_dial_src_lang'],
kwargs['non_dial_dest_lang'])
line_language = kwargs['non_dial_dest_lang']
line_script = language_script[code_2_language[kwargs['non_dial_dest_lang']]]
else:
line_language = kwargs['non_dial_src_lang']
line_script = kwargs['non_dial_src_script']
if args[2][3]:
if line_script != kwargs['non_dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": line_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
else:
if line_script != kwargs['non_dial_src_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_src_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": line_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
# here only transliteration will happen and not translation
elif data[3] == 'speaker':
# if args[2][4]:
#
# translated_dataframe[index][2] = translate_comparison2(data[2], kwargs['non_dial_src_lang'],
# kwargs['non_dial_dest_lang'])
# line_language = kwargs['non_dial_dest_lang']
# line_script = language_script(kwargs['non_dial_dest_lang'])
#
# else:
line_language = kwargs['non_dial_src_lang']
line_script = kwargs['non_dial_src_script']
if args[2][5]:
if line_script != kwargs['non_dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": line_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
else:
if line_script != kwargs['non_dial_src_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_src_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": line_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
elif data[3] == 'transition':
if args[2][6]:
translated_dataframe[index][2] = translate_comparison2(data[2], kwargs['non_dial_src_lang'],
kwargs['non_dial_dest_lang'])
line_language = kwargs['non_dial_dest_lang']
line_script = language_script[code_2_language[kwargs['non_dial_dest_lang']]]
else:
line_language = kwargs['non_dial_src_lang']
line_script = kwargs['non_dial_src_script']
if args[2][7]:
if line_script != kwargs['non_dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": line_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
else:
if line_script != kwargs['non_dial_src_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_src_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": line_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
elif data[3] == 'special_term':
if args[2][8]:
translated_dataframe[index][2] = translate_comparison2(data[2], kwargs['non_dial_src_lang'],
kwargs['non_dial_dest_lang'])
line_language = kwargs['non_dial_dest_lang']
line_script = language_script[code_2_language[kwargs['non_dial_dest_lang']]]
else:
line_language = kwargs['non_dial_src_lang']
line_script = kwargs['non_dial_src_script']
if args[2][9]:
if line_script != kwargs['non_dial_dest_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_dest_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": line_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
else:
if line_script != kwargs['non_dial_src_script']:
kwargs2 = {
"dest_script": kwargs['non_dial_src_script'],
"dual_dial_script": "No",
"line": str(data[2]),
"lang": line_language,
"src_script": line_script,
}
translated_dataframe[index][2] = makeTransliteration_only(**kwargs2)
# if dual_dial_script == "Yes":
if translation_instance.dual_dial_script:
converted_file = add_dual_dialogue(translated_dataframe, kwargs['dataframe'], kwargs['non_dial_dest_lang'],
kwargs['dial_dest_lang'], translation_instance.dual_dial_script,
language_2_code[translation_instance.left_dual_dial_lang],
translation_instance.left_dual_dial_script)
else:
converted_file = add_dual_dialogue(translated_dataframe, kwargs['dataframe'], kwargs['non_dial_dest_lang'],
kwargs['dial_dest_lang'], translation_instance.dual_dial_script)
# sample_title = original_script.script_title
if original_script.sample_script:
data = [kwargs['dial_src_lang'], kwargs['non_dial_src_lang'], kwargs['non_dial_dest_lang'],
kwargs['dial_src_script'], kwargs['dial_dest_script'], kwargs['non_dial_src_script'],
kwargs['non_dial_dest_script'], original_script.sample_id.script_title]
else:
data = []
doc_file = get_doc_file(kwargs['dial_dest_lang'], basePath + "/media/" + original_script.script.name, data)
print("Doc Name", doc_file, data)
if not os.path.exists(f'{basePath}/media/scripts/translated'):
os.makedirs(f'{basePath}/media/scripts/translated', mode=0o777)
# Saving the New Translated Doc File
if (translation_instance != None):
if original_script.sample_script:
print(doc_file)
# if dual_dial_script != "Yes":
# converted_df = pd.DataFrame(converted_df,
# columns=['dataframe', 'para_no', 'content', 'script_element'])
# print("Translated and Transliterated DF is", converted_df)
# converted_file = csv_to_docx(converted_df)
converted_file.save(doc_file)
print("convert to pdf called", "and it is inside sample script translations")
# convert_to_pdf(doc_file, "/".join((doc_file.split("/"))[:-1]))
# os.remove(doc_file)
saveFile = doc_file.split('.')[0] + ".pdf"
translation_instance.translated_script_path = "/media/" + str(doc_file.split('media')[1])
# translation_instance.translated_script_pdf = "/media/" + str(saveFile.split('media')[1])
translation_instance.save()
return saveFile
# if dual_dial_script != "Yes":
# converted_df = pd.DataFrame(converted_df, columns=['dataframe', 'para_no', 'content', 'script_element'])
# print("Translated and Transliterated DF is", converted_df)
# converted_file = csv_to_docx(converted_df)
converted_file.save(doc_file)
# convert_to_pdf(doc_file, get_pdf_save_dir())
saveFile = doc_file.split('.')[0] + ".pdf"
translation_instance.translated_script_path = "/media/" + str(doc_file.split('media')[1])
# translation_instance.translated_script_pdf = "/media/" + str(saveFile.split('media')[1])
translation_instance.status = "Translated"
translation_instance.save()
# return saveFile
# print("saved done go to ")
original_stdout = sys.stdout
"""code for saving translated df into script pad for editing"""
if not original_script.sample_script:
'''
saving the converted df to script_csv of the centralised file System
'''
# audited_file_name = "audit_translated.docx"
# converted_df2 = pd.DataFrame(converted_df,
# columns=['dataframe', 'para_no', 'content', 'script_element'])
# converted_df2.to_csv("/home/user/mnf/project/MNF/conversion/translation/translated/test_csv.csv")
# audited_docx = csv_to_docx(converted_df2)
# audited_
# req_file = ContentFile(
# doc_file,
# (doc_file.split("/"))[-1],
# )
# try:
# print("inside try of script_csv saving")
# existing_file = File.objects.get(type="script-original", script_id=audited_id)
# existing_file.file = req_file
# existing_file.save()
# print("script-csv saved in the models")
# except Exception as exp:
# print("Con-Aud Exception = ", exp)
# print("this doesnt work much :(")
# File.objects.update(
# script=Script.objects.get(id=audited_id), # audied_id
# type="script-csv",
# file=req_file,
# )
# print("----> Script id from conv-aud is here=", audited_id).
existing_script = kwargs["iteration_no"]
if existing_script:
audit_name = str(
((doc_file.split("/"))[-1]).split(".")[0]) + f"_{kwargs['iteration_no']}"
else:
audit_name = str(((doc_file.split("/"))[-1]).split(".")[0])
from utils import filesystem
file = ContentFile(
open(doc_file, 'rb').read(),
(doc_file.split("/"))[-1],
)
language_code = "en"
# result = filesystem.new_screenplay(
# request.user,
# request.user.username,
# audit_name,
# file,
# "script-original",
# language_code,
# )
result = filesystem.new_screenplay_without_audit_in_background(
kwargs['user'],
kwargs['user'].username,
str(audit_name),
file,
"script-original",
language_code,
)
audit_id = result.get("script", {}).get("id")
ScriptAuditModel.objects.update_or_create(
script=Script.objects.get(
id=audit_id
),
defaults={"status": States.STARTED}
)
audit = NeutralAudit(audit_id, False)
status = ScriptAuditModel.objects.get(
script=Script.objects.get(
id=audit_id
)
)
try:
audit.audit()
status.status = "SUCCESS"
status.save()
except:
status.status = "FAILURE"
status.save()
print("----> Script id from conv-aud is here=", audit_id)
translation_instance.converted_audit_id = audit_id
translation_instance.save()
sys.stdout = original_stdout
return saveFile