Skip to content

【建议】[模块名称:public.py] 的 翻译功能和读取srt文件 #99

@GNthword

Description

@GNthword

模块名称

public.py

建议描述

translate_srt,中所有网络请求应该加上 try except Exception,毕竟这是基础常识网络请求放线程里并加上try catch。
read_srt_file,中没有处理空行的问题,毕竟自动识别出来的很垃圾,删减很正常,导致内容为空,进一步导致下一个内容的第一行为空了,这样下一个内容被跳过了。

34
00:04:02,520 --> 00:04:03,420
//空行
//空行
35
00:04:03,420 --> 00:04:05,700
ありがとうございました

第35条内容就会异常

预期行为

translate_srt我是自己改了

           if text != "":
                try:
                    response = client.chat.completions.create(
                        model=model,
                        messages=[
                            {"role": "system", "content": system_prompt},
                            {"role": "user", "content": current_user_prompt + str(text)}
                        ])
                    answer = response.choices[0].message.content
                except Exception as e:
                    print("发生错误:", e)
                    answer = "翻译发生异常,源文本为:"+str(text)
            else:
                answer = ""

read_srt_file里我是加了判断

  for block in srt:
        lines = block.split('\n')
        i=i+1
        index = 0
        if len(lines) >= 3:
            if lines[0] == "":
                index=1
            num = lines[index]
            times = lines[index+1]
            text = "\n".join(lines[(index+2):])

            if timestamp_pattern.match(times):
                subtitles.append({
                    'number': num,
                    'time': times,
                    'text': text
                })

其他信息(可选)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions