Update app.py
Browse files
app.py
CHANGED
@@ -8,12 +8,13 @@ import pandas as pd
|
|
8 |
import tempfile
|
9 |
import gradio as gr
|
10 |
|
11 |
-
#
|
12 |
def generate_signature(timestamp, method, uri, secret_key):
|
13 |
message = f"{timestamp}.{method}.{uri}"
|
14 |
digest = hmac.new(secret_key.encode("utf-8"), message.encode("utf-8"), hashlib.sha256).digest()
|
15 |
return base64.b64encode(digest).decode()
|
16 |
|
|
|
17 |
def get_header(method, uri, api_key, secret_key, customer_id):
|
18 |
timestamp = str(round(time.time() * 1000))
|
19 |
signature = generate_signature(timestamp, method, uri, secret_key)
|
@@ -25,8 +26,13 @@ def get_header(method, uri, api_key, secret_key, customer_id):
|
|
25 |
"X-Signature": signature
|
26 |
}
|
27 |
|
28 |
-
#
|
29 |
def fetch_related_keywords(keyword):
|
|
|
|
|
|
|
|
|
|
|
30 |
API_KEY = os.environ["NAVER_API_KEY"]
|
31 |
SECRET_KEY = os.environ["NAVER_SECRET_KEY"]
|
32 |
CUSTOMER_ID = os.environ["NAVER_CUSTOMER_ID"]
|
@@ -35,6 +41,7 @@ def fetch_related_keywords(keyword):
|
|
35 |
uri = "/keywordstool"
|
36 |
method = "GET"
|
37 |
headers = get_header(method, uri, API_KEY, SECRET_KEY, CUSTOMER_ID)
|
|
|
38 |
params = {
|
39 |
"hintKeywords": [keyword],
|
40 |
"showDetail": "1"
|
@@ -44,68 +51,60 @@ def fetch_related_keywords(keyword):
|
|
44 |
if "keywordList" not in data:
|
45 |
return pd.DataFrame()
|
46 |
df = pd.DataFrame(data["keywordList"])
|
|
|
47 |
if len(df) > 100:
|
48 |
df = df.head(100)
|
49 |
|
|
|
50 |
def parse_count(x):
|
51 |
try:
|
52 |
-
|
|
|
53 |
except:
|
54 |
return 0
|
55 |
|
|
|
56 |
df["PCμκ²μλ"] = df["monthlyPcQcCnt"].apply(parse_count)
|
57 |
df["λͺ¨λ°μΌμκ²μλ"] = df["monthlyMobileQcCnt"].apply(parse_count)
|
58 |
df["ν νμκ²μλ"] = df["PCμκ²μλ"] + df["λͺ¨λ°μΌμκ²μλ"]
|
|
|
59 |
df.rename(columns={"relKeyword": "μ 보ν€μλ"}, inplace=True)
|
|
|
60 |
result_df = df[["μ 보ν€μλ", "PCμκ²μλ", "λͺ¨λ°μΌμκ²μλ", "ν νμκ²μλ"]]
|
61 |
return result_df
|
62 |
|
63 |
-
#
|
64 |
-
def fetch_blog_count(keyword):
|
65 |
-
client_id = os.environ["NAVER_SEARCH_CLIENT_ID"]
|
66 |
-
client_secret = os.environ["NAVER_SEARCH_CLIENT_SECRET"]
|
67 |
-
url = "https://openapi.naver.com/v1/search/blog.json"
|
68 |
-
headers = {
|
69 |
-
"X-Naver-Client-Id": client_id,
|
70 |
-
"X-Naver-Client-Secret": client_secret
|
71 |
-
}
|
72 |
-
params = {"query": keyword, "display": 1}
|
73 |
-
response = requests.get(url, headers=headers, params=params)
|
74 |
-
if response.status_code == 200:
|
75 |
-
data = response.json()
|
76 |
-
return data.get("total", 0)
|
77 |
-
else:
|
78 |
-
return 0
|
79 |
-
|
80 |
-
# --- μμ μμ
νμΌ μμ± ---
|
81 |
def create_excel_file(df):
|
82 |
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
83 |
excel_path = tmp.name
|
84 |
df.to_excel(excel_path, index=False)
|
85 |
return excel_path
|
86 |
|
87 |
-
#
|
88 |
def process_keyword(keywords: str, include_related: bool):
|
89 |
"""
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
"""
|
95 |
-
|
|
|
96 |
result_dfs = []
|
97 |
|
98 |
for idx, kw in enumerate(input_keywords):
|
99 |
df_kw = fetch_related_keywords(kw)
|
100 |
if df_kw.empty:
|
101 |
continue
|
102 |
-
# μ
λ ₯ ν€μλμ ν΄λΉνλ
|
103 |
row_kw = df_kw[df_kw["μ 보ν€μλ"] == kw]
|
104 |
if not row_kw.empty:
|
105 |
result_dfs.append(row_kw)
|
106 |
else:
|
|
|
107 |
result_dfs.append(df_kw.head(1))
|
108 |
-
|
|
|
109 |
if include_related and idx == 0:
|
110 |
df_related = df_kw[df_kw["μ 보ν€μλ"] != kw]
|
111 |
if not df_related.empty:
|
@@ -113,44 +112,30 @@ def process_keyword(keywords: str, include_related: bool):
|
|
113 |
|
114 |
if result_dfs:
|
115 |
result_df = pd.concat(result_dfs, ignore_index=True)
|
|
|
116 |
result_df.drop_duplicates(subset=["μ 보ν€μλ"], inplace=True)
|
117 |
else:
|
118 |
result_df = pd.DataFrame(columns=["μ 보ν€μλ", "PCμκ²μλ", "λͺ¨λ°μΌμκ²μλ", "ν νμκ²μλ"])
|
119 |
-
|
120 |
-
# κ° μ 보ν€μλμ λν΄ λΈλ‘κ·Έ λ¬Έμμ μ‘°ν
|
121 |
-
result_df["λΈλ‘κ·Έλ¬Έμμ"] = result_df["μ 보ν€μλ"].apply(fetch_blog_count)
|
122 |
result_df.sort_values(by="ν νμκ²μλ", ascending=False, inplace=True)
|
123 |
-
|
124 |
return result_df, create_excel_file(result_df)
|
125 |
|
126 |
-
#
|
127 |
-
with gr.Blocks(
|
128 |
-
gr.Markdown("
|
129 |
-
gr.Markdown(
|
130 |
-
|
131 |
-
|
132 |
-
"
|
133 |
-
|
|
|
134 |
|
135 |
with gr.Row():
|
136 |
-
|
137 |
-
|
138 |
-
label="ν€μλ μ
λ ₯ (μ¬λ¬ κ°μΌ κ²½μ° μν°λ‘ ꡬλΆ)",
|
139 |
-
lines=6,
|
140 |
-
placeholder="μ:\nκ°μλνλΉλΌ\nμλ°μ€ν¬λ¦½νΈ"
|
141 |
-
)
|
142 |
-
include_checkbox = gr.Checkbox(label="μ°κ΄κ²μμ΄ ν¬ν¨ (첫λ²μ§Έ ν€μλμ νν¨)", value=False)
|
143 |
-
search_button = gr.Button("κ²μ", variant="primary")
|
144 |
-
with gr.Column(scale=1):
|
145 |
-
gr.Markdown("### κ²μ κ²°κ³Ό")
|
146 |
-
df_output = gr.Dataframe(label="κ²°κ³Ό ν
μ΄λΈ")
|
147 |
-
excel_output = gr.File(label="μμ
λ€μ΄λ‘λ")
|
148 |
|
149 |
-
|
150 |
-
|
151 |
-
inputs=[keyword_input, include_checkbox],
|
152 |
-
outputs=[df_output, excel_output]
|
153 |
-
)
|
154 |
|
155 |
# μ± μ€ν (Hugging Face Spaces λ°°ν¬ κ°λ₯)
|
156 |
demo.launch()
|
|
|
8 |
import tempfile
|
9 |
import gradio as gr
|
10 |
|
11 |
+
# μΈμ¦ μλͺ
μ μμ±νλ ν¨μ
|
12 |
def generate_signature(timestamp, method, uri, secret_key):
|
13 |
message = f"{timestamp}.{method}.{uri}"
|
14 |
digest = hmac.new(secret_key.encode("utf-8"), message.encode("utf-8"), hashlib.sha256).digest()
|
15 |
return base64.b64encode(digest).decode()
|
16 |
|
17 |
+
# API νΈμΆ ν€λλ₯Ό μμ±νλ ν¨μ
|
18 |
def get_header(method, uri, api_key, secret_key, customer_id):
|
19 |
timestamp = str(round(time.time() * 1000))
|
20 |
signature = generate_signature(timestamp, method, uri, secret_key)
|
|
|
26 |
"X-Signature": signature
|
27 |
}
|
28 |
|
29 |
+
# λ€μ΄λ² μ°κ΄κ²μμ΄ λ° κ²μλ λ°μ΄ν°λ₯Ό κ°μ Έμ€λ ν¨μ
|
30 |
def fetch_related_keywords(keyword):
|
31 |
+
"""
|
32 |
+
λ¨μΌ ν€μλμ λν΄ λ€μ΄λ² κ²μκ΄κ³ APIλ₯Ό νΈμΆνμ¬ μ°κ΄κ²μμ΄ λ° κ²μλ λ°μ΄ν°λ₯Ό DataFrameμΌλ‘ λ°νν©λλ€.
|
33 |
+
λ°ν 컬λΌ: "μ 보ν€μλ", "PCμκ²μλ", "λͺ¨λ°μΌμκ²μλ", "ν νμκ²μλ"
|
34 |
+
"""
|
35 |
+
# νκ²½λ³μμμ API ν€ λ±μ μ½μ΄μ΅λλ€ (κΈ°λ³Έκ° μμ΄ μ¬μ©)
|
36 |
API_KEY = os.environ["NAVER_API_KEY"]
|
37 |
SECRET_KEY = os.environ["NAVER_SECRET_KEY"]
|
38 |
CUSTOMER_ID = os.environ["NAVER_CUSTOMER_ID"]
|
|
|
41 |
uri = "/keywordstool"
|
42 |
method = "GET"
|
43 |
headers = get_header(method, uri, API_KEY, SECRET_KEY, CUSTOMER_ID)
|
44 |
+
# API νλΌλ―Έν°: hintKeywordsλ 리μ€νΈλ‘ μ λ¬
|
45 |
params = {
|
46 |
"hintKeywords": [keyword],
|
47 |
"showDetail": "1"
|
|
|
51 |
if "keywordList" not in data:
|
52 |
return pd.DataFrame()
|
53 |
df = pd.DataFrame(data["keywordList"])
|
54 |
+
# μ΅λ 100κ°κΉμ§ κ²°κ³Ό μ¬μ©
|
55 |
if len(df) > 100:
|
56 |
df = df.head(100)
|
57 |
|
58 |
+
# λ¬Έμμ΄ ννμ κ²μλμ μ μλ‘ λ³ννλ ν¨μ
|
59 |
def parse_count(x):
|
60 |
try:
|
61 |
+
x_str = str(x).replace(",", "")
|
62 |
+
return int(x_str)
|
63 |
except:
|
64 |
return 0
|
65 |
|
66 |
+
# κ° κ²μλ νλλ₯Ό μ μνμΌλ‘ λ³ν ν ν ν κ²μλ κ³μ°
|
67 |
df["PCμκ²μλ"] = df["monthlyPcQcCnt"].apply(parse_count)
|
68 |
df["λͺ¨λ°μΌμκ²μλ"] = df["monthlyMobileQcCnt"].apply(parse_count)
|
69 |
df["ν νμκ²μλ"] = df["PCμκ²μλ"] + df["λͺ¨λ°μΌμκ²μλ"]
|
70 |
+
# 'relKeyword' 컬λΌλͺ
μ "μ 보ν€μλ"λ‘ λ³κ²½
|
71 |
df.rename(columns={"relKeyword": "μ 보ν€μλ"}, inplace=True)
|
72 |
+
# νμν 컬λΌλ§ μ ν
|
73 |
result_df = df[["μ 보ν€μλ", "PCμκ²μλ", "λͺ¨λ°μΌμκ²μλ", "ν νμκ²μλ"]]
|
74 |
return result_df
|
75 |
|
76 |
+
# μμ μμ
νμΌ μμ± ν¨μ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
def create_excel_file(df):
|
78 |
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
79 |
excel_path = tmp.name
|
80 |
df.to_excel(excel_path, index=False)
|
81 |
return excel_path
|
82 |
|
83 |
+
# μ
λ ₯λ μ¬λ¬ ν€μλλ₯Ό μ²λ¦¬νλ ν¨μ
|
84 |
def process_keyword(keywords: str, include_related: bool):
|
85 |
"""
|
86 |
+
- ν
μ€νΈλ°μ€μ μν°λ‘ ꡬλΆλ μ¬λ¬ ν€μλλ₯Ό λ°μ κ° ν€μλμ κ²μλ μ 보λ₯Ό μ‘°νν©λλ€.
|
87 |
+
- κ° ν€μλμ λν΄ μ
λ ₯ν ν€μλ μ체μ κ²°κ³Ό(κ²μλ)λ₯Ό μΆκ°ν©λλ€.
|
88 |
+
- 체ν¬λ°μ€κ° μ νλ κ²½μ°, 첫 λ²μ§Έ ν€μλμ λν΄μλ§ μ°κ΄κ²μμ΄(μ
λ ₯ ν€μλλ₯Ό μ μΈν λλ¨Έμ§ κ²°κ³Ό)λ₯Ό μΆκ°ν©λλ€.
|
89 |
+
- κ²°κ³Ό DataFrameκ³Ό μμ
νμΌ κ²½λ‘λ₯Ό ννλ‘ λ°νν©λλ€.
|
90 |
"""
|
91 |
+
# μ€λ°κΏμΌλ‘ λΆλ¦¬νμ¬ μ
λ ₯ ν€μλ 리μ€νΈ μμ± (λΉ μ€ μ μΈ)
|
92 |
+
input_keywords = [k.strip() for k in keywords.splitlines() if k.strip() != ""]
|
93 |
result_dfs = []
|
94 |
|
95 |
for idx, kw in enumerate(input_keywords):
|
96 |
df_kw = fetch_related_keywords(kw)
|
97 |
if df_kw.empty:
|
98 |
continue
|
99 |
+
# μ
λ ₯ ν€μλμ ν΄λΉνλ νμ μ°Ύμ΅λλ€.
|
100 |
row_kw = df_kw[df_kw["μ 보ν€μλ"] == kw]
|
101 |
if not row_kw.empty:
|
102 |
result_dfs.append(row_kw)
|
103 |
else:
|
104 |
+
# λ§μ½ μ
λ ₯ ν€μλ νμ΄ μλ€λ©΄ 첫λ²μ§Έ νμ μΆκ° (λ체)
|
105 |
result_dfs.append(df_kw.head(1))
|
106 |
+
|
107 |
+
# 체ν¬λ°μ€κ° Trueμ΄κ³ , 첫 λ²μ§Έ ν€μλμΈ κ²½μ°μλ§ μ°κ΄κ²μμ΄ μΆκ° (μ
λ ₯ ν€μλ μ μΈ)
|
108 |
if include_related and idx == 0:
|
109 |
df_related = df_kw[df_kw["μ 보ν€μλ"] != kw]
|
110 |
if not df_related.empty:
|
|
|
112 |
|
113 |
if result_dfs:
|
114 |
result_df = pd.concat(result_dfs, ignore_index=True)
|
115 |
+
# μ€λ³΅ ν μ κ±° (λ§μ½ μ
λ ₯ ν€μλκ° μ°κ΄κ²μμ΄ κ²°κ³Όμ ν¬ν¨λ κ²½μ°)
|
116 |
result_df.drop_duplicates(subset=["μ 보ν€μλ"], inplace=True)
|
117 |
else:
|
118 |
result_df = pd.DataFrame(columns=["μ 보ν€μλ", "PCμκ²μλ", "λͺ¨λ°μΌμκ²μλ", "ν νμκ²μλ"])
|
119 |
+
# ν νμκ²μλ λ΄λ¦Όμ°¨μ μ λ ¬ (μ νμ¬ν)
|
|
|
|
|
120 |
result_df.sort_values(by="ν νμκ²μλ", ascending=False, inplace=True)
|
|
|
121 |
return result_df, create_excel_file(result_df)
|
122 |
|
123 |
+
# Gradio UI ꡬμ±
|
124 |
+
with gr.Blocks() as demo:
|
125 |
+
gr.Markdown("### λ€μ΄λ² μ°κ΄κ²μμ΄ λ° κ²μλ μ‘°ν μ±")
|
126 |
+
gr.Markdown("μ¬λ¬ ν€μλλ₯Ό μν°λ‘ ꡬλΆνμ¬ μ
λ ₯νλ©΄ κ° ν€μλμ κ²μλ μ 보λ₯Ό μ‘°νν©λλ€. (첫 λ²μ§Έ ν€μλμ κ²½μ°, 'μ°κ΄κ²μμ΄ ν¬ν¨' μ²΄ν¬ μ μ°κ΄κ²μμ΄λ ν¨κ» μ‘°νλ©λλ€.)")
|
127 |
+
|
128 |
+
with gr.Row():
|
129 |
+
keyword_input = gr.Textbox(label="ν€μλ μ
λ ₯ (μ¬λ¬ κ°μΌ κ²½μ° μν°λ‘ ꡬλΆ)", lines=5, placeholder="μ:\nκ°μλνλΉλΌ\nμλ°μ€ν¬λ¦½νΈ")
|
130 |
+
include_checkbox = gr.Checkbox(label="μ°κ΄κ²μμ΄ ν¬ν¨ (첫λ²μ§Έ ν€μλμ νν¨)", value=False)
|
131 |
+
search_button = gr.Button("κ²μ")
|
132 |
|
133 |
with gr.Row():
|
134 |
+
df_output = gr.Dataframe(label="κ²μ κ²°κ³Ό")
|
135 |
+
excel_output = gr.File(label="μμ
λ€μ΄λ‘λ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
+
# λ²νΌ ν΄λ¦ μ process_keyword ν¨μλ₯Ό μ€ν (λ κ°μ μ
λ ₯: ν€μλ, 체ν¬λ°μ€ μν)
|
138 |
+
search_button.click(fn=process_keyword, inputs=[keyword_input, include_checkbox], outputs=[df_output, excel_output])
|
|
|
|
|
|
|
139 |
|
140 |
# μ± μ€ν (Hugging Face Spaces λ°°ν¬ κ°λ₯)
|
141 |
demo.launch()
|