{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import json" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "INSTRUCT_CHUNKED_PROMPT = \"\"\"你是一个擅长翻译科技新闻的翻译专家。请将以下内容翻译为中文,使用相同格式输出,并保留时间戳。不要漏掉任何信息。合并多行文本时,保留第一个和最后一个时间戳,样例如下:\n", "输入:\n", "[10.54] Qualcomm has announced a new series of laptop processors [13.80]\n", "[13.80] representing a quantum leap forward [16.06]\n", "[16.06] in performance and power efficiency. [18.14]\n", "\n", "输出:\n", "[10.54] 高通发布了一系列新的手机处理器 [13.80]\n", "[13.80] 在性能和功耗上都取得了巨大的跃进 [18.14]\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def new_message(eng_in, chs_out, prev_in = None, prev_out = None):\n", " if(prev_in == None or prev_out == None):\n", " return {\"messages\": [\n", " {\"role\": \"system\", \"content\": INSTRUCT_CHUNKED_PROMPT}, \n", " {\"role\": \"user\", \"content\": eng_in}, \n", " {\"role\": \"assistant\", \"content\": chs_out}]\n", " }\n", " else:\n", " return {\"messages\": [\n", " {\"role\": \"system\", \"content\": INSTRUCT_CHUNKED_PROMPT}, \n", " {\"role\": \"user\", \"content\": prev_in}, \n", " {\"role\": \"assistant\", \"content\": prev_out},\n", " {\"role\": \"user\", \"content\": eng_in}, \n", " {\"role\": \"assistant\", \"content\": chs_out}]\n", " }" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def write_jsonl(message_groups, filename):\n", " with open(filename, \"w\") as fout:\n", " for i in range(len(message_groups)):\n", " if(i>0):\n", " msg_obj = new_message(\n", " message_groups[i][0].strip(),\n", " message_groups[i][1].strip(),\n", " message_groups[i-1][0].strip(),\n", " message_groups[i-1][1].strip()\n", " )\n", " else:\n", " msg_obj = new_message(\n", " message_groups[i][0].strip(),\n", " message_groups[i][1].strip()\n", " )\n", " json.dump(msg_obj, fout)\n", " fout.write(\"\\n\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "message_groups = []" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found data pair: data\\Apple, Are U Ok? [34R2xVzBmfA].en.txt and data\\Apple, Are U Ok? [34R2xVzBmfA].cn.txt\n", "Found data pair: data\\China lost its GPU privileges [Wz9JoTTRa4k].en.txt and data\\China lost its GPU privileges [Wz9JoTTRa4k].cn.txt\n", "Found data pair: data\\It's Finally Over. [KJF5Nn23CwM]-4k.en.txt and data\\It's Finally Over. [KJF5Nn23CwM]-4k.cn.txt\n", "Found data pair: data\\Watch Out, Apple... [5v72vj2nFN0].en.txt and data\\Watch Out, Apple... [5v72vj2nFN0].cn.txt\n" ] } ], "source": [ "import os\n", "from os import listdir\n", "from os.path import isfile, join\n", "\n", "DOCUMENT_ROOT = \"data\"\n", "files = listdir(\"data\")\n", "\n", "for f in files:\n", " en_fname = join(DOCUMENT_ROOT, f)\n", " if en_fname.endswith(\".en.txt\") and isfile(en_fname):\n", " cn_fname = join(DOCUMENT_ROOT, f.replace(\".en.txt\", \".cn.txt\"))\n", " if os.path.exists(cn_fname) and isfile(cn_fname):\n", " print(f\"Found data pair: {en_fname} and {cn_fname}\")\n", "\n", " with open(en_fname, \"r\") as enfin:\n", " en_messages = enfin.read()\n", "\n", " with open(cn_fname, \"r\") as cnfin:\n", " cn_messages = cnfin.read()\n", " \n", " en_messages = [part.strip() for part in en_messages.split(\"\\n\\n\") if part.strip() != \"\"]\n", " cn_messages = [part.strip() for part in cn_messages.split(\"\\n\\n\") if part.strip() != \"\"]\n", "\n", " if(len(en_messages) != len(cn_messages)):\n", " print(f\"English and Chinese version mismatch. Discarding {en_fname} pair.\")\n", " \n", " messages = zip(en_messages, cn_messages)\n", "\n", " message_groups.extend(messages)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "write_jsonl(message_groups, \"combined.jsonl\")" ] } ], "metadata": { "kernelspec": { "display_name": "whisper", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 2 }