鱼C论坛

 找回密码
 立即注册
查看: 350|回复: 3

怎么才能让gpt4free帮我翻译srt字幕文件呢?

[复制链接]
发表于 2024-10-25 16:09:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import g4f
  2. from g4f.client import Client

  3. client = Client()

  4. response = client.chat.completions.create(
  5.     model="gpt-3.5-turbo",
  6.     messages=[
  7.         {
  8.             "role": "user",
  9.             "content": "How do I make a cup of coffee? "
  10.         }
  11.     ]
  12.     # Add any other necessary parameters
  13. )

  14. print(response.choices[0].message.content)
复制代码

这个做coffee的问题,它回答的很好。


但下面这个翻译srt字幕的代码,没有结果
  1. import g4f
  2. from g4f.client import Client

  3. client = Client()
  4. def srtFileGPTtran(questionfile, srtZhFileNameAndPath):
  5.     questionfile = "translate the following subtitles text to Chinese, treat as one continuous text:\n" + open(questionfile, "r", encoding="utf-8").read()
  6.     print(questionfile, 'chatGPT is working .....................................')

  7.     response = client.chat.completions.create(
  8.         model="gpt-3.5-turbo",
  9.         messages=[
  10.             {
  11.                 "role": "system", "content": "You are a professional subtitle translator.",
  12.                 "role": "user", "content": questionfile
  13.             }
  14.         ]
  15.         # Add any other necessary parameters
  16.     )
  17.     reply = response.choices[0].message.content
  18.     with open(srtZhFileNameAndPath, "w", encoding="utf-8") as file:
  19.         file.write(reply)

  20. srtEnFileNameMergeAndPath = './1fr.srt'
  21. srtZhFileNameAndPath = './1zh.srt'
  22. srtFileGPTtran(srtEnFileNameMergeAndPath, srtZhFileNameAndPath)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-10-25 16:09:25 | 显示全部楼层
为了让你的代码能够正常工作并翻译 srt 字幕文件,你需要确保以下几点:
1. 正确设置 `messages` 参数。
2. 异常处理(如处理 API 请求失败的情况)。

下面是修正后的代码示例,包含了必要的参数和处理逻辑:

  1. import g4f
  2. from g4f.client import Client

  3. client = Client()

  4. def srtFileGPTtran(questionfile, srtZhFileNameAndPath):
  5.     with open(questionfile, "r", encoding="utf-8") as file:
  6.         srt_content = file.read()

  7.     question_content = "translate the following subtitles text to Chinese, treat as one continuous text:\n" + srt_content
  8.     print("ChatGPT is working...")

  9.     response = client.chat.completions.create(
  10.         model="gpt-3.5-turbo",
  11.         messages=[
  12.             {"role": "system", "content": "You are a professional translator."},
  13.             {"role": "user", "content": question_content}
  14.         ]
  15.     )

  16.     reply = response.choices[0].message['content']
  17.     with open(srtZhFileNameAndPath, "w", encoding="utf-8") as file:
  18.         file.write(reply)

  19. srtEnFileNameMergeAndPath = './1fr.srt'
  20. srtZhFileNameAndPath = './1zh.srt'
  21. srtFileGPTtran(srtEnFileNameMergeAndPath, srtZhFileNameAndPath)
复制代码


让我们来解释一下关键部分:

1. **读取文件内容**:
  
  1. with open(questionfile, "r", encoding="utf-8") as file:
  2.        srt_content = file.read()
复制代码


2. **构建问题内容**:
  
  1. question_content = "translate the following subtitles text to Chinese, treat as one continuous text:\n" + srt_content
复制代码


3. **创建消息**:
  
  1. response = client.chat.completions.create(
  2.        model="gpt-3.5-turbo",
  3.        messages=[
  4.            {"role": "system", "content": "You are a professional translator."},
  5.            {"role": "user", "content": question_content}
  6.        ]
  7.    )
复制代码


4. **处理回复并写入文件**:
  
  1. reply = response.choices[0].message['content']
  2.    with open(srtZhFileNameAndPath, "w", encoding="utf-8") as file:
  3.        file.write(reply)
复制代码


确保你要翻译的文件路径和文件名都是正确的。此外,请确保你的 `g4f` API 调用配置和参数都是准确无误的。这样可以确保你的代码能够正常运行并生成翻译后的 srt 文件。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-10-25 16:21:12 | 显示全部楼层
FishC 发表于 2024-10-25 16:09
为了让你的代码能够正常工作并翻译 srt 字幕文件,你需要确保以下几点:
1. 正确设置 `messages` 参数。
...

在把 reply = response.choices[0].message['content']  改为 reply = response.choices[0].message.content 后
执行结果是:  Hello! How can I assist you today?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-10-25 16:21:25 | 显示全部楼层
blackantt 发表于 2024-10-25 16:21
在把 reply = response.choices.message  改为 reply  ...


看起来你的代码缺少必要的参数传递以及正确构建 API 请求信息的方式。我们需要传递模型名称、信息内容和消息格式给 API。下面是一个具体的示例代码,确保正确地处理 GPT-3.5-turbo API 的调用:

  1. import g4f
  2. from g4f.client import Client

  3. def srtFileGPTtran(questionfile, srtZhFileNameAndPath):
  4.     with open(questionfile, "r", encoding="utf-8") as file:
  5.         srt_content = file.read()

  6.     question_content = "Translate the following subtitles text to Chinese, treat as one continuous text:\n" + srt_content
  7.     print("ChatGPT is working...")

  8.     client = Client()

  9.     response = client.chat.completions.create(
  10.         model="gpt-3.5-turbo",
  11.         messages=[
  12.             {"role": "system", "content": "You are a helpful assistant."},
  13.             {"role": "user", "content": question_content}
  14.         ]
  15.     )

  16.     reply = response.choices[0].message['content']

  17.     with open(srtZhFileNameAndPath, "w", encoding="utf-8") as file:
  18.         file.write(reply)
  19.     print(f"Translation complete! The translated subtitles are saved in {srtZhFileNameAndPath}")

  20. # 使用示例
  21. srtEnFileNameMergeAndPath = './1fr.srt'
  22. srtZhFileNameAndPath = './1zh.srt'
  23. srtFileGPTtran(srtEnFileNameMergeAndPath, srtZhFileNameAndPath)
复制代码


### 关键点解释:

1. **构建消息格式**:
  
  1. messages=[
  2.        {"role": "system", "content": "You are a helpful assistant."},
  3.        {"role": "user", "content": question_content}
  4.    ]
复制代码

   `role` 参数有 `system` 和 `user`,分别表示系统信息和用户输入。`content` 参数包含实际内容。

2. **从响应中获取内容**:
  
  1. reply = response.choices[0].message['content']
复制代码

   这确保我们正确提取到了 GPT-3.5-turbo 返回的文本。

3. **错误处理**:
   你可能还想考虑添加错误处理机制,以防 API 请求失败。如:
  
  1. try:
  2.        response = client.chat.completions.create(...)
  3.        # 处理响应
  4.    except Exception as e:
  5.        print(f"An error occurred: {e}")
复制代码


### 注意事项:

- 确保你有正确的 API 访问权限,并且 `g4f` 模块已经正确配置。
- 确保输入和输出文件路径是正确的。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 16:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表