import gradio as gr import requests from bs4 import BeautifulSoup def get_url_content(url): response = requests.get(url) if response.status_code == 200: return response.text else: return "URL에서 콘텐츠를 가져오는 데 실패했습니다." def parse_html(html_content): soup = BeautifulSoup(html_content, 'html.parser') return soup.prettify() def gradio_fetch_and_parse(url): html_content = get_url_content(url) parsed_content = parse_html(html_content) return parsed_content def get_main_content(html_content): soup = BeautifulSoup(html_content, 'html.parser') # 다양한 요소에서 텍스트 추출 extracted_texts = [] # extracted_texts.extend([item.get_text(strip=True) for item in soup.find_all('span', class_='a-list-item')]) # extracted_texts.extend([meta.get('content', '') for meta in soup.find_all('meta') if meta.get('content')]) # 추가 요소들 for tag in ['section', 'article', 'div', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'footer', 'aside', 'iframe']: extracted_texts.extend([item.get_text(strip=True) for item in soup.find_all(tag)]) combined_text = ' '.join(extracted_texts) if combined_text.strip(): print("추출된 텍스트:", combined_text) return combined_text else: print("본문 콘텐츠를 찾을 수 없습니다.") return '' def format_script(text): sentences = text.split('.') script = "" for i in range(0, min(len(sentences), 10), 2): line = sentences[i].strip() + '. ' if i+1 < len(sentences): line += sentences[i+1].strip() + '\n' script += line print("현재 스크립트:", script) return script def gradio_fetch_and_format_script(url): print("함수 호출됨:", url) html_content = get_url_content(url) main_content = get_main_content(html_content) print("추출된 본문:", main_content) script = format_script(main_content) print("생성된 스크립트:", script) return script iface_html = gr.Interface(fn=gradio_fetch_and_parse, inputs=gr.Textbox(label="URL을 입력하세요"), outputs=gr.Textbox(label="스크랩된 HTML 콘텐츠")) iface_script = gr.Interface(fn=gradio_fetch_and_format_script, inputs=gr.Textbox(label="URL을 입력하세요"), outputs=gr.Textbox(label="영상용 스크립트")) iface_combined = gr.TabbedInterface([iface_html, iface_script], ["HTML 보기", "스크립트 생성"]) iface_combined.launch()