Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
from datetime import datetime
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
|
7 |
+
def millisec_to_srt_time(millisec):
|
8 |
+
"""แปลงเวลาจากวินาทีเป็นรูปแบบ SRT"""
|
9 |
+
hours = int(millisec / 3600)
|
10 |
+
minutes = int((millisec % 3600) / 60)
|
11 |
+
seconds = int(millisec % 60)
|
12 |
+
milliseconds = int((millisec - int(millisec)) * 1000)
|
13 |
+
|
14 |
+
return f"{hours:02d}:{minutes:02d}:{seconds:02d},{milliseconds:03d}"
|
15 |
+
|
16 |
+
def convert_json_to_srt(json_content):
|
17 |
+
"""แปลง JSON content เป็น SRT format"""
|
18 |
+
try:
|
19 |
+
data = json.loads(json_content)
|
20 |
+
srt_content = ""
|
21 |
+
|
22 |
+
for i, subtitle in enumerate(data, 1):
|
23 |
+
srt_content += f"{i}\n"
|
24 |
+
start_time = millisec_to_srt_time(subtitle['start'])
|
25 |
+
end_time = millisec_to_srt_time(subtitle['end'])
|
26 |
+
srt_content += f"{start_time} --> {end_time}\n"
|
27 |
+
srt_content += f"{subtitle['text'].strip()}\n\n"
|
28 |
+
|
29 |
+
return srt_content
|
30 |
+
except Exception as e:
|
31 |
+
return f"เกิดข้อผิดพลาด: {str(e)}"
|
32 |
+
|
33 |
+
def process_input(file_input=None, text_input=None):
|
34 |
+
"""ฟังก์ชันประมวลผลทั้งไฟล์และข้อความ"""
|
35 |
+
try:
|
36 |
+
if file_input is not None:
|
37 |
+
json_content = file_input.decode('utf-8')
|
38 |
+
elif text_input:
|
39 |
+
json_content = text_input
|
40 |
+
else:
|
41 |
+
return "กรุณาอัปโหลดไฟล์หรือวางข้อความ JSON", None
|
42 |
+
|
43 |
+
srt_preview = convert_json_to_srt(json_content)
|
44 |
+
|
45 |
+
temp_srt = tempfile.NamedTemporaryFile(delete=False, suffix='.srt', mode='w', encoding='utf-8')
|
46 |
+
temp_srt.write(srt_preview)
|
47 |
+
temp_srt.close()
|
48 |
+
|
49 |
+
return srt_preview, temp_srt.name
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
return f"เกิดข้อผิดพลาด: {str(e)}", None
|
53 |
+
|
54 |
+
def toggle_visibility(mode):
|
55 |
+
"""สลับการแสดงผลระหว่างโหมด"""
|
56 |
+
if mode == "file":
|
57 |
+
return gr.update(visible=True), gr.update(visible=False), "Copy-Paste Mode"
|
58 |
+
else:
|
59 |
+
return gr.update(visible=False), gr.update(visible=True), "File Upload Mode"
|
60 |
+
|
61 |
+
# สร้าง Gradio Interface
|
62 |
+
with gr.Blocks(title="JSON Subtitle to SRT Converter") as iface:
|
63 |
+
gr.Markdown("# แปลง JSON Subtitle เป็น SRT")
|
64 |
+
|
65 |
+
# ปุ่มสลับโหมด
|
66 |
+
mode = gr.State("file")
|
67 |
+
toggle_btn = gr.Button("Copy-Paste Mode", variant="secondary")
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column():
|
71 |
+
# ส่วนอัปโหลดไฟล์
|
72 |
+
file_input = gr.File(
|
73 |
+
label="อัปโหลดไฟล์ JSON Subtitle",
|
74 |
+
file_types=[".json", ".txt"],
|
75 |
+
type="binary",
|
76 |
+
visible=True
|
77 |
+
)
|
78 |
+
|
79 |
+
# ส่วนรับข้อความ
|
80 |
+
text_input = gr.Textbox(
|
81 |
+
label="วางข้อความ JSON ที่นี่",
|
82 |
+
lines=10,
|
83 |
+
placeholder="วางข้อความ JSON ที่นี่...",
|
84 |
+
visible=False
|
85 |
+
)
|
86 |
+
|
87 |
+
convert_btn = gr.Button("แปลงเป็น SRT", variant="primary")
|
88 |
+
|
89 |
+
with gr.Column():
|
90 |
+
# ส่วนแสดง preview
|
91 |
+
preview = gr.Textbox(
|
92 |
+
label="Preview SRT",
|
93 |
+
lines=10,
|
94 |
+
max_lines=20,
|
95 |
+
interactive=False
|
96 |
+
)
|
97 |
+
|
98 |
+
# ปุ่มดาวน์โหลด
|
99 |
+
download_button = gr.File(
|
100 |
+
label="ดาวน์โหลด SRT",
|
101 |
+
interactive=True
|
102 |
+
)
|
103 |
+
|
104 |
+
# เชื่อมต่อการทำงาน
|
105 |
+
toggle_btn.click(
|
106 |
+
fn=toggle_visibility,
|
107 |
+
inputs=[mode],
|
108 |
+
outputs=[file_input, text_input, toggle_btn],
|
109 |
+
).then(
|
110 |
+
fn=lambda x: "file" if x == "text" else "text",
|
111 |
+
inputs=[mode],
|
112 |
+
outputs=[mode]
|
113 |
+
)
|
114 |
+
|
115 |
+
# การแปลงไฟล์
|
116 |
+
convert_btn.click(
|
117 |
+
fn=process_input,
|
118 |
+
inputs=[file_input, text_input],
|
119 |
+
outputs=[preview, download_button]
|
120 |
+
)
|
121 |
+
|
122 |
+
# รัน Gradio app
|
123 |
+
if __name__ == "__main__":
|
124 |
+
iface.launch()
|