Update app.py
Browse files
app.py
CHANGED
@@ -8,13 +8,12 @@ import pandas as pd
|
|
8 |
import tempfile
|
9 |
import gradio as gr
|
10 |
|
11 |
-
# λ€μ΄λ² κ΄κ³ API
|
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,9 +25,8 @@ def get_header(method, uri, api_key, secret_key, customer_id):
|
|
26 |
"X-Signature": signature
|
27 |
}
|
28 |
|
29 |
-
# λ€μ΄λ² κ΄κ³ API
|
30 |
def fetch_related_keywords(keyword):
|
31 |
-
# νκ²½λ³μμμ κ΄κ³ API ν€κ°λ€μ λΆλ¬μ΅λλ€.
|
32 |
API_KEY = os.environ["NAVER_API_KEY"]
|
33 |
SECRET_KEY = os.environ["NAVER_SECRET_KEY"]
|
34 |
CUSTOMER_ID = os.environ["NAVER_CUSTOMER_ID"]
|
@@ -51,8 +49,7 @@ def fetch_related_keywords(keyword):
|
|
51 |
|
52 |
def parse_count(x):
|
53 |
try:
|
54 |
-
|
55 |
-
return int(x_str)
|
56 |
except:
|
57 |
return 0
|
58 |
|
@@ -63,9 +60,8 @@ def fetch_related_keywords(keyword):
|
|
63 |
result_df = df[["μ 보ν€μλ", "PCμκ²μλ", "λͺ¨λ°μΌμκ²μλ", "ν νμκ²μλ"]]
|
64 |
return result_df
|
65 |
|
66 |
-
# λ€μ΄λ² κ²μ
|
67 |
def fetch_blog_count(keyword):
|
68 |
-
# νκ²½λ³μμμ λ€μ΄λ² κ²μ API μ격μ¦λͺ
μ λΆλ¬μ΅λλ€.
|
69 |
client_id = os.environ["NAVER_SEARCH_CLIENT_ID"]
|
70 |
client_secret = os.environ["NAVER_SEARCH_CLIENT_SECRET"]
|
71 |
url = "https://openapi.naver.com/v1/search/blog.json"
|
@@ -81,38 +77,35 @@ def fetch_blog_count(keyword):
|
|
81 |
else:
|
82 |
return 0
|
83 |
|
84 |
-
# μμ μμ
νμΌ μμ±
|
85 |
def create_excel_file(df):
|
86 |
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
87 |
excel_path = tmp.name
|
88 |
df.to_excel(excel_path, index=False)
|
89 |
return excel_path
|
90 |
|
91 |
-
#
|
92 |
def process_keyword(keywords: str, include_related: bool):
|
93 |
"""
|
94 |
-
1.
|
95 |
-
2. κ° ν€μλμ λν΄
|
96 |
-
|
97 |
-
|
98 |
"""
|
99 |
-
|
100 |
-
input_keywords = [k.strip() for k in keywords.splitlines() if k.strip() != ""]
|
101 |
result_dfs = []
|
102 |
|
103 |
for idx, kw in enumerate(input_keywords):
|
104 |
df_kw = fetch_related_keywords(kw)
|
105 |
if df_kw.empty:
|
106 |
continue
|
107 |
-
# μ
λ ₯
|
108 |
row_kw = df_kw[df_kw["μ 보ν€μλ"] == kw]
|
109 |
if not row_kw.empty:
|
110 |
result_dfs.append(row_kw)
|
111 |
else:
|
112 |
-
# μ
λ ₯ ν€μλμ ν΄λΉνλ νμ΄ μμΌλ©΄ 첫 λ²μ§Έ νμ λμ²΄λ‘ μΆκ°
|
113 |
result_dfs.append(df_kw.head(1))
|
114 |
-
|
115 |
-
# 체ν¬λ°μ€κ° Trueμ΄κ³ , 첫 λ²μ§Έ ν€μλμ λν΄μλ§ μ°κ΄κ²μμ΄ μΆκ° (μ
λ ₯ ν€μλ μ μΈ)
|
116 |
if include_related and idx == 0:
|
117 |
df_related = df_kw[df_kw["μ 보ν€μλ"] != kw]
|
118 |
if not df_related.empty:
|
@@ -124,32 +117,40 @@ def process_keyword(keywords: str, include_related: bool):
|
|
124 |
else:
|
125 |
result_df = pd.DataFrame(columns=["μ 보ν€μλ", "PCμκ²μλ", "λͺ¨λ°μΌμκ²μλ", "ν νμκ²μλ"])
|
126 |
|
127 |
-
#
|
128 |
result_df["λΈλ‘κ·Έλ¬Έμμ"] = result_df["μ 보ν€μλ"].apply(fetch_blog_count)
|
129 |
-
|
130 |
result_df.sort_values(by="ν νμκ²μλ", ascending=False, inplace=True)
|
|
|
131 |
return result_df, create_excel_file(result_df)
|
132 |
|
133 |
-
# Gradio UI ꡬμ±
|
134 |
-
with gr.Blocks() as demo:
|
135 |
-
gr.Markdown("
|
136 |
gr.Markdown(
|
137 |
-
"μ¬λ¬ ν€μλλ₯Ό
|
138 |
-
"첫 λ²μ§Έ
|
139 |
"λν, κ° μ 보ν€μλμ λν λ€μ΄λ² λΈλ‘κ·Έ λ¬Έμμλ ν¨κ» μΆλ ₯λ©λλ€."
|
140 |
)
|
141 |
|
142 |
with gr.Row():
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
search_button.click(fn=process_keyword, inputs=[keyword_input, include_checkbox], outputs=[df_output, excel_output])
|
153 |
|
154 |
# μ± μ€ν (Hugging Face Spaces λ°°ν¬ κ°λ₯)
|
155 |
demo.launch()
|
|
|
8 |
import tempfile
|
9 |
import gradio as gr
|
10 |
|
11 |
+
# --- λ€μ΄λ² κ΄κ³ API: μλͺ
μμ± λ° ν€λ κ΅¬μ± ---
|
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 |
"X-Signature": signature
|
26 |
}
|
27 |
|
28 |
+
# --- λ€μ΄λ² κ΄κ³ API: μ°κ΄κ²μμ΄ λ° κ²μλ μ‘°ν ---
|
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"]
|
|
|
49 |
|
50 |
def parse_count(x):
|
51 |
try:
|
52 |
+
return int(str(x).replace(",", ""))
|
|
|
53 |
except:
|
54 |
return 0
|
55 |
|
|
|
60 |
result_df = df[["μ 보ν€μλ", "PCμκ²μλ", "λͺ¨λ°μΌμκ²μλ", "ν νμκ²μλ"]]
|
61 |
return result_df
|
62 |
|
63 |
+
# --- λ€μ΄λ² κ²μ API: λΈλ‘κ·Έ λ¬Έμμ μ‘°ν ---
|
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"
|
|
|
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 |
+
1. μ¬λ¬ ν€μλλ₯Ό μν°λ‘ ꡬλΆνμ¬ λ¦¬μ€νΈλ‘ λ§λλλ€.
|
91 |
+
2. κ° ν€μλμ λν΄ λ€μ΄λ² κ΄κ³ APIλ‘ κ²μλ μ 보λ₯Ό μ‘°ννκ³ ,
|
92 |
+
첫 λ²μ§Έ ν€μλμ λν΄ μ΅μ
(μ°κ΄κ²μμ΄ ν¬ν¨)μ΄ TrueμΈ κ²½μ° μ°κ΄κ²μμ΄λ μΆκ°ν©λλ€.
|
93 |
+
3. μ΅μ’
κ²°κ³Ό DataFrameμ κ° "μ 보ν€μλ"λ§λ€ λ€μ΄λ² κ²μ APIλ‘ λΈλ‘κ·Έ λ¬Έμμλ₯Ό μ‘°ννμ¬ "λΈλ‘κ·Έλ¬Έμμ" 컬λΌμ μΆκ°ν©λλ€.
|
94 |
"""
|
95 |
+
input_keywords = [k.strip() for k in keywords.splitlines() if k.strip()]
|
|
|
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:
|
|
|
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 |
+
# --- Gradio UI κ΅¬μ± ---
|
127 |
+
with gr.Blocks(css=".gradio-container { max-width: 960px; margin: auto; }") as demo:
|
128 |
+
gr.Markdown("# λ€μ΄λ² μ°κ΄κ²μμ΄, κ²μλ λ° λΈλ‘κ·Έ λ¬Έμμ μ‘°ν")
|
129 |
gr.Markdown(
|
130 |
+
"μ¬λ¬ ν€μλλ₯Ό **μν°**λ‘ κ΅¬λΆνμ¬ μ
λ ₯νμΈμ. κ° ν€μλμ λν κ²μλ μ 보λ₯Ό μ‘°ννλ©°, "
|
131 |
+
"첫 λ²μ§Έ ν€μλμ λν΄ 'μ°κ΄κ²μμ΄ ν¬ν¨' μ΅μ
μ μ ννλ©΄ μ°κ΄κ²μμ΄ κ²°κ³Όλ ν¨κ» μ‘°νλ©λλ€. \n\n"
|
132 |
"λν, κ° μ 보ν€μλμ λν λ€μ΄λ² λΈλ‘κ·Έ λ¬Έμμλ ν¨κ» μΆλ ₯λ©λλ€."
|
133 |
)
|
134 |
|
135 |
with gr.Row():
|
136 |
+
with gr.Column(scale=1):
|
137 |
+
keyword_input = gr.Textbox(
|
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 |
+
search_button.click(
|
150 |
+
fn=process_keyword,
|
151 |
+
inputs=[keyword_input, include_checkbox],
|
152 |
+
outputs=[df_output, excel_output]
|
153 |
+
)
|
|
|
154 |
|
155 |
# μ± μ€ν (Hugging Face Spaces λ°°ν¬ κ°λ₯)
|
156 |
demo.launch()
|