Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,78 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from pykospacing import Spacing
|
| 4 |
+
from hanspell import spell_checker
|
| 5 |
+
import warnings
|
| 6 |
+
|
| 7 |
+
# ๊ฒฝ๊ณ ๋ฉ์์ง ๋ฌด์
|
| 8 |
+
warnings.filterwarnings("ignore")
|
| 9 |
+
|
| 10 |
+
def correct_text(text):
|
| 11 |
+
# ๋์ด์ฐ๊ธฐ ๊ต์
|
| 12 |
+
spacing = Spacing()
|
| 13 |
+
spaced_text = spacing(text)
|
| 14 |
+
|
| 15 |
+
# ๋ง์ถค๋ฒ ๋ฐ ๋์ด์ฐ๊ธฐ ๊ฒ์ฌ
|
| 16 |
+
checked_text = spell_checker.check(spaced_text)
|
| 17 |
+
return checked_text.checked, checked_text.errors
|
| 18 |
+
|
| 19 |
+
def parse_srt(file_path):
|
| 20 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 21 |
+
lines = file.readlines()
|
| 22 |
+
|
| 23 |
+
captions = []
|
| 24 |
+
temp_caption = {'index': None, 'time': None, 'text': ""}
|
| 25 |
+
|
| 26 |
+
for line in lines:
|
| 27 |
+
line = line.strip()
|
| 28 |
+
if line.isdigit():
|
| 29 |
+
if temp_caption['index'] is not None: # ์ ์๋ง์ด ์์๋๋ฉด ๊ธฐ์กด ์๋ง์ ๋ฆฌ์คํธ์ ์ถ๊ฐ
|
| 30 |
+
captions.append(temp_caption)
|
| 31 |
+
temp_caption = {'index': None, 'time': None, 'text': ""}
|
| 32 |
+
temp_caption['index'] = int(line)
|
| 33 |
+
elif '-->' in line:
|
| 34 |
+
temp_caption['time'] = line
|
| 35 |
+
elif line:
|
| 36 |
+
if temp_caption['text']:
|
| 37 |
+
temp_caption['text'] += " " + line
|
| 38 |
+
else:
|
| 39 |
+
temp_caption['text'] = line
|
| 40 |
+
|
| 41 |
+
if temp_caption['index'] is not None: # ๋ง์ง๋ง ์๋ง ์ถ๊ฐ
|
| 42 |
+
captions.append(temp_caption)
|
| 43 |
+
|
| 44 |
+
return captions
|
| 45 |
+
|
| 46 |
+
def spell_check_captions(file):
|
| 47 |
+
captions = parse_srt(file)
|
| 48 |
+
results = []
|
| 49 |
+
for caption in captions:
|
| 50 |
+
corrected_text, errors = correct_text(caption['text'])
|
| 51 |
+
results.append({
|
| 52 |
+
'์๊ฐ': caption['time'],
|
| 53 |
+
'์๋ณธ ์๋ง': caption['text'],
|
| 54 |
+
'์์ ๋ ์๋ง': corrected_text,
|
| 55 |
+
'์์ ํ์ ๋ด์ฉ': ', '.join([f"{error}->{correct}" for error, correct in errors.items() if error != correct])
|
| 56 |
+
})
|
| 57 |
+
|
| 58 |
+
if results:
|
| 59 |
+
df = pd.DataFrame(results)
|
| 60 |
+
output_file = 'corrected_captions.xlsx'
|
| 61 |
+
df.to_excel(output_file, index=False, engine='openpyxl')
|
| 62 |
+
return df, output_file, "Check the result in the table and download the file."
|
| 63 |
+
else:
|
| 64 |
+
return pd.DataFrame(), None, "์์ ํ ๋ด์ฉ์ด ์์ต๋๏ฟฝ๏ฟฝ."
|
| 65 |
+
|
| 66 |
+
iface = gr.Interface(
|
| 67 |
+
fn=spell_check_captions,
|
| 68 |
+
inputs=gr.File(type="filepath", label="์๋ง ํ์ผ ์
๋ก๋"),
|
| 69 |
+
outputs=[
|
| 70 |
+
gr.Dataframe(label="๊ฒ์ฌ ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ"),
|
| 71 |
+
gr.File(label="๊ฒฐ๊ณผ ์์
ํ์ผ ๋ค์ด๋ก๋"),
|
| 72 |
+
gr.Textbox(label="๋ฉ์์ง")
|
| 73 |
+
],
|
| 74 |
+
title="์๋ง ๊ฒ์ฌ ๋ฐ ์์ ",
|
| 75 |
+
description="์๋ง ํ์ผ์ ์
๋ก๋ํ๊ณ , ์์ ํ ๋ด์ฉ์ด ์๋ ๊ฒฝ์ฐ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ์ธ์."
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
iface.launch()
|