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
import re
|
| 6 |
+
import logging
|
| 7 |
+
import tempfile
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import mecab # pythonโmecabโko ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ
|
| 10 |
+
import os
|
| 11 |
+
import time
|
| 12 |
+
import hmac
|
| 13 |
+
import hashlib
|
| 14 |
+
import base64
|
| 15 |
+
|
| 16 |
+
# ๋๋ฒ๊น
(๋ก๊ทธ)์ฉ ํจ์
|
| 17 |
+
def debug_log(message: str):
|
| 18 |
+
print(f"[DEBUG] {message}")
|
| 19 |
+
|
| 20 |
+
# =============================================================================
|
| 21 |
+
# [๊ธฐ๋ณธ์ฝ๋]: ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ์์ ์ ๋ชฉ๊ณผ ๋ณธ๋ฌธ์ ์ถ์ถํ๋ ํจ์
|
| 22 |
+
# =============================================================================
|
| 23 |
+
def scrape_naver_blog(url: str) -> str:
|
| 24 |
+
debug_log("scrape_naver_blog ํจ์ ์์")
|
| 25 |
+
debug_log(f"์์ฒญ๋ฐ์ URL: {url}")
|
| 26 |
+
|
| 27 |
+
# ํค๋ ์ธํ
(ํฌ๋กค๋ง ์ฐจ๋จ ๋ฐฉ์ง)
|
| 28 |
+
headers = {
|
| 29 |
+
"User-Agent": (
|
| 30 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 31 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 32 |
+
"Chrome/96.0.4664.110 Safari/537.36"
|
| 33 |
+
)
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
# 1) ๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ๋ฉ์ธ ํ์ด์ง ์์ฒญ
|
| 38 |
+
response = requests.get(url, headers=headers)
|
| 39 |
+
debug_log("HTTP GET ์์ฒญ(๋ฉ์ธ ํ์ด์ง) ์๋ฃ")
|
| 40 |
+
|
| 41 |
+
if response.status_code != 200:
|
| 42 |
+
debug_log(f"์์ฒญ ์คํจ, ์ํ์ฝ๋: {response.status_code}")
|
| 43 |
+
return f"์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ํ์ฝ๋: {response.status_code}"
|
| 44 |
+
|
| 45 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 46 |
+
debug_log("HTML ํ์ฑ(๋ฉ์ธ ํ์ด์ง) ์๋ฃ")
|
| 47 |
+
|
| 48 |
+
# 2) iframe ํ๊ทธ ์ฐพ๊ธฐ
|
| 49 |
+
iframe = soup.select_one("iframe#mainFrame")
|
| 50 |
+
if not iframe:
|
| 51 |
+
debug_log("iframe#mainFrame ํ๊ทธ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.")
|
| 52 |
+
return "๋ณธ๋ฌธ iframe์ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 53 |
+
|
| 54 |
+
iframe_src = iframe.get("src")
|
| 55 |
+
if not iframe_src:
|
| 56 |
+
debug_log("iframe src๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.")
|
| 57 |
+
return "๋ณธ๋ฌธ iframe์ src๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 58 |
+
|
| 59 |
+
# 3) iframe src๊ฐ ์๋๊ฒฝ๋ก์ธ ๊ฒฝ์ฐ ์ ๋๊ฒฝ๋ก๋ก ๋ณด์
|
| 60 |
+
parsed_iframe_url = urllib.parse.urljoin(url, iframe_src)
|
| 61 |
+
debug_log(f"iframe ํ์ด์ง ์์ฒญ URL: {parsed_iframe_url}")
|
| 62 |
+
|
| 63 |
+
# 4) iframe ํ์ด์ง ์ฌ์์ฒญ
|
| 64 |
+
iframe_response = requests.get(parsed_iframe_url, headers=headers)
|
| 65 |
+
debug_log("HTTP GET ์์ฒญ(iframe ํ์ด์ง) ์๋ฃ")
|
| 66 |
+
|
| 67 |
+
if iframe_response.status_code != 200:
|
| 68 |
+
debug_log(f"iframe ์์ฒญ ์คํจ, ์ํ์ฝ๋: {iframe_response.status_code}")
|
| 69 |
+
return f"iframe์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ํ์ฝ๋: {iframe_response.status_code}"
|
| 70 |
+
|
| 71 |
+
iframe_soup = BeautifulSoup(iframe_response.text, "html.parser")
|
| 72 |
+
debug_log("HTML ํ์ฑ(iframe ํ์ด์ง) ์๋ฃ")
|
| 73 |
+
|
| 74 |
+
# ์ ๋ชฉ ์ถ์ถ
|
| 75 |
+
title_div = iframe_soup.select_one('.se-module.se-module-text.se-title-text')
|
| 76 |
+
title = title_div.get_text(strip=True) if title_div else "์ ๋ชฉ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 77 |
+
debug_log(f"์ถ์ถ๋ ์ ๋ชฉ: {title}")
|
| 78 |
+
|
| 79 |
+
# ๋ณธ๋ฌธ ์ถ์ถ
|
| 80 |
+
content_div = iframe_soup.select_one('.se-main-container')
|
| 81 |
+
if content_div:
|
| 82 |
+
content = content_div.get_text("\n", strip=True)
|
| 83 |
+
else:
|
| 84 |
+
content = "๋ณธ๋ฌธ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 85 |
+
debug_log("๋ณธ๋ฌธ ์ถ์ถ ์๋ฃ")
|
| 86 |
+
|
| 87 |
+
# ๊ฒฐ๊ณผ ํฉ์น๊ธฐ
|
| 88 |
+
result = f"[์ ๋ชฉ]\n{title}\n\n[๋ณธ๋ฌธ]\n{content}"
|
| 89 |
+
debug_log("์ ๋ชฉ๊ณผ ๋ณธ๋ฌธ์ ํฉ์ณ ๋ฐํ ์ค๋น ์๋ฃ")
|
| 90 |
+
return result
|
| 91 |
+
|
| 92 |
+
except Exception as e:
|
| 93 |
+
debug_log(f"์๋ฌ ๋ฐ์: {str(e)}")
|
| 94 |
+
return f"์คํฌ๋ํ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
| 95 |
+
|
| 96 |
+
# =============================================================================
|
| 97 |
+
# [์ฐธ์กฐ์ฝ๋-1]: ํํ์ ๋ถ์ ํจ์ (Mecab ์ด์ฉ)
|
| 98 |
+
# =============================================================================
|
| 99 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 100 |
+
logger = logging.getLogger(__name__)
|
| 101 |
+
|
| 102 |
+
def analyze_text(text: str):
|
| 103 |
+
logger.debug("์๋ณธ ํ
์คํธ: %s", text)
|
| 104 |
+
|
| 105 |
+
# 1. ํ๊ตญ์ด๋ง ๋จ๊ธฐ๊ธฐ (๊ณต๋ฐฑ, ์์ด, ๊ธฐํธ ๋ฑ ์ ๊ฑฐ)
|
| 106 |
+
filtered_text = re.sub(r'[^๊ฐ-ํฃ]', '', text)
|
| 107 |
+
logger.debug("ํํฐ๋ง๋ ํ
์คํธ (ํ๊ตญ์ด๋ง, ๊ณต๋ฐฑ ์ ๊ฑฐ): %s", filtered_text)
|
| 108 |
+
|
| 109 |
+
if not filtered_text:
|
| 110 |
+
logger.debug("์ ํจํ ํ๊ตญ์ด ํ
์คํธ๊ฐ ์์.")
|
| 111 |
+
return pd.DataFrame(columns=["๋จ์ด", "๋น๋์"]), ""
|
| 112 |
+
|
| 113 |
+
# 2. Mecab์ ์ด์ฉํ ํํ์ ๋ถ์ (๋ช
์ฌ์ ๋ณตํฉ๋ช
์ฌ๋ง ์ถ์ถ)
|
| 114 |
+
mecab_instance = mecab.MeCab() # ์ธ์คํด์ค ์์ฑ
|
| 115 |
+
tokens = mecab_instance.pos(filtered_text)
|
| 116 |
+
logger.debug("ํํ์ ๋ถ์ ๊ฒฐ๊ณผ: %s", tokens)
|
| 117 |
+
|
| 118 |
+
freq = {}
|
| 119 |
+
for word, pos in tokens:
|
| 120 |
+
if word and word.strip():
|
| 121 |
+
if pos.startswith("NN"):
|
| 122 |
+
freq[word] = freq.get(word, 0) + 1
|
| 123 |
+
logger.debug("๋จ์ด: %s, ํ์ฌ: %s, ํ์ฌ ๋น๋: %d", word, pos, freq[word])
|
| 124 |
+
|
| 125 |
+
# 3. ๋น๋์๋ฅผ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
|
| 126 |
+
sorted_freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
|
| 127 |
+
logger.debug("๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ๋ ๋จ์ด ๋น๋: %s", sorted_freq)
|
| 128 |
+
|
| 129 |
+
# 4. ๊ฒฐ๊ณผ DataFrame ์์ฑ
|
| 130 |
+
df = pd.DataFrame(sorted_freq, columns=["๋จ์ด", "๋น๋์"])
|
| 131 |
+
logger.debug("๊ฒฐ๊ณผ DataFrame ์์ฑ๋จ, shape: %s", df.shape)
|
| 132 |
+
|
| 133 |
+
# 5. Excel ํ์ผ ์์ฑ (์์ ํ์ผ ์ ์ฅ)
|
| 134 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
|
| 135 |
+
df.to_excel(temp_file.name, index=False, engine='openpyxl')
|
| 136 |
+
temp_file.close()
|
| 137 |
+
logger.debug("Excel ํ์ผ ์์ฑ๋จ: %s", temp_file.name)
|
| 138 |
+
|
| 139 |
+
return df, temp_file.name
|
| 140 |
+
|
| 141 |
+
# =============================================================================
|
| 142 |
+
# [์ฐธ์กฐ์ฝ๋-2]: ํค์๋ ๊ฒ์๋ ๋ฐ ๋ธ๋ก๊ทธ ๋ฌธ์์ ์กฐํ ๊ด๋ จ ํจ์
|
| 143 |
+
# =============================================================================
|
| 144 |
+
def generate_signature(timestamp, method, uri, secret_key):
|
| 145 |
+
message = f"{timestamp}.{method}.{uri}"
|
| 146 |
+
digest = hmac.new(secret_key.encode("utf-8"), message.encode("utf-8"), hashlib.sha256).digest()
|
| 147 |
+
return base64.b64encode(digest).decode()
|
| 148 |
+
|
| 149 |
+
def get_header(method, uri, api_key, secret_key, customer_id):
|
| 150 |
+
timestamp = str(round(time.time() * 1000))
|
| 151 |
+
signature = generate_signature(timestamp, method, uri, secret_key)
|
| 152 |
+
return {
|
| 153 |
+
"Content-Type": "application/json; charset=UTF-8",
|
| 154 |
+
"X-Timestamp": timestamp,
|
| 155 |
+
"X-API-KEY": api_key,
|
| 156 |
+
"X-Customer": str(customer_id),
|
| 157 |
+
"X-Signature": signature
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
def fetch_related_keywords(keyword):
|
| 161 |
+
API_KEY = os.environ["NAVER_API_KEY"]
|
| 162 |
+
SECRET_KEY = os.environ["NAVER_SECRET_KEY"]
|
| 163 |
+
CUSTOMER_ID = os.environ["NAVER_CUSTOMER_ID"]
|
| 164 |
+
|
| 165 |
+
BASE_URL = "https://api.naver.com"
|
| 166 |
+
uri = "/keywordstool"
|
| 167 |
+
method = "GET"
|
| 168 |
+
headers = get_header(method, uri, API_KEY, SECRET_KEY, CUSTOMER_ID)
|
| 169 |
+
params = {
|
| 170 |
+
"hintKeywords": [keyword],
|
| 171 |
+
"showDetail": "1"
|
| 172 |
+
}
|
| 173 |
+
response = requests.get(BASE_URL + uri, params=params, headers=headers)
|
| 174 |
+
data = response.json()
|
| 175 |
+
if "keywordList" not in data:
|
| 176 |
+
return pd.DataFrame()
|
| 177 |
+
df = pd.DataFrame(data["keywordList"])
|
| 178 |
+
if len(df) > 100:
|
| 179 |
+
df = df.head(100)
|
| 180 |
+
|
| 181 |
+
def parse_count(x):
|
| 182 |
+
try:
|
| 183 |
+
return int(str(x).replace(",", ""))
|
| 184 |
+
except:
|
| 185 |
+
return 0
|
| 186 |
+
|
| 187 |
+
df["PC์๊ฒ์๋"] = df["monthlyPcQcCnt"].apply(parse_count)
|
| 188 |
+
df["๋ชจ๋ฐ์ผ์๊ฒ์๋"] = df["monthlyMobileQcCnt"].apply(parse_count)
|
| 189 |
+
df["ํ ํ์๊ฒ์๋"] = df["PC์๊ฒ์๋"] + df["๋ชจ๋ฐ์ผ์๊ฒ์๋"]
|
| 190 |
+
df.rename(columns={"relKeyword": "์ ๋ณดํค์๋"}, inplace=True)
|
| 191 |
+
result_df = df[["์ ๋ณดํค์๋", "PC์๊ฒ์๋", "๋ชจ๋ฐ์ผ์๊ฒ์๋", "ํ ํ์๊ฒ์๋"]]
|
| 192 |
+
return result_df
|
| 193 |
+
|
| 194 |
+
def fetch_blog_count(keyword):
|
| 195 |
+
client_id = os.environ["NAVER_SEARCH_CLIENT_ID"]
|
| 196 |
+
client_secret = os.environ["NAVER_SEARCH_CLIENT_SECRET"]
|
| 197 |
+
url = "https://openapi.naver.com/v1/search/blog.json"
|
| 198 |
+
headers = {
|
| 199 |
+
"X-Naver-Client-Id": client_id,
|
| 200 |
+
"X-Naver-Client-Secret": client_secret
|
| 201 |
+
}
|
| 202 |
+
params = {"query": keyword, "display": 1}
|
| 203 |
+
response = requests.get(url, headers=headers, params=params)
|
| 204 |
+
if response.status_code == 200:
|
| 205 |
+
data = response.json()
|
| 206 |
+
return data.get("total", 0)
|
| 207 |
+
else:
|
| 208 |
+
return 0
|
| 209 |
+
|
| 210 |
+
def create_excel_file(df):
|
| 211 |
+
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
| 212 |
+
excel_path = tmp.name
|
| 213 |
+
df.to_excel(excel_path, index=False)
|
| 214 |
+
return excel_path
|
| 215 |
+
|
| 216 |
+
def process_keyword(keywords: str, include_related: bool):
|
| 217 |
+
"""
|
| 218 |
+
์ฌ๋ฌ ํค์๋๋ฅผ ์ํฐ๋ก ๊ตฌ๋ถํ์ฌ ๋ฆฌ์คํธ๋ก ๋ง๋ค๊ณ ,
|
| 219 |
+
๊ฐ ํค์๋์ ๋ํด ๋ค์ด๋ฒ ๊ด๊ณ API๋ก ๊ฒ์๋ ์ ๋ณด๋ฅผ ์กฐํํ๋ฉฐ,
|
| 220 |
+
์ฒซ ๋ฒ์งธ ํค์๋์ ๊ฒฝ์ฐ ์ต์
์ ๋ฐ๋ผ ์ฐ๊ด๊ฒ์์ด๋ ์ถ๊ฐํ ํ,
|
| 221 |
+
๊ฐ ์ ๋ณดํค์๋์ ๋ํด ๋ธ๋ก๊ทธ ๋ฌธ์์๋ฅผ ์กฐํํ์ฌ DataFrame๊ณผ Excel ํ์ผ์ ๋ฐํํฉ๋๋ค.
|
| 222 |
+
"""
|
| 223 |
+
input_keywords = [k.strip() for k in keywords.splitlines() if k.strip()]
|
| 224 |
+
result_dfs = []
|
| 225 |
+
|
| 226 |
+
for idx, kw in enumerate(input_keywords):
|
| 227 |
+
df_kw = fetch_related_keywords(kw)
|
| 228 |
+
if df_kw.empty:
|
| 229 |
+
continue
|
| 230 |
+
row_kw = df_kw[df_kw["์ ๋ณดํค์๋"] == kw]
|
| 231 |
+
if not row_kw.empty:
|
| 232 |
+
result_dfs.append(row_kw)
|
| 233 |
+
else:
|
| 234 |
+
result_dfs.append(df_kw.head(1))
|
| 235 |
+
if include_related and idx == 0:
|
| 236 |
+
df_related = df_kw[df_kw["์ ๋ณดํค์๋"] != kw]
|
| 237 |
+
if not df_related.empty:
|
| 238 |
+
result_dfs.append(df_related)
|
| 239 |
+
|
| 240 |
+
if result_dfs:
|
| 241 |
+
result_df = pd.concat(result_dfs, ignore_index=True)
|
| 242 |
+
result_df.drop_duplicates(subset=["์ ๋ณดํค์๋"], inplace=True)
|
| 243 |
+
else:
|
| 244 |
+
result_df = pd.DataFrame(columns=["์ ๋ณดํค์๋", "PC์๊ฒ์๋", "๋ชจ๋ฐ์ผ์๊ฒ์๋", "ํ ํ์๊ฒ์๋"])
|
| 245 |
+
|
| 246 |
+
result_df["๋ธ๋ก๊ทธ๋ฌธ์์"] = result_df["์ ๋ณดํค์๋"].apply(fetch_blog_count)
|
| 247 |
+
result_df.sort_values(by="ํ ํ์๊ฒ์๋", ascending=False, inplace=True)
|
| 248 |
+
|
| 249 |
+
return result_df, create_excel_file(result_df)
|
| 250 |
+
|
| 251 |
+
# =============================================================================
|
| 252 |
+
# ํตํฉ ์ฒ๋ฆฌ ํจ์: ๋ธ๋ก๊ทธ ๋ด์ฉ(ํ
์คํธ)์ ๋ํด ํํ์ ๋ถ์์ ์ํํ ํ,
|
| 253 |
+
# ํค์๋์ ๊ฒ์๋ ๋ฐ ๋ธ๋ก๊ทธ ๋ฌธ์์๋ฅผ ์ถ๊ฐํ์ฌ ์ต์ข
๊ฒฐ๊ณผ๋ฅผ ๋ฐํํจ.
|
| 254 |
+
# =============================================================================
|
| 255 |
+
def process_blog_content(text: str):
|
| 256 |
+
debug_log("process_blog_content ํจ์ ์์")
|
| 257 |
+
# 1. ํํ์ ๋ถ์ ์คํ ([์ฐธ์กฐ์ฝ๋-1] ํ์ฉ)
|
| 258 |
+
df_morph, morph_excel = analyze_text(text)
|
| 259 |
+
debug_log("ํํ์ ๋ถ์ ์๋ฃ")
|
| 260 |
+
|
| 261 |
+
if df_morph.empty:
|
| 262 |
+
debug_log("ํํ์ ๋ถ์ ๊ฒฐ๊ณผ๊ฐ ๋น์ด์์")
|
| 263 |
+
return df_morph, ""
|
| 264 |
+
|
| 265 |
+
# 2. ํํ์ ๋ถ์๋ ๋จ์ด ๋ชฉ๋ก ์ถ์ถ (ํค์๋ ์กฐํ์ฉ)
|
| 266 |
+
keywords = "\n".join(df_morph["๋จ์ด"].tolist())
|
| 267 |
+
debug_log(f"์ถ์ถ๋ ๋จ์ด ๋ชฉ๋ก: {keywords}")
|
| 268 |
+
|
| 269 |
+
# 3. ํค์๋ ๊ฒ์๋ ๋ฐ ๋ธ๋ก๊ทธ ๋ฌธ์์ ์กฐํ ([์ฐธ์กฐ์ฝ๋-2] ํ์ฉ)
|
| 270 |
+
df_keyword, keyword_excel = process_keyword(keywords, include_related=False)
|
| 271 |
+
debug_log("ํค์๋ ๊ฒ์ ์ ๋ณด ์กฐํ ์๋ฃ")
|
| 272 |
+
|
| 273 |
+
# 4. ํํ์ ๋ถ์ ๊ฒฐ๊ณผ์ ํค์๋ ์ ๋ณด๋ฅผ ๋จ์ด ๊ธฐ์ค์ผ๋ก ๋ณํฉ
|
| 274 |
+
df_merged = pd.merge(df_morph, df_keyword, left_on="๋จ์ด", right_on="์ ๋ณดํค์๋", how="left")
|
| 275 |
+
debug_log("๋ฐ์ดํฐ ๋ณํฉ ์๋ฃ")
|
| 276 |
+
df_merged.drop(columns=["์ ๋ณดํค์๋"], inplace=True)
|
| 277 |
+
|
| 278 |
+
# 5. ๋ณํฉ ๊ฒฐ๊ณผ๋ฅผ Excel ํ์ผ๋ก ์์ฑ
|
| 279 |
+
merged_excel = create_excel_file(df_merged)
|
| 280 |
+
debug_log(f"๋ณํฉ ๊ฒฐ๊ณผ Excel ํ์ผ ์์ฑ๋จ: {merged_excel}")
|
| 281 |
+
|
| 282 |
+
return df_merged, merged_excel
|
| 283 |
+
|
| 284 |
+
# =============================================================================
|
| 285 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ (ํ๊น
ํ์ด์ค ๊ทธ๋ผ๋์ค ํ๊ฒฝ)
|
| 286 |
+
# =============================================================================
|
| 287 |
+
with gr.Blocks() as demo:
|
| 288 |
+
gr.Markdown("# ๋ธ๋ก๊ทธ ๊ธ ํํ์ ๋ถ์ ๋ฐ ํค์๋ ์ ๋ณด ์กฐํ")
|
| 289 |
+
|
| 290 |
+
with gr.Tab("๋ธ๋ก๊ทธ ๋ด์ฉ ์
๋ ฅ ๋ฐ ์คํฌ๋ํ"):
|
| 291 |
+
with gr.Row():
|
| 292 |
+
blog_url = gr.Textbox(label="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ๋งํฌ", placeholder="์: https://blog.naver.com/ssboost/222983068507")
|
| 293 |
+
fetch_button = gr.Button("๋ธ๋ก๊ทธ๋ด์ฉ๊ฐ์ ธ์ค๊ธฐ")
|
| 294 |
+
blog_content = gr.Textbox(label="๋ธ๋ก๊ทธ ๋ด์ฉ (์ ๋ชฉ ๋ฐ ๋ณธ๋ฌธ)", lines=10, placeholder="๋ธ๋ก๊ทธ ๋ด์ฉ์ ๊ฐ์ ธ์ค๊ฑฐ๋ ์ง์ ์
๋ ฅํ์ธ์.")
|
| 295 |
+
# '๋ธ๋ก๊ทธ๋ด์ฉ๊ฐ์ ธ์ค๊ธฐ' ๋ฒํผ ํด๋ฆญ ์ ์คํฌ๋ํ ์คํํ์ฌ blog_content์ ๋ฐ์
|
| 296 |
+
fetch_button.click(fn=scrape_naver_blog, inputs=blog_url, outputs=blog_content)
|
| 297 |
+
|
| 298 |
+
with gr.Tab("ํํ์ ๋ถ์ ์คํ"):
|
| 299 |
+
with gr.Row():
|
| 300 |
+
analysis_button = gr.Button("ํํ์๋ถ์")
|
| 301 |
+
# ๋ถ์ ๊ฒฐ๊ณผ๋ ์์ ๊ฐ๋ฅํ๋๋ก interactive=True ์ค์
|
| 302 |
+
output_table = gr.Dataframe(label="๋ถ์ ๊ฒฐ๊ณผ (ํํ์ ๋ฐ ํค์๋ ์ ๋ณด)", interactive=True)
|
| 303 |
+
output_file = gr.File(label="Excel ๋ค์ด๋ก๋")
|
| 304 |
+
# 'ํํ์๋ถ์' ๋ฒํผ ํด๋ฆญ ์ process_blog_content ํจ์ ์คํ
|
| 305 |
+
analysis_button.click(fn=process_blog_content, inputs=blog_content, outputs=[output_table, output_file])
|
| 306 |
+
|
| 307 |
+
if __name__ == "__main__":
|
| 308 |
+
debug_log("Gradio ์ฑ ์คํ ์์")
|
| 309 |
+
demo.launch()
|
| 310 |
+
debug_log("Gradio ์ฑ ์คํ ์ข
๋ฃ")
|