|
| 1 | +from langchain.prompts import ( |
| 2 | + ChatPromptTemplate, |
| 3 | + SystemMessagePromptTemplate, |
| 4 | + HumanMessagePromptTemplate, |
| 5 | +) |
| 6 | + |
| 7 | +from langchain.chat_models import ChatOpenAI |
| 8 | +from langchain.chains import LLMChain |
| 9 | + |
| 10 | +import sys |
| 11 | +import time |
| 12 | + |
| 13 | +WINDOW = 50 |
| 14 | + |
| 15 | +def translate(text, input="English", output="Portuguese"): |
| 16 | + template="You are a helpful assistant that translates from {input_language} to {output_language}. Translate without changing the original format." |
| 17 | + system_message_prompt = SystemMessagePromptTemplate.from_template(template) |
| 18 | + |
| 19 | + human_template="{text}" |
| 20 | + human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) |
| 21 | + chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) |
| 22 | + |
| 23 | + chain = LLMChain(llm=ChatOpenAI(temperature=0, model_name='gpt-3.5-turbo'), prompt=chat_prompt) |
| 24 | + |
| 25 | + # response = chain.run({"input_language": "Spanish", "output_language": "Portuguese", "text" :""" |
| 26 | + # 1 |
| 27 | + # 00:01:32,885 --> 00:01:36,388 |
| 28 | + # <i>Cuando era niño, mi maestro me preguntó</i> |
| 29 | + |
| 30 | + # 2 |
| 31 | + # 00:01:36,388 --> 00:01:40,976 |
| 32 | + # <i>cuáles serían las tres cosas que salvaría |
| 33 | + # si mi casa se incendiara.</i> |
| 34 | + |
| 35 | + # 3 |
| 36 | + # 00:01:43,854 --> 00:01:50,277 |
| 37 | + # <i>Le respondí: "Mi cuaderno de dibujo, |
| 38 | + # mi álbum de AC/DC y a mi gato Enojón".</i> |
| 39 | + |
| 40 | + # 4 |
| 41 | + # 00:01:57,910 --> 00:02:00,412 |
| 42 | + # <i>No mencioné a mis papás ni a mi hermana.</i> |
| 43 | + |
| 44 | + # 5 |
| 45 | + # 00:02:01,288 --> 00:02:03,498 |
| 46 | + # <i>Los otros niños sí lo hicieron.</i> |
| 47 | + |
| 48 | + # 6 |
| 49 | + # 00:02:04,791 --> 00:02:06,793 |
| 50 | + # <i>¿Eso me hace una mala persona?</i> |
| 51 | + |
| 52 | + # 7 |
| 53 | + # 00:02:11,840 --> 00:02:13,467 |
| 54 | + # <i>Mi gato se murió,</i> |
| 55 | + # """}) |
| 56 | + |
| 57 | + response = chain.run({"input_language": input, "output_language": output, "text" : text}) |
| 58 | + return response |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == '__main__': |
| 63 | + if len(sys.argv) > 1: |
| 64 | + fname = sys.argv[1] |
| 65 | + else: |
| 66 | + fname = "test.srt" |
| 67 | + with open(fname, encoding='utf-8-sig') as file: |
| 68 | + content = file.readlines() |
| 69 | + try: |
| 70 | + translated_text = "" |
| 71 | + while len(content) > 0: |
| 72 | + chunk = [] |
| 73 | + while len(content) > 0: |
| 74 | + line = content.pop(0) |
| 75 | + chunk.append(line) |
| 76 | + if line.strip().isnumeric(): |
| 77 | + if len(chunk) > WINDOW: |
| 78 | + content.insert(0, line) |
| 79 | + chunk.pop() |
| 80 | + break |
| 81 | + time.sleep(5) |
| 82 | + text = ''.join(chunk) |
| 83 | + response = translate(text, input="Spanish") |
| 84 | + translated_text += f"{response}\n\n" |
| 85 | + except Exception as ex: |
| 86 | + print(ex) |
| 87 | + |
| 88 | +print(translated_text) |
0 commit comments