Skip to content

Instantly share code, notes, and snippets.

@sunlei
Last active January 23, 2024 14:52
Show Gist options
  • Save sunlei/44f211cdf817c1373b4380e401161d17 to your computer and use it in GitHub Desktop.
Save sunlei/44f211cdf817c1373b4380e401161d17 to your computer and use it in GitHub Desktop.
使用 ChatGPT Few-shot 翻译 SRT 字幕
import json
import os
import requests
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
OPENAI_BASE_URL = "https://api.openai.com/v1/chat/completions"
def translator(srt):
prompt = "You are an expert subtitle translator in all languages. Translate all language subtitle to Chinese subtitle, and keep the original language sentence and the new Chinese subtitle. You need to respond in a fixed format."
few_shot_text_request = (
"1\n"
"00:00:00,000 --> 00:00:07,800\n"
"Ladies and gentlemen, let's welcome Mr. L.M.A.S, the co-founder and CEO at Tesla, and Jack Ma,\n"
# "\n"
# "2\n"
# "00:00:07,800 --> 00:00:11,680\n"
# "the co-chair of the UN high-level panel on digital cooperation.\n"
)
few_shot_text_response = (
"1\n"
"00:00:00,000 --> 00:00:07,800\n"
"Ladies and gentlemen, let's welcome Mr. L.M.A.S, the co-founder and CEO at Tesla, and Jack Ma,\n"
"女士们先生们,让我们欢迎特斯拉公司的联合创始人兼首席执行官L.M.A.S先生和数字合作联合国高级别小组的联合主席马云先生。\n"
# "\n"
# "2\n"
# "00:00:07,800 --> 00:00:11,680\n"
# "the co-chair of the UN high-level panel on digital cooperation.\n"
# "数字合作联合国高级别小组的联合主席。\n"
)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}",
}
data = {
"model": "gpt-3.5-turbo",
# "temperature": 0,
# "top_p": 1,
# "frequency_penalty": 1,
# "presence_penalty": 1,
"stream": False,
"messages": [
{"role": "system", "content": prompt},
{"role": "user", "content": few_shot_text_request},
{"role": "assistant", "content": few_shot_text_response},
{"role": "user", "content": srt},
],
}
response = requests.post(OPENAI_BASE_URL, headers=headers, json=data)
answer = response.json()["choices"][0]["message"]["content"].strip()
print(answer)
if __name__ == "__main__":
test_srt = (
"1\n"
"00:00:00,000 --> 00:00:07,800\n"
"Ladies and gentlemen, let's welcome Mr. L.M.A.S, the co-founder and CEO at Tesla, and Jack Ma,\n"
"\n"
"2\n"
"00:00:07,800 --> 00:00:11,680\n"
"the co-chair of the UN high-level panel on digital cooperation.\n"
"\n"
"3\n"
"00:00:11,680 --> 00:00:17,680\n"
"Let's give them a very big applause.\n"
"\n"
"4\n"
"00:00:17,680 --> 00:00:18,680\n"
"Welcome.\n"
"\n"
"5\n"
"00:00:18,680 --> 00:00:26,000\n"
"Great to have both of you here with us today.\n"
"\n"
"6\n"
"00:00:26,000 --> 00:00:28,160\n"
"The session is actually much awaited.\n"
"\n"
"7\n"
"00:00:28,160 --> 00:00:32,680\n"
"We already posted some of the key words of the questions on the big screen.\n"
"\n"
"8\n"
"00:00:32,680 --> 00:00:36,240\n"
"Later on, you can just click the word you're interested in to talk about.\n"
"\n"
"9\n"
"00:00:36,240 --> 00:00:43,240\n"
"Now the stage is yours.\n"
"\n"
"10\n"
"00:00:43,240 --> 00:00:45,720\n"
"What are you supposed to say?\n"
)
translator(test_srt)
@zhang202203
Copy link

电影字幕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment