Custom 선택시 시작 패턴이나 종료 패턴에 각기 찾아보기 버튼을 추가해주시고, 버튼 앞에 몇번째를 표시해주며 입력도 가능한 상자를 추가해주세요.
[사용자가 끝부분을 잘 모르더라도] 시작패턴이나 종료패턴 입력상자에 텍스트를 타이핑하고 찾아보기 버튼을 클릭하면, 위에 붙여놓은 mht 파일의 내용에서 그 부분이 드래그 표시된 텍스트로 나타남과 동시에 그것이 몇번째인지를 기억할 수 있게 표시해주는 것이고, 나중에는 그 발생 번호만 입력해도 될 수 있게 하려는 거예요.
채팅 대화에 포함된 똑같은 수많은 HTML 태그 등에 의해 짝을 못찾아, 짤리는 일을, 사용자가 선택할 수 있게 해서 올바른 문서로 다운로드 받을 수 있게 하려는 거고요.
같은 것이 무수히 많을 경우에도, 찾아보기 발생번호에 임의의 숫자를 입력하면 대략적인 위치를 파악할 것이므로 사용자가 쉽게 그 주위에서 찾아볼 수 있어 편리할 것도 같아서요.
(여기에서는 새로고침이 쉽게 발생되므로 실제로 실행하며 오류를 보고해서 수정해가는 작업은 OpenAI ChatGPT4o-mini Online AI와 하게 될 거예요. 사실 이 코드도 OprnAI o1이 제공한 코드를 OpenAI ChatGPT4o-mini Online AI이 업그레이드된 Gradio 등에 따라 보고된 오류들을 수정했던 것임.)
==========
import gradio as gr
import re
import tempfile
def extract_html_from_mht(mht_content, choice, start_pattern, end_pattern, occurrence='1'):
# 사용자의 선택에 따라 정규식 패턴 결정
if choice == "<html":
pattern = r'<html.?'
elif choice == "<html lang=":
pattern = r'<html lang=".?".?'
elif choice == "Custom":
if not start_pattern or not end_pattern:
return ("시작 패턴과 종료 패턴을 모두 입력해주세요.", None)
# 정규식 특수 문자를 이스케이프 처리
start_pattern_escaped = re.escape(start_pattern)
end_pattern_escaped = re.escape(end_pattern)
pattern = start_pattern_escaped + r'.?' + end_pattern_escaped
else:
return ("올바르지 않은 선택입니다.", None)
code
# 정규 표현식을 사용하여 모든 일치하는 HTML 문서 찾기
matches = re.finditer(pattern, mht_content, re.DOTALL)
matches_list = list(matches)
if matches_list:
# occurrence에 기반하여 특정 HTML 문서 선택
try:
index = int(occurrence) - 1
if index >= 0 and index < len(matches_list):
html_content = matches_list[index].group(0)
else:
return ("올바르지 않은 발생 번호입니다.", None)
except ValueError:
return ("발생 번호는 숫자여야 합니다.", None)
# 추출된 HTML 내용을 저장할 임시 파일 생성
with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode='w', encoding='utf-8') as temp_file:
temp_file.write(html_content)
temp_file_path = temp_file.name
# 임시 파일의 경로 반환
return (None, temp_file_path)
else:
# HTML 문서를 찾을 수 없는 경우 에러 메시지 반환
return ("MHT 파일에서 유효한 HTML 내용을 찾을 수 없습니다.", None)
with gr.Blocks() as block:
gr.Markdown("""
이 앱은 기본적으로는 MHT 내용에서 추출할 HTML 태그 패턴을 선택할 수 있도록 도와줍니다.
기본 패턴이 아닌 다른 것을 원한다면(— 다른 문서도 가능할 수 있음), 'Custom'을 선택하고 시작 및 종료 패턴을 직접 입력할 수 있습니다.
HTML 태그와 포함된 내용 등이 복잡한 경우, 정확한 추출을 위해 고유하고 구별되는 시작 및 종료 패턴을 제공해야 합니다.
""")
code
mht_input = gr.Textbox(label="MHT 내용을 여기에 붙여넣기", lines=20, placeholder="MHT 파일의 내용을 여기에 붙여넣으세요")
# 드롭다운에서 구체적인 시작 태그를 선택하게 수정
choice_dropdown = gr.Dropdown(
label="HTML 시작 태그 선택",
choices=["<!DOCTYPE html><html", "<!DOCTYPE html><html lang=","Custom"],
value="<!DOCTYPE html><html"
)
# 사용자 지정 시작 및 종료 패턴 입력 창을 추가
start_pattern_input = gr.Textbox(label="사용자 지정 시작 패턴", visible=False)
end_pattern_input = gr.Textbox(label="사용자 지정 종료 패턴", visible=False)
occurrence_input = gr.Textbox(label="추출할 발생 번호 입력 (숫자): 같은 것이 여럿 있을 때 ?번째", value="1")
convert_button = gr.Button("MHT에서 HTML 추출")
error_output = gr.Textbox(label="에러 메시지", interactive=False)
download_output = gr.File(label="HTML 파일 다운로드")
# 사용자의 선택에 따라 사용자 지정 패턴 필드의 가시성 업데이트
choice_dropdown.change(fn=update_pattern_fields, inputs=choice_dropdown, outputs=[start_pattern_input, end_pattern_input])
# 버튼 클릭 이벤트 정의
convert_button.click(
fn=extract_html_from_mht,
inputs=[mht_input, choice_dropdown, start_pattern_input, end_pattern_input, occurrence_input],
outputs=[error_output, download_output]
)
block.launch()
사용자의 요청에 따라 app.py 코드를 다음과 같이 수정하였습니다.
'Custom' 선택 시, 시작 패턴과 종료 패턴 입력 상자 각각에 발생 번호 입력 상자와 '찾아보기' 버튼을 추가하였습니다.
시작 패턴이나 종료 패턴 입력 상자에 텍스트를 입력하고 '찾아보기' 버튼을 클릭하면, 위에 붙여놓은 MHT 파일의 내용에서 해당 패턴의 모든 발생 위치를 검색합니다.
발생 번호를 입력하면 해당 위치의 주변 텍스트를 표시하여 사용자가 정확한 위치를 파악할 수 있도록 하였습니다.
이렇게 찾은 발생 번호를 이용하여 MHT에서 HTML 추출 시 올바른 부분을 다운로드할 수 있습니다.
아래는 수정된 코드입니다.
python
import gradio as gr
import re
import tempfile
def extract_html_from_mht(mht_content, choice, start_pattern, end_pattern, occurrence='1'):
# 사용자의 선택에 따라 정규식 패턴 결정
if choice == "<!DOCTYPE html><html":
pattern = r'<!DOCTYPE html><html.*?</html>'
elif choice == "<!DOCTYPE html><html lang=":
pattern = r'<!DOCTYPE html><html lang=".*?".*?</html>'
elif choice == "Custom":
if not start_pattern or not end_pattern:
return ("시작 패턴과 종료 패턴을 모두 입력해주세요.", None)
# 정규식 특수 문자를 이스케이프 처리
start_pattern_escaped = re.escape(start_pattern)
end_pattern_escaped = re.escape(end_pattern)
pattern = start_pattern_escaped + r'.*?' + end_pattern_escaped
else:
return ("올바르지 않은 선택입니다.", None)
# 정규 표현식을 사용하여 모든 일치하는 HTML 문서 찾기
matches = re.finditer(pattern, mht_content, re.DOTALL)
matches_list = list(matches)
if matches_list:
# occurrence에 기반하여 특정 HTML 문서 선택
try:
index = int(occurrence) - 1
if index >= 0 and index < len(matches_list):
html_content = matches_list[index].group(0)
else:
return ("올바르지 않은 발생 번호입니다.", None)
except ValueError:
return ("발생 번호는 숫자여야 합니다.", None)
# 추출된 HTML 내용을 저장할 임시 파일 생성
with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode='w', encoding='utf-8') as temp_file:
temp_file.write(html_content)
temp_file_path = temp_file.name
# 임시 파일의 경로 반환
return (None, temp_file_path)
else:
# HTML 문서를 찾을 수 없는 경우 에러 메시지 반환
return ("MHT 파일에서 유효한 HTML 내용을 찾을 수 없습니다.", None)
def update_pattern_fields(choice):
# 'Custom'을 선택하면 시작/종료 패턴 필드를 보이게 함
if choice == "Custom":
return [
gr.update(visible=True), # start_pattern_input
gr.update(visible=True), # end_pattern_input
gr.update(visible=True), # start_occurrence_input
gr.update(visible=True), # end_occurrence_input
gr.update(visible=True), # start_find_button
gr.update(visible=True), # end_find_button
gr.update(visible=True), # start_pattern_message
gr.update(visible=True) # end_pattern_message
]
else:
return [
gr.update(visible=False), # start_pattern_input
gr.update(visible=False), # end_pattern_input
gr.update(visible=False), # start_occurrence_input
gr.update(visible=False), # end_occurrence_input
gr.update(visible=False), # start_find_button
gr.update(visible=False), # end_find_button
gr.update(visible=False), # start_pattern_message
gr.update(visible=False) # end_pattern_message
]
def find_start_pattern(mht_content, pattern, occurrence):
if not pattern:
return "시작 패턴을 입력해주세요.", None
matches = list(re.finditer(re.escape(pattern), mht_content))
if matches:
total_occurrences = len(matches)
try:
index = int(occurrence) - 1
if index >= 0 and index < total_occurrences:
match = matches[index]
# 매치된 부분의 주변 텍스트를 추출
start = max(0, match.start() - 50)
end = min(len(mht_content), match.end() + 50)
context = mht_content[start:end]
message = f"시작 패턴의 전체 발생 횟수: {total_occurrences}\n선택된 발생번호 주변 텍스트:\n{context}"
return message, str(total_occurrences)
else:
return "올바르지 않은 발생 번호입니다.", str(total_occurrences)
except ValueError:
return "발생 번호는 숫자여야 합니다.", str(len(matches))
else:
return "시작 패턴을 MHT 내용에서 찾을 수 없습니다.", "0"
def find_end_pattern(mht_content, pattern, occurrence):
if not pattern:
return "종료 패턴을 입력해주세요.", None
matches = list(re.finditer(re.escape(pattern), mht_content))
if matches:
total_occurrences = len(matches)
try:
index = int(occurrence) - 1
if index >= 0 and index < total_occurrences:
match = matches[index]
# 매치된 부분의 주변 텍스트를 추출
start = max(0, match.start() - 50)
end = min(len(mht_content), match.end() + 50)
context = mht_content[start:end]
message = f"종료 패턴의 전체 발생 횟수: {total_occurrences}\n선택된 발생번호 주변 텍스트:\n{context}"
return message, str(total_occurrences)
else:
return "올바르지 않은 발생 번호입니다.", str(total_occurrences)
except ValueError:
return "발생 번호는 숫자여야 합니다.", str(len(matches))
else:
return "종료 패턴을 MHT 내용에서 찾을 수 없습니다.", "0"
with gr.Blocks() as block:
gr.Markdown("""
<h4>이 앱은 기본적으로는 MHT 내용에서 추출할 HTML 태그 패턴을 선택할 수 있도록 도와줍니다.</h4>
<p>기본 패턴이 아닌 다른 것을 원한다면(— 다른 문서도 가능할 수 있음), 'Custom'을 선택하고 시작 및 종료 패턴을 직접 입력할 수 있습니다.</p>
<p>HTML 태그와 포함된 내용 등이 복잡한 경우, 정확한 추출을 위해 고유하고 구별되는 시작 및 종료 패턴을 제공해야 합니다.</p>
""")
mht_input = gr.Textbox(label="MHT 내용을 여기에 붙여넣기", lines=20, placeholder="MHT 파일의 내용을 여기에 붙여넣으세요")
# 드롭다운에서 구체적인 시작 태그를 선택하게 수정
choice_dropdown = gr.Dropdown(
label="HTML 시작 태그 선택",
choices=["<!DOCTYPE html><html", "<!DOCTYPE html><html lang=","Custom"],
value="<!DOCTYPE html><html"
)
# 사용자 지정 시작 및 종료 패턴 입력 창과 추가 컴포넌트들
start_pattern_input = gr.Textbox(label="사용자 지정 시작 패턴", visible=False)
start_occurrence_input = gr.Textbox(label="시작 패턴 발생 번호", value="1", visible=False)
start_find_button = gr.Button("찾아보기", visible=False)
start_pattern_message = gr.Textbox(label="시작 패턴 찾기 결과", interactive=False, visible=False)
end_pattern_input = gr.Textbox(label="사용자 지정 종료 패턴", visible=False)
end_occurrence_input = gr.Textbox(label="종료 패턴 발생 번호", value="1", visible=False)
end_find_button = gr.Button("찾아보기", visible=False)
end_pattern_message = gr.Textbox(label="종료 패턴 찾기 결과", interactive=False, visible=False)
occurrence_input = gr.Textbox(label="추출할 발생 번호 입력 (숫자): 같은 것이 여럿 있을 때 ?번째", value="1")
convert_button = gr.Button("MHT에서 HTML 추출")
error_output = gr.Textbox(label="에러 메시지", interactive=False)
download_output = gr.File(label="HTML 파일 다운로드")
# 사용자의 선택에 따라 사용자 지정 패턴 필드의 가시성 업데이트
choice_dropdown.change(
fn=update_pattern_fields,
inputs=choice_dropdown,
outputs=[
start_pattern_input,
end_pattern_input,
start_occurrence_input,
end_occurrence_input,
start_find_button,
end_find_button,
start_pattern_message,
end_pattern_message
]
)
# 시작 패턴 찾기 버튼 클릭 이벤트 정의
start_find_button.click(
fn=find_start_pattern,
inputs=[mht_input, start_pattern_input, start_occurrence_input],
outputs=[start_pattern_message, start_occurrence_input]
)
# 종료 패턴 찾기 버튼 클릭 이벤트 정의
end_find_button.click(
fn=find_end_pattern,
inputs=[mht_input, end_pattern_input, end_occurrence_input],
outputs=[end_pattern_message, end_occurrence_input]
)
# 버튼 클릭 이벤트 정의
convert_button.click(
fn=extract_html_from_mht,
inputs=[mht_input, choice_dropdown, start_pattern_input, end_pattern_input, occurrence_input],
outputs=[error_output, download_output]
)
block.launch()
이렇게 수정된 코드는 다음과 같은 기능을 제공합니다.
Custom을 선택하면 시작 패턴과 종료 패턴 입력 상자, 발생 번호 입력 상자, 그리고 찾아보기 버튼이 나타납니다.
찾아보기 버튼을 클릭하면 입력된 패턴이 MHT 내용에서 몇 번째로 나타나는지 찾고, 해당 위치 주변의 텍스트를 보여줍니다.
발생 번호를 조정하여 원하는 위치의 패턴을 찾을 수 있으며, 이를 이용하여 정확한 HTML 부분을 추출할 수 있습니다.
주의사항:
패턴을 입력할 때 특수 문자가 포함되어 있다면 정확한 매칭을 위해 이스케이프 처리가 필요합니다.
발생 번호는 1 이상의 숫자여야 합니다.
MHT 내용이 매우 클 경우 응답이 지연될 수 있습니다.
이 코드를 사용하여 원하는 기능을 구현하시기 바랍니다. 추가적인 수정이나 문제가 발생하면 알려주세요.