|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
|
|
<style> |
|
body { |
|
font-family: 'Roboto', sans-serif; |
|
font-size: 16px; |
|
} |
|
.logo { |
|
height: 1em; |
|
vertical-align: middle; |
|
margin-bottom: 0.1em; |
|
} |
|
</style> |
|
|
|
<script type="module" crossorigin src="https://cdn.jsdelivr.net/npm/@gradio/lite@0.4.1/dist/lite.js"></script> |
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@gradio/lite@0.4.1/dist/lite.css" /> |
|
</head> |
|
<body> |
|
<h2> |
|
|
|
Gradio-lite (Gradio running entirely in your browser!) |
|
</h2> |
|
<p>Try it out! Once the Gradio app loads (can take 10-15 seconds), disconnect your Wifi and the machine learning model will still work!</p> |
|
<gradio-lite> |
|
|
|
<gradio-file name="app.py" entrypoint> |
|
import gradio as gr |
|
from tempfile import NamedTemporaryFile |
|
|
|
def translate_subtitles(english_srt, translated_json): |
|
with open(english_srt, 'r', encoding='utf-8') as file: |
|
english_subtitles = file.read().strip().split('\n\n') |
|
|
|
with open(translated_json, 'r', encoding='utf-8') as file: |
|
translated_texts = eval(file.read()) |
|
# 将上传的JSON文件内容转化为字典 |
|
# 确保翻译文本的数量与SRT文件中的条目数量相同 |
|
assert len(translated_texts) == len(english_subtitles) |
|
|
|
# 用来存储最终翻译的SRT内容 |
|
translated_srt_content = [] |
|
|
|
# 遍历SRT文件中的每个条目,并用翻译后的文本替换原文本 |
|
for index, block in enumerate(english_subtitles): |
|
lines = block.split('\n') |
|
translated_srt_content.append(lines[0]) # 序号 |
|
translated_srt_content.append(lines[1]) # 时间轴 |
|
translated_srt_content.append(translated_texts[index]) # 翻译文本 |
|
translated_srt_content.append('') # 添加空行作为字幕块的分隔 |
|
|
|
# 将列表转换为字符串,每个元素之间以换行符连接 |
|
translated_srt_string = '\n'.join(translated_srt_content) |
|
with NamedTemporaryFile(delete=False, suffix='.srt', mode='w', encoding='utf-8') as tmp_file: |
|
tmp_file.write(translated_srt_string) |
|
return tmp_file.name # 返回文件路径以便下载 |
|
|
|
# 创建Gradio接 |
|
iface = gr.Interface( |
|
fn=translate_subtitles, |
|
inputs=[ |
|
gr.File(label="Upload English SRT File"), |
|
gr.File(label="Upload Translated Texts JSON File") |
|
], |
|
outputs=gr.File(label="Download Translated SRT File") |
|
|
|
) |
|
|
|
# 启动Gradio界面 |
|
iface.launch() |
|
</gradio-file> |
|
|
|
</gradio-lite> |
|
</body> |
|
</html> |