Download app.py from bluskyyy/AAAAAAA: direct link, hf CLI and curl.
- Browser
- Download file 28.9 kB
-
https://huggingface.co/spaces/bluskyyy/AAAAAAA/resolve/main/app.py
- Command line
-
hf download hf://spaces/bluskyyy/AAAAAAA/app.py
-
curl -L -o app.py https://huggingface.co/spaces/bluskyyy/AAAAAAA/resolve/main/app.py
28.9 kB
| import re | |
| import pandas as pd | |
| import io | |
| import tempfile | |
| import os | |
| from collections import Counter, defaultdict | |
| import gradio as gr | |
| import requests | |
| import time | |
| import hmac | |
| import hashlib | |
| import base64 | |
| from concurrent.futures import ThreadPoolExecutor, as_completed | |
| import threading | |
| # ๋ค์ด๋ฒ API ๊ด๋ จ ํจ์๋ค | |
| def generate_signature(timestamp, method, uri, secret_key): | |
| message = f"{timestamp}.{method}.{uri}" | |
| digest = hmac.new(secret_key.encode("utf-8"), message.encode("utf-8"), hashlib.sha256).digest() | |
| return base64.b64encode(digest).decode() | |
| def get_header(method, uri, api_key, secret_key, customer_id): | |
| timestamp = str(round(time.time() * 1000)) | |
| signature = generate_signature(timestamp, method, uri, secret_key) | |
| return { | |
| "Content-Type": "application/json; charset=UTF-8", | |
| "X-Timestamp": timestamp, | |
| "X-API-KEY": api_key, | |
| "X-Customer": str(customer_id), | |
| "X-Signature": signature | |
| } | |
| def fetch_search_volume_batch(keywords_batch): | |
| """ํค์๋ ๋ฐฐ์น์ ๋ํ ๋ค์ด๋ฒ ๊ฒ์๋ ์กฐํ""" | |
| try: | |
| # ํ๊ฒฝ ๋ณ์์์ API ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ | |
| API_KEY = os.environ.get("NAVER_API_KEY") | |
| SECRET_KEY = os.environ.get("NAVER_SECRET_KEY") | |
| CUSTOMER_ID = os.environ.get("NAVER_CUSTOMER_ID") | |
| if not all([API_KEY, SECRET_KEY, CUSTOMER_ID]): | |
| print("๋ค์ด๋ฒ API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.") | |
| return {} | |
| BASE_URL = "https://api.naver.com" | |
| uri = "/keywordstool" | |
| method = "POST" # POST ๋ฉ์๋๋ก ๋ณ๊ฒฝ | |
| headers = get_header(method, uri, API_KEY, SECRET_KEY, int(CUSTOMER_ID)) | |
| # JSON body๋ก ๋ฐ์ดํฐ ์ ์ก | |
| data = { | |
| "hintKeywords": keywords_batch, | |
| "showDetail": "1" | |
| } | |
| response = requests.post(BASE_URL + uri, json=data, headers=headers) | |
| if response.status_code != 200: | |
| print(f"๋ค์ด๋ฒ API ์๋ต ์ค๋ฅ: {response.status_code} - {response.text}") | |
| return {} | |
| data = response.json() | |
| # ๊ฒฐ๊ณผ ํ์ฑ | |
| result = {} | |
| if "keywordList" in data: | |
| for item in data["keywordList"]: | |
| keyword = item.get("relKeyword", "") | |
| pc_count = item.get("monthlyPcQcCnt", 0) | |
| mobile_count = item.get("monthlyMobileQcCnt", 0) | |
| # ๋ฌธ์์ด์ ์ซ์๋ก ๋ณํ | |
| try: | |
| pc_count = int(str(pc_count).replace(",", "")) if pc_count else 0 | |
| mobile_count = int(str(mobile_count).replace(",", "")) if mobile_count else 0 | |
| except: | |
| pc_count = mobile_count = 0 | |
| total_count = pc_count + mobile_count | |
| result[keyword] = { | |
| "PC๊ฒ์๋": pc_count, | |
| "๋ชจ๋ฐ์ผ๊ฒ์๋": mobile_count, | |
| "์ด๊ฒ์๋": total_count | |
| } | |
| return result | |
| except Exception as e: | |
| print(f"๋ค์ด๋ฒ API ํธ์ถ ์ค๋ฅ: {str(e)}") | |
| return {} | |
| def fetch_all_search_volumes(keywords, batch_size=5): # ๋ฐฐ์น ํฌ๊ธฐ๋ฅผ 5๋ก ์ค์ | |
| """ํค์๋ ๋ฆฌ์คํธ์ ๋ํ ๋ค์ด๋ฒ ๊ฒ์๋ ๋ณ๋ ฌ ์กฐํ""" | |
| results = {} | |
| batches = [] | |
| # ํค์๋๋ฅผ ๋ฐฐ์น๋ก ๋๋๊ธฐ | |
| for i in range(0, len(keywords), batch_size): | |
| batch = keywords[i:i + batch_size] | |
| batches.append(batch) | |
| print(f"์ด {len(batches)}๊ฐ ๋ฐฐ์น๋ก {len(keywords)}๊ฐ ํค์๋ ์ฒ๋ฆฌ ์ค...") | |
| # ๋ณ๋ ฌ ์ฒ๋ฆฌ | |
| with ThreadPoolExecutor(max_workers=3) as executor: # ์์ปค ์๋ 3์ผ๋ก ์ค์ | |
| future_to_batch = {executor.submit(fetch_search_volume_batch, batch): batch for batch in batches} | |
| for future in as_completed(future_to_batch): | |
| batch = future_to_batch[future] | |
| try: | |
| batch_results = future.result() | |
| results.update(batch_results) | |
| print(f"๋ฐฐ์น ์ฒ๋ฆฌ ์๋ฃ: {len(batch)}๊ฐ ํค์๋") | |
| except Exception as e: | |
| print(f"๋ฐฐ์น ์ฒ๋ฆฌ ์ค๋ฅ: {str(e)}") | |
| # API ๋ ์ดํธ ๋ฆฌ๋ฏธํธ ๋ฐฉ์ง๋ฅผ ์ํ ๋๋ ์ด | |
| time.sleep(1) # ๋๋ ์ด๋ฅผ 1์ด๋ก ์ฆ๊ฐ | |
| print(f"๊ฒ์๋ ์กฐํ ์๋ฃ: {len(results)}๊ฐ ํค์๋") | |
| return results | |
| def preprocess_text(text): | |
| """ | |
| ํ ์คํธ ์ ์ฒ๋ฆฌ ํจ์: | |
| 1. "*" ๋ฌธ์ ๊ฐ ๋์ค๋ฉด ์ ๊ฑฐ | |
| 2. " "์ด๋ฉด " "๋ก ๋ณ๊ฒฝ | |
| 3. ์คํ์ด์ค๊ฐ 1๊ฐ ์ด์์ด๋ฉด ๋ฌด์กฐ๊ฑด 2๊ฐ๋ฉด 1๊ฐ๋ก ์์ | |
| """ | |
| # 1. "*" ์ ๊ฑฐ | |
| processed = re.sub(r'\*', '', text) | |
| # 2 & 3. ์คํ์ด์ค๊ฐ 2๊ฐ ์ด์์ด๋ฉด 1๊ฐ๋ก ์์ | |
| processed = re.sub(r'\s{2,}', ' ', processed) | |
| return processed.strip() | |
| def extract_product_categories(text): | |
| """ | |
| ํ ์คํธ์์ ์ํ๋ช ๊ณผ ํด๋น ์นดํ ๊ณ ๋ฆฌ๋ฅผ ์ถ์ถํ๋ ํจ์ | |
| """ | |
| main_categories = [ | |
| '๊ฐ๊ตฌ/์ธํ ๋ฆฌ์ด', | |
| '์ํ/๊ฑด๊ฐ', | |
| 'ํจ์ ์กํ', | |
| '๋์งํธ/๊ฐ์ ', | |
| '์ถ์ฐ/์ก์', | |
| '์คํฌ์ธ /๋ ์ ', | |
| 'ํ์ฅํ/๋ฏธ์ฉ', | |
| 'ํจ์ ์๋ฅ', | |
| '์ฌ๊ฐ/์ํํธ์', | |
| '์ํ' | |
| ] | |
| lines = text.split('\n') | |
| # ์ํ๋ช ๊ณผ ์นดํ ๊ณ ๋ฆฌ๋ฅผ ๋งคํ | |
| product_categories = {} | |
| current_product = None | |
| for i, line in enumerate(lines): | |
| clean_line = preprocess_text(line) | |
| # ์ฐํ๊ธฐ๊ฐ ํฌํจ๋ ๋ผ์ธ์ด๋ฉด ์งํ์ ๋ผ์ธ์ด ์ํ๋ช | |
| if '์ฐํ๊ธฐ' in clean_line: | |
| # ์ด์ ์ํ ์ ์ฅ | |
| if current_product and current_category: | |
| product_categories[current_product] = current_category | |
| # ์๋ก์ด ์ํ ์์ | |
| if i + 1 < len(lines): | |
| current_product = preprocess_text(lines[i + 1]) | |
| current_category = None | |
| # ์นดํ ๊ณ ๋ฆฌ ๋ผ์ธ ์ฒดํฌ - ์ ์ฒด ๋ผ์ธ์ ๊ทธ๋๋ก ์ ์ฅ | |
| else: | |
| for main_cat in main_categories: | |
| if clean_line.startswith(main_cat): | |
| if current_product: | |
| current_category = clean_line # ์ ์ฒด ๋ผ์ธ์ ์ ์ฅ | |
| break | |
| # ๋ง์ง๋ง ์ํ ์ ์ฅ | |
| if current_product and current_category: | |
| product_categories[current_product] = current_category | |
| return product_categories | |
| def extract_titles(text, debug=False): | |
| """ | |
| ํ ์คํธ์์ ์ ํ ์ ๋ชฉ์ ์ถ์ถํ๋ ํจ์ | |
| """ | |
| # ์๋ณธ ์ค ์ ์ฅ (** ํจํด ๋ณด์กด) | |
| original_lines = text.split('\n') | |
| # ์ ์ฒ๋ฆฌ๋ ์ค ์ ์ฅ | |
| lines = [preprocess_text(line) for line in original_lines] | |
| # ์ ๋ชฉ์ด ์๋ ๊ฒ์ผ๋ก ๊ฐ์ฃผํ ๋ช ํํ ํจํด๋ค (์ ๊ทํํ์) | |
| exclude_patterns = [ | |
| r'^์ํ๋ง ๋ณด๊ธฐ$', | |
| r'^๊ตฌ๋งค์ ๋ณด$', | |
| r'^ํฌ์ธํธ ์ต๋.*$', | |
| r'^์ํ/๊ฑด๊ฐ.*$', | |
| r'^์ ๋ฆฝ์ฟ ํฐ$', | |
| r'^๋ณ์ .*$', | |
| r'^๋ฆฌ๋ทฐ.*$', | |
| r'^์ผํ๋ชฐ๋ณ.*$', | |
| r'^ํ์$', | |
| r'^์๋น์ค$', | |
| r'^๋น ํ์.*$', | |
| r'^๊ตฟ์๋น์ค$', | |
| r'^๋ฌด๋ฃ๊ตํ๋ฐํ$', | |
| r'^์ค๋์ถ๋ฐ$', | |
| r'^๋ค์ด๋ฒํ๋ฌ์ค.*$', | |
| r'^๋ฉค๋ฒ์ญ.*$', | |
| r'^์ ๊ณ ํ๊ธฐ$', | |
| r'^๋ฑ๋ก์ผ.*$', | |
| r'^ํกํก$', | |
| r'^.*ํฌ์ธํธ ์ ๋ฆฝ์์ $', | |
| r'^์์ฐ ๋ฉค๋ฒ์ญ.*$', | |
| ] | |
| # ์ ํ ๋ธ๋ก ๊ฒฝ๊ณ ์ฐพ๊ธฐ | |
| block_boundaries = [] | |
| for i in range(len(lines)): | |
| if '์ฐํ๊ธฐ' in lines[i]: | |
| block_boundaries.append(i) | |
| # ๋ง์ง๋ง ๋ธ๋ก ๋ ์ถ๊ฐ | |
| if len(block_boundaries) > 0: | |
| block_boundaries.append(len(lines)) | |
| else: | |
| # ์ฐํ๊ธฐ๊ฐ ์๋ ๊ฒฝ์ฐ, ์ ์ฒด ํ ์คํธ๋ฅผ ํ๋์ ๋ธ๋ก์ผ๋ก ์ฒ๋ฆฌ | |
| block_boundaries = [0, len(lines)] | |
| if debug: | |
| print(f"์ด {len(block_boundaries) - 1}๊ฐ์ ์ ํ ๋ธ๋ก ๋ฐ๊ฒฌ") | |
| # ์ต์ข ์ ๋ชฉ ๋ชฉ๋ก | |
| final_titles = [] | |
| # ๊ฐ ์ ํ ๋ธ๋ก๋ณ๋ก ์ฒ๋ฆฌ | |
| for b in range(len(block_boundaries) - 1): | |
| block_start = block_boundaries[b] | |
| block_end = block_boundaries[b + 1] | |
| # ํ์ฌ ๋ธ๋ก ๋ด์์๋ง ์ฌ์ฉํ ์ ๋ชฉ ํ๋ณด ๋ฆฌ์คํธ | |
| block_title_candidates = [] | |
| # ๋ธ๋ก ๋ด ๊ด๊ณ ์์น ํ์ | |
| ad_indices = [] | |
| for i in range(block_start, block_end): | |
| if '๊ด๊ณ ' in lines[i]: | |
| ad_indices.append(i) | |
| # ๋ธ๋ก ๋ด ๊ด๊ณ ์์น ๊ธฐ๋ฐ ๊ด๊ณ ๊ทผ์ฒ ํ์ (๋ธ๋ก ๊ฒฝ๊ณ ๋ด์์๋ง) | |
| has_ad_nearby = [False] * (block_end - block_start) | |
| for ad_idx in ad_indices: | |
| # ๊ด๊ณ ์ฃผ๋ณ 5์ค ๋ฒ์๋ฅผ ๋ธ๋ก ๋ด์์๋ง ๊ณ์ฐ | |
| for j in range(max(0, ad_idx - block_start - 5), min(block_end - block_start, ad_idx - block_start + 6)): | |
| has_ad_nearby[j] = True | |
| # ๋ธ๋ก์ ๊ด๊ณ ๊ฐ ์๋์ง ํ์ธ | |
| block_has_ad = len(ad_indices) > 0 | |
| # ๊ด๊ณ ๊ฐ ์๋ ๋ธ๋ก์ ์ ์ธํ์ง ์๊ณ , ๊ด๊ณ ์ฃผ๋ณ ํ ์คํธ๋ง ์ ์ธ | |
| if debug and block_has_ad: | |
| print(f"๋ธ๋ก {b+1} (๋ผ์ธ {block_start+1}-{block_end}): ๊ด๊ณ ํฌํจ") | |
| # ํจํด 1: ** ํจํด ํ์ธ | |
| for i in range(block_start, block_end): | |
| # ๋ธ๋ก ๋ด ์๋์ ์ธ๋ฑ์ค | |
| relative_idx = i - block_start | |
| # ๊ด๊ณ ์ฃผ๋ณ์ด๋ฉด ๊ฑด๋๋ฐ๊ธฐ | |
| if block_has_ad and has_ad_nearby[relative_idx]: | |
| continue | |
| original_line = original_lines[i] | |
| if '**' in original_line: | |
| matches = re.findall(r'\*\*(.*?)\*\*', original_line) | |
| for match in matches: | |
| clean_match = preprocess_text(match) | |
| # ์ ๋ชฉ์ผ๋ก ์ ํฉํ์ง ํ์ธ | |
| if is_valid_title(clean_match, exclude_patterns): | |
| # ํด์ธ ์ ๋์ฌ ์ ๊ฑฐ | |
| if clean_match.startswith('ํด์ธ'): | |
| clean_match = clean_match[2:] | |
| block_title_candidates.append(clean_match) | |
| # ํจํด 2: ๊ฐ๊ฒฉ ์ ๋ณด ๋ฐ๋ก ์ ์ค ํ์ธ | |
| for i in range(block_start, block_end - 1): | |
| # ๋ธ๋ก ๋ด ์๋์ ์ธ๋ฑ์ค | |
| relative_idx = i - block_start | |
| # ๊ด๊ณ ์ฃผ๋ณ์ด๋ฉด ๊ฑด๋๋ฐ๊ธฐ | |
| if block_has_ad and has_ad_nearby[relative_idx]: | |
| continue | |
| current_line = lines[i] | |
| next_line = lines[i + 1] | |
| # ๊ฐ๊ฒฉ ํจํด ํ์ธ | |
| price_pattern = re.search(r'\d+,?\d*์', next_line) or '์๋ฐฐ์ก๋น' in next_line | |
| if price_pattern and is_valid_title(current_line, exclude_patterns) and '์ฐํ๊ธฐ' not in current_line: | |
| # ํด์ธ ์ ๋์ฌ ์ ๊ฑฐ | |
| if current_line.startswith('ํด์ธ'): | |
| current_line = current_line[2:] | |
| block_title_candidates.append(current_line) | |
| # ํจํด 3: ์ฐํ๊ธฐ ๋ค์ ์ค ํ์ธ | |
| if block_start + 1 < block_end: | |
| # ๋ธ๋ก ๋ด ์๋์ ์ธ๋ฑ์ค | |
| relative_idx = 1 # ์ฒซ ์ค ๋ค์ | |
| # ๊ด๊ณ ์ฃผ๋ณ์ด ์๋ ๊ฒฝ์ฐ์๋ง ์ฒ๋ฆฌ | |
| if not (block_has_ad and has_ad_nearby[relative_idx]): | |
| next_line = lines[block_start + 1] | |
| if is_valid_title(next_line, exclude_patterns): | |
| # ํด์ธ ์ ๋์ฌ ์ ๊ฑฐ | |
| if next_line.startswith('ํด์ธ'): | |
| next_line = next_line[2:] | |
| block_title_candidates.append(next_line) | |
| # ํจํด 4: ์์๋๋ก 3์ค ๋ด์ ๋์ผํ ํ ์คํธ ํ์ธ (๊ท์น 4) | |
| for i in range(block_start, block_end): | |
| # ๋ธ๋ก ๋ด ์๋์ ์ธ๋ฑ์ค | |
| relative_idx = i - block_start | |
| # ๊ด๊ณ ์ฃผ๋ณ์ด๋ฉด ๊ฑด๋๋ฐ๊ธฐ | |
| if block_has_ad and has_ad_nearby[relative_idx]: | |
| continue | |
| current_line = lines[i] | |
| if not is_valid_title(current_line, exclude_patterns): | |
| continue | |
| # ์์๋๋ก 3์ค ๋ฒ์ ๋ด์ ๋์ผํ ํ ์คํธ ํ์ธ (๋ธ๋ก ๊ฒฝ๊ณ ๋ด์์) | |
| for j in range(max(block_start, i - 3), min(block_end, i + 4)): | |
| if i != j and lines[i] == lines[j]: | |
| # ํด์ธ ์ ๋์ฌ ์ ๊ฑฐ | |
| if current_line.startswith('ํด์ธ'): | |
| current_line = current_line[2:] | |
| block_title_candidates.append(current_line) | |
| break | |
| # ํ์ฌ ๋ธ๋ก์์ ๊ฐ์ฅ ์ ํฉํ ์ ๋ชฉ ์ ํ | |
| best_title = select_best_title(block_title_candidates) | |
| # ์ ํฉํ ์ ๋ชฉ์ ์ฐพ์์ผ๋ฉด ์ต์ข ๋ชฉ๋ก์ ์ถ๊ฐ | |
| if best_title: | |
| final_titles.append(best_title) | |
| if debug: | |
| print(f"๋ธ๋ก {b+1}์ ์ ๋ชฉ: {best_title}") | |
| else: | |
| # ์ ๋ชฉ์ ์ฐพ์ง ๋ชปํ ๊ฒฝ์ฐ, ๋ธ๋ก์ ์ฒซ ์ค ์ดํ 5์ค ๋ด์์ ์ฐพ๊ธฐ | |
| for i in range(block_start + 1, min(block_start + 6, block_end)): | |
| # ๋ธ๋ก ๋ด ์๋์ ์ธ๋ฑ์ค | |
| relative_idx = i - block_start | |
| # ๊ด๊ณ ์ฃผ๋ณ์ด๋ฉด ๊ฑด๋๋ฐ๊ธฐ | |
| if block_has_ad and has_ad_nearby[relative_idx]: | |
| continue | |
| current_line = lines[i] | |
| if is_valid_title(current_line, exclude_patterns): | |
| # ํด์ธ ์ ๋์ฌ ์ ๊ฑฐ | |
| if current_line.startswith('ํด์ธ'): | |
| current_line = current_line[2:] | |
| final_titles.append(current_line) | |
| if debug: | |
| print(f"๋ธ๋ก {b+1}์ ๋์ฒด ์ ๋ชฉ: {current_line}") | |
| break | |
| return final_titles | |
| def is_valid_title(text, exclude_patterns): | |
| """ | |
| ์ ๋ชฉ์ผ๋ก ์ ํฉํ์ง ํ์ธํ๋ ํจ์ | |
| * ํน์ ํค์๋ ์๊ตฌ ๋์ ๊ตฌ์กฐ์ ํจํด ๊ฒ์ฌ | |
| """ | |
| # ๊ธธ์ด ๊ฒ์ฌ (๋๋ฌด ์งง์ผ๋ฉด ์ ๋ชฉ์ด ์๋) | |
| if len(text) < 10: | |
| return False | |
| # ์ซ์๋ง์ผ๋ก ๊ตฌ์ฑ๋ ํ ์คํธ ์ ์ธ | |
| if re.match(r'^[\d,.\s]+$', text): | |
| return False | |
| # ์ ์ธ ํจํด ๊ฒ์ฌ | |
| for pattern in exclude_patterns: | |
| if re.match(pattern, text): | |
| return False | |
| return True | |
| def select_best_title(candidates): | |
| """ | |
| ์ฌ๋ฌ ์ ๋ชฉ ํ๋ณด ์ค ๊ฐ์ฅ ์ ํฉํ ๊ฒ์ ์ ํ | |
| """ | |
| if not candidates: | |
| return None | |
| # ์ ๋ชฉ ์ ์ ๊ณ์ฐ ํจ์ | |
| def score_title(title): | |
| score = 0 | |
| # ๊ธธ์ด๊ฐ ์ ์ ํ ์ ๋ชฉ ์ ํธ (10-50์) | |
| if 10 <= len(title) <= 50: | |
| score += 3 | |
| elif 50 < len(title) <= 100: | |
| score += 2 | |
| else: | |
| score += 1 | |
| # ํน์ ํจํด์ด ์ ๋ชฉ์ ํฌํจ๋์ด ์์ผ๋ฉด ์ ์ ์ถ๊ฐ | |
| if re.search(r'[A-Za-z0-9\-]+', title): # ๋ชจ๋ธ๋ฒํธ ํฌํจ ๊ฐ๋ฅ์ฑ | |
| score += 2 | |
| # ๋ธ๋๋๋ช ์ด ์์ ์ค๋ ๊ฒฝ์ฐ ์ ์ ์ถ๊ฐ | |
| if re.match(r'^[๊ฐ-ํฃA-Za-z]+\s', title): | |
| score += 1 | |
| return score | |
| # ์ ์ ๊ธฐ๋ฐ์ผ๋ก ์ ๋ ฌ | |
| scored_candidates = [(score_title(title), title) for title in candidates] | |
| scored_candidates.sort(reverse=True) | |
| return scored_candidates[0][1] if scored_candidates else None | |
| def extract_keywords(titles): | |
| """ | |
| ์ ๋ชฉ์์ ์๋ฏธ ์๋ ํค์๋๋ฅผ ์ถ์ถํ๋ ํจ์ | |
| """ | |
| # ๋ถ์ฉ์ด ๋ชฉ๋ก (์ ์ธํ ๋จ์ด๋ค) | |
| stop_words = [ | |
| '๋ฐ', '๋ฑ', '์ธ', 'ํฌํจ', '์ธํธ', '๊ตฌ์ฑ', '๊ฐ๋ฅ', '์ฉ', | |
| '์๋', '์ํ', '๊ธฐํ', '์์', '์ฌ์ฉ', '๊ฐ', '์์', '์์', | |
| '๊ธฐ๋ณธ', '๊ฐ๋ฅํ', '๋๋', 'ํฌํจ๋', 'ํจ๊ป', '๊ฒฝ์ฐ', '์ ๊ณต', | |
| '์ ์ฉ', '์', '์', 'ํจ', '์', '์ด', '๊ฐ', '์', '๋ฅผ', | |
| '์', '์์', '์ผ๋ก', '๋ก', '๋', '์', '์ด๋ค', '์๋ค', | |
| '๋', '์', '๊ณผ', 'ํด๋' | |
| ] | |
| # ๋ชจ๋ ํค์๋๋ฅผ ์ ์ฅํ ๋์ ๋๋ฆฌ: {ํค์๋: [๋ฑ์ฅ ์ ๋ชฉ ์ธ๋ฑ์ค ๋ฆฌ์คํธ]} | |
| keyword_indices = defaultdict(list) | |
| # ๋ชจ๋ ์ ๋ชฉ์์ ํค์๋ ์ถ์ถ | |
| for idx, title in enumerate(titles): | |
| # ์ ๋ชฉ์์ ํน์๋ฌธ์, ๊ดํธ ๋ฑ ์ ๊ฑฐ | |
| clean_title = re.sub(r'[^\w\s]', ' ', title) | |
| # ์ ๋ชฉ์ ๋จ์ด๋ก ๋ถ๋ฆฌ | |
| words = clean_title.split() | |
| # ํ ์ ๋ชฉ ๋ด์์ ์ด๋ฏธ ์ฒ๋ฆฌํ ํค์๋ ์ถ์ | |
| processed_words = set() | |
| for word in words: | |
| # 2๊ธ์ ๋ฏธ๋ง ๋จ์ด, ์ซ์๋ง์ผ๋ก ๋ ๋จ์ด, ๋ถ์ฉ์ด ์ ์ธ | |
| if len(word) < 2 or word.isdigit() or word.lower() in stop_words: | |
| continue | |
| # ๊ฐ ํค์๋๋ ์ ๋ชฉ๋น ํ ๋ฒ๋ง ์นด์ดํธ | |
| if word not in processed_words: | |
| keyword_indices[word].append(idx) | |
| processed_words.add(word) | |
| return keyword_indices | |
| def categorize_keywords_by_products(keyword_indices, titles, product_categories): | |
| """ | |
| ์ํ๋ช ์ ๋ฐ๋ผ ํค์๋๋ฅผ ์นดํ ๊ณ ๋ฆฌ๋ณ๋ก ๋ถ๋ฅํ๋ ํจ์ | |
| """ | |
| # ๋ชจ๋ ์นดํ ๊ณ ๋ฆฌ ๋ชฉ๋ก ์ถ์ถ | |
| all_categories = set(product_categories.values()) | |
| # ๊ฐ ํค์๋๋ณ๋ก ์นดํ ๊ณ ๋ฆฌ ๋งคํ | |
| keyword_mappings = {} | |
| # ๊ฐ ํค์๋์ ๋ํด ์นดํ ๊ณ ๋ฆฌ ํ๋จ | |
| for keyword, indices in keyword_indices.items(): | |
| keyword_mappings[keyword] = set() | |
| # ํค์๋๊ฐ ๋ฑ์ฅํ ๊ฐ ์ํ์ ๋ํด ์นดํ ๊ณ ๋ฆฌ ํ์ธ | |
| for idx in indices: | |
| if idx < len(titles): | |
| title = titles[idx] | |
| # ํด๋น ์ํ์ ์นดํ ๊ณ ๋ฆฌ ์ฐพ๊ธฐ | |
| if title in product_categories: | |
| category = product_categories[title] | |
| keyword_mappings[keyword].add(category) | |
| return keyword_mappings, sorted(list(all_categories)) | |
| def analyze_keywords_with_categories(titles, text, include_search_volume=True): | |
| """ | |
| ์ ๋ชฉ์์ ํค์๋๋ฅผ ๋ถ์ํ๊ณ ์นดํ ๊ณ ๋ฆฌ๋ณ๋ก ๋ถ๋ฅํ๋ ํจ์ | |
| ๋ฐํ: | |
| - keyword_stats: ํค์๋ ๋ถ์ ์ ๋ณด๊ฐ ๋ด๊ธด DataFrame | |
| """ | |
| # ์ํ๋ช ๊ณผ ์นดํ ๊ณ ๋ฆฌ ๋งคํ ์ถ์ถ | |
| product_categories = extract_product_categories(text) | |
| # ์ ๋ชฉ์์ ํค์๋ ์ถ์ถ | |
| keyword_indices = extract_keywords(titles) | |
| # ํค์๋ ์นดํ ๊ณ ๋ฆฌ ๋ถ๋ฅ | |
| keyword_mappings, all_categories = categorize_keywords_by_products(keyword_indices, titles, product_categories) | |
| # ๋ค์ด๋ฒ ๊ฒ์๋ ์กฐํ (๋ณ๋ ฌ ์ฒ๋ฆฌ) | |
| search_volumes = {} | |
| if include_search_volume: | |
| keywords_list = list(keyword_indices.keys()) | |
| search_volumes = fetch_all_search_volumes(keywords_list) | |
| # ํค์๋ ํต๊ณ ์ ๋ณด ์์ฑ | |
| keyword_stats = [] | |
| for keyword, indices in keyword_indices.items(): | |
| # ํด๋น ํค์๋๊ฐ ๋ฑ์ฅํ ์ ๋ชฉ ์ | |
| count = len(indices) | |
| # ๋ฑ์ฅํ ์ ๋ชฉ ์์ ๋ฌธ์์ด | |
| ranks = ", ".join(str(idx + 1) for idx in indices) | |
| # ์์ ๋ ธ์ถ ์นดํ ๊ณ ๋ฆฌ ๊ฒฐ์ | |
| if any(idx < 10 for idx in indices): | |
| exposure_category = "์์ 10๊ฐ" | |
| elif any(idx < 20 for idx in indices): | |
| exposure_category = "์์ 20๊ฐ" | |
| elif any(idx < 30 for idx in indices): | |
| exposure_category = "์์ 30๊ฐ" | |
| elif any(idx < 40 for idx in indices): | |
| exposure_category = "์์ 40๊ฐ" | |
| else: | |
| exposure_category = "1ํ์ด์ง ๋ฏธํฌํจ" | |
| # ๊ธฐ๋ณธ ํค์๋ ์ ๋ณด | |
| keyword_data = { | |
| 'ํค์๋': keyword, | |
| '๋ฑ์ฅ ์์': ranks, | |
| '์ฌ์ฉ ํ์': count, | |
| '์์๋ ธ์ถ ํค์๋': exposure_category | |
| } | |
| # ๋ค์ด๋ฒ ๊ฒ์๋ ์ ๋ณด ์ถ๊ฐ | |
| if keyword in search_volumes: | |
| keyword_data['PC๊ฒ์๋'] = search_volumes[keyword]['PC๊ฒ์๋'] | |
| keyword_data['๋ชจ๋ฐ์ผ๊ฒ์๋'] = search_volumes[keyword]['๋ชจ๋ฐ์ผ๊ฒ์๋'] | |
| keyword_data['์ด๊ฒ์๋'] = search_volumes[keyword]['์ด๊ฒ์๋'] | |
| else: | |
| keyword_data['PC๊ฒ์๋'] = 0 | |
| keyword_data['๋ชจ๋ฐ์ผ๊ฒ์๋'] = 0 | |
| keyword_data['์ด๊ฒ์๋'] = 0 | |
| # ๊ฐ ์นดํ ๊ณ ๋ฆฌ๋ณ๋ก "O" ํ์ ์ถ๊ฐ | |
| for category in all_categories: | |
| keyword_data[category] = "O" if category in keyword_mappings[keyword] else "" | |
| keyword_stats.append(keyword_data) | |
| # ์ฌ์ฉ ํ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ | |
| keyword_stats = sorted(keyword_stats, key=lambda x: x['์ฌ์ฉ ํ์'], reverse=True) | |
| # DataFrame์ผ๋ก ๋ณํ | |
| df = pd.DataFrame(keyword_stats) | |
| return df | |
| def process_text_and_download(input_text, debug=False, include_search_volume=True): | |
| """ | |
| ํ ์คํธ๋ฅผ ๋ถ์ํ์ฌ ์ ๋ชฉ์ ์ถ์ถํ๊ณ ์์ ํ์ผ๋ก ๋ณํํ๋ ํจ์ | |
| """ | |
| if not input_text.strip(): | |
| return None, None, "ํ ์คํธ๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์." | |
| # ์ ๋ชฉ ์ถ์ถ | |
| titles = extract_titles(input_text, debug) | |
| if not titles: | |
| return None, None, "์ถ์ถ๋ ์ ๋ชฉ์ด ์์ต๋๋ค. ๋ค๋ฅธ ํ ์คํธ๋ฅผ ์๋ํด๋ณด์ธ์." | |
| # ๋ฒํธ ๋ถ์ฌ ๋ฐ DataFrame ์์ฑ | |
| df_titles = pd.DataFrame({ | |
| '๋ฒํธ': range(1, len(titles) + 1), | |
| '์ ๋ชฉ': titles | |
| }) | |
| # ํค์๋ ๋ถ์ ์ํ (์นดํ ๊ณ ๋ฆฌ ํฌํจ) | |
| keyword_df = analyze_keywords_with_categories(titles, input_text, include_search_volume) | |
| # ์์ ํ์ผ๋ก ์ ์ฅ (์ ๋ชฉ์ฉ) | |
| titles_file = tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx') | |
| titles_file.close() | |
| # ์์ ํ์ผ๋ก ์ ์ฅ (ํค์๋์ฉ) | |
| keywords_file = tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx') | |
| keywords_file.close() | |
| # ์ ๋ชฉ ๋ฐ์ดํฐ ์์ ํ์ผ๋ก ๋ณํ | |
| with pd.ExcelWriter(titles_file.name, engine='xlsxwriter') as writer: | |
| df_titles.to_excel(writer, sheet_name='์ ๋ชฉ ๋ชฉ๋ก', index=False) | |
| # ์ด ๋๋น ์๋ ์กฐ์ | |
| worksheet = writer.sheets['์ ๋ชฉ ๋ชฉ๋ก'] | |
| worksheet.set_column('A:A', 5) # ๋ฒํธ ์ด | |
| worksheet.set_column('B:B', 100) # ์ ๋ชฉ ์ด | |
| # ํค์๋ ๋ฐ์ดํฐ ์์ ํ์ผ๋ก ๋ณํ | |
| with pd.ExcelWriter(keywords_file.name, engine='xlsxwriter') as writer: | |
| keyword_df.to_excel(writer, sheet_name='ํค์๋ ๋ถ์', index=False) | |
| # ์ด ๋๋น ์๋ ์กฐ์ | |
| worksheet = writer.sheets['ํค์๋ ๋ถ์'] | |
| worksheet.set_column('A:A', 15) # ํค์๋ ์ด | |
| worksheet.set_column('B:B', 30) # ๋ฑ์ฅ ์์ ์ด | |
| worksheet.set_column('C:C', 10) # ์ฌ์ฉ ํ์ ์ด | |
| worksheet.set_column('D:D', 15) # ์์๋ ธ์ถ ํค์๋ ์ด | |
| worksheet.set_column('E:E', 10) # PC๊ฒ์๋ ์ด | |
| worksheet.set_column('F:F', 12) # ๋ชจ๋ฐ์ผ๊ฒ์๋ ์ด | |
| worksheet.set_column('G:G', 10) # ์ด๊ฒ์๋ ์ด | |
| # H์ด๋ถํฐ ์นดํ ๊ณ ๋ฆฌ ๋งคํธ๋ฆญ์ค ์ด ๋๋น ์กฐ์ | |
| for i, col in enumerate(keyword_df.columns[7:], start=7): | |
| worksheet.set_column(f'{chr(65+i)}:{chr(65+i)}', 20) | |
| # ๋ ธ์ถ ์นดํ ๊ณ ๋ฆฌ๋ณ ํค์๋ ์ ๊ณ์ฐ | |
| exposure_counts = keyword_df['์์๋ ธ์ถ ํค์๋'].value_counts().to_dict() | |
| top10_count = exposure_counts.get('์์ 10๊ฐ', 0) | |
| top20_count = exposure_counts.get('์์ 20๊ฐ', 0) | |
| top30_count = exposure_counts.get('์์ 30๊ฐ', 0) | |
| top40_count = exposure_counts.get('์์ 40๊ฐ', 0) | |
| not_included_count = exposure_counts.get('1ํ์ด์ง ๋ฏธํฌํจ', 0) | |
| # ์นดํ ๊ณ ๋ฆฌ๋ณ ํค์๋ ์ ๊ณ์ฐ | |
| result_text = f"์ด {len(titles)}๊ฐ์ ์ ๋ชฉ์ด ์ถ์ถ๋์์ต๋๋ค.\n\n" | |
| for i, title in enumerate(titles, 1): | |
| result_text += f"{i}. {title}\n" | |
| result_text += f"\n\nํค์๋ ๋ถ์ ๊ฒฐ๊ณผ (์ด {len(keyword_df)}๊ฐ ํค์๋):\n" | |
| result_text += f"- ์์ 10๊ฐ ์ ๋ชฉ์ ํฌํจ๋ ํค์๋: {top10_count}๊ฐ\n" | |
| result_text += f"- ์์ 20๊ฐ ์ ๋ชฉ์ ํฌํจ๋ ํค์๋: {top20_count}๊ฐ\n" | |
| result_text += f"- ์์ 30๊ฐ ์ ๋ชฉ์ ํฌํจ๋ ํค์๋: {top30_count}๊ฐ\n" | |
| result_text += f"- ์์ 40๊ฐ ์ ๋ชฉ์ ํฌํจ๋ ํค์๋: {top40_count}๊ฐ\n" | |
| result_text += f"- 1ํ์ด์ง์ ํฌํจ๋์ง ์์ ํค์๋: {not_included_count}๊ฐ\n\n" | |
| result_text += f"\n๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋ ํค์๋ (์์ 20๊ฐ):\n" | |
| for i, row in keyword_df.head(20).iterrows(): | |
| # ํด๋น ํค์๋๊ฐ ์ํ ๋ชจ๋ ์นดํ ๊ณ ๋ฆฌ ์ฐพ๊ธฐ | |
| categories = [] | |
| for col in keyword_df.columns[7:]: # ์นดํ ๊ณ ๋ฆฌ ์ด๋ค | |
| if row[col] == "O": | |
| categories.append(col) | |
| category_text = ", ".join(categories) if categories else "๋ฏธ๋ถ๋ฅ" | |
| result_text += f"{row['ํค์๋']} - {row['์ฌ์ฉ ํ์']}ํ ๋ฑ์ฅ ({row['์์๋ ธ์ถ ํค์๋']}, PC:{row['PC๊ฒ์๋']}, ๋ชจ๋ฐ์ผ:{row['๋ชจ๋ฐ์ผ๊ฒ์๋']}, ์ด:{row['์ด๊ฒ์๋']}, ์นดํ ๊ณ ๋ฆฌ: {category_text})\n" | |
| return titles_file.name, keywords_file.name, result_text | |
| def create_app(): | |
| """ | |
| ๊ทธ๋ผ๋์ค ์ฑ ์์ฑ ํจ์ | |
| """ | |
| with gr.Blocks(title="์ ํ ์ ๋ชฉ ๋ฐ ํค์๋ ์ถ์ถ๊ธฐ") as app: | |
| gr.Markdown(""" | |
| # ํ ์คํธ์์ ์ ํ ์ ๋ชฉ ๋ฐ ํค์๋ ์ถ์ถ๊ธฐ (๋ค์ด๋ฒ ๊ฒ์๋ ํฌํจ) | |
| ์ ํ ๋ชฉ๋ก์ด ํฌํจ๋ ํ ์คํธ๋ฅผ ์ ๋ ฅํ๋ฉด ์ ํ ์ ๋ชฉ์ ์๋์ผ๋ก ์ถ์ถํ๊ณ , ํค์๋ ๋ถ์์ ์ํํฉ๋๋ค. | |
| ### ๊ธฐ๋ฅ: | |
| 1. ์ ํ ์ ๋ชฉ ์ถ์ถ | |
| 2. ์นดํ ๊ณ ๋ฆฌ ์ถ์ถ (๊ฐ๊ตฌ/์ธํ ๋ฆฌ์ด, ์ํ/๊ฑด๊ฐ, ํจ์ ์กํ, ๋์งํธ/๊ฐ์ , ์ถ์ฐ/์ก์, ์คํฌ์ธ /๋ ์ , ํ์ฅํ/๋ฏธ์ฉ, ํจ์ ์๋ฅ, ์ฌ๊ฐ/์ํํธ์, ์ํ) | |
| 3. ์ถ์ถ๋ ์ ๋ชฉ์์ ํค์๋ ์ถ์ถ ๋ฐ ๋ถ์ | |
| 4. ํค์๋ ์ฌ์ฉ ๋น๋ ๋ฐ ๋ฑ์ฅ ์์ ๋ถ์ | |
| 5. ์์ ๋ ธ์ถ ํค์๋ ๋ถ์ (์์ 10๊ฐ, 20๊ฐ, 30๊ฐ, 40๊ฐ) | |
| 6. **๋ค์ด๋ฒ ๊ฒ์๋ ์กฐํ (PC๊ฒ์๋, ๋ชจ๋ฐ์ผ๊ฒ์๋, ์ด๊ฒ์๋)** | |
| 7. ํค์๋๋ณ ์นดํ ๊ณ ๋ฆฌ ๋ถ๋ฅ ๋ฐ ๋งคํธ๋ฆญ์ค ์์ฑ | |
| ### ์ฃผ์ ํน์ง: | |
| - "*" ๋ฌธ์ ์ ๊ฑฐ, ์ค๋ณต ๊ณต๋ฐฑ ์ฒ๋ฆฌ | |
| - ๊ด๊ณ ์ฃผ๋ณ ํ ์คํธ๋ง ์ ์ธํ์ฌ ์ ํํ ์ ๋ชฉ ์ถ์ถ | |
| - 'ํด์ธ' ์ ๋์ฌ ์๋ ์ ๊ฑฐ | |
| - ํค์๋ ์์ ๋ ธ์ถ ๋ถ์ | |
| - ๋ค์ด๋ฒ ๊ฒ์๋ ๋ณ๋ ฌ ์กฐํ (25๊ฐ ๋จ์) | |
| - ์นดํ ๊ณ ๋ฆฌ๋ณ ํค์๋ ๋ถ๋ฅ ๋ฐ ๋งคํธ๋ฆญ์ค ํ์ | |
| ### ๋ค์ด๋ฒ API ์ค์ : | |
| - NAVER_API_KEY ํ๊ฒฝ ๋ณ์ ์ค์ ํ์ | |
| - NAVER_SECRET_KEY ํ๊ฒฝ ๋ณ์ ์ค์ ํ์ | |
| - NAVER_CUSTOMER_ID ํ๊ฒฝ ๋ณ์ ์ค์ ํ์ | |
| """) | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_text = gr.Textbox( | |
| label="์ ๋ชฉ์ ์ถ์ถํ ํ ์คํธ ์ ๋ ฅ", | |
| placeholder="์ฌ๊ธฐ์ ์ ํ ๋ชฉ๋ก์ด ํฌํจ๋ ํ ์คํธ๋ฅผ ๋ถ์ฌ๋ฃ์ผ์ธ์...", | |
| lines=15 | |
| ) | |
| debug_mode = gr.Checkbox(label="๋๋ฒ๊น ๋ชจ๋ (์์ธ ๋ก๊ทธ ์ถ๋ ฅ)") | |
| search_volume_checkbox = gr.Checkbox(label="๋ค์ด๋ฒ ๊ฒ์๋ ์กฐํ", value=True) | |
| submit_btn = gr.Button("์ ๋ชฉ ์ถ์ถ ๋ฐ ํค์๋ ๋ถ์") | |
| with gr.Column(): | |
| output_text = gr.Textbox(label="์ถ์ถ ๊ฒฐ๊ณผ", lines=15) | |
| titles_download_btn = gr.File(label="์ ๋ชฉ ๋ชฉ๋ก ๋ค์ด๋ก๋", file_types=[".xlsx"]) | |
| keywords_download_btn = gr.File(label="ํค์๋ ๋ถ์ ๋ค์ด๋ก๋", file_types=[".xlsx"]) | |
| def process_wrapper(text, debug, include_search_volume): | |
| titles_file, keywords_file, result_text = process_text_and_download(text, debug, include_search_volume) | |
| return [titles_file, keywords_file, result_text] | |
| submit_btn.click( | |
| process_wrapper, | |
| inputs=[input_text, debug_mode, search_volume_checkbox], | |
| outputs=[titles_download_btn, keywords_download_btn, output_text] | |
| ) | |
| return app | |
| # ์ฑ ์คํ | |
| if __name__ == "__main__": | |
| demo = create_app() | |
| demo.launch() |