Update app.py
Browse files
app.py
CHANGED
@@ -3,181 +3,164 @@ import pandas as pd
|
|
3 |
import tempfile
|
4 |
import re
|
5 |
|
6 |
-
########################################
|
7 |
-
# 1) ๋ณธ๋ฌธ ์ ์ฒ๋ฆฌ: ํ๊ธ๋ง ๋จ๊ธฐ๊ณ ์ ๊ฑฐ #
|
8 |
-
########################################
|
9 |
def preprocess_text(text: str) -> str:
|
10 |
"""
|
11 |
-
์ผํ, ๋ง์นจํ, ๊ณต๋ฐฑ, ์ซ์, ์์ด ๋ฑ
|
12 |
-
ํ๊ธ(๊ฐ-ํฃ) ์ด์ธ์
|
13 |
ํ๊ธ๋ง ์ฐ์์ผ๋ก ๋จ๊ธด๋ค.
|
14 |
"""
|
15 |
return re.sub(r'[^๊ฐ-ํฃ]', '', text)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
########################################
|
19 |
-
# 2) ๋ฉ์ธ ํจ์
|
20 |
-
########################################
|
21 |
def count_keywords(main_text, excel_file, direct_input):
|
22 |
"""
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
3) ์นด์ดํธ ๋ก์ง:
|
31 |
-
- .count(ํค์๋)
|
32 |
-
- 1ํ ์ด์๋ง ๊ฒฐ๊ณผ ํ(Markdown)์ ํ์
|
33 |
-
4) ์์
๊ฒฐ๊ณผ:
|
34 |
-
- ์ง์ ์
๋ ฅ์ผ ๊ฒฝ์ฐ: A์ด=ํค์๋, B์ด=์นด์ดํธ
|
35 |
-
- ์์
์
๋ก๋์ผ ๊ฒฝ์ฐ:
|
36 |
-
* ๊ธฐ์กด ๋ฐ์ดํฐํ๋ ์ ์ฌ์ฉ
|
37 |
-
* A5~A10000 ์ฝ์ด ํค์๋
|
38 |
-
* N5~N10000์ ์นด์ดํธ
|
39 |
"""
|
40 |
-
#
|
41 |
cleaned_text = preprocess_text(main_text)
|
42 |
|
43 |
-
|
44 |
-
# ์ง์ ์
๋ ฅ vs ์์
ํ์ผ ์ฐ์ ์์
|
45 |
-
####################################
|
46 |
-
direct_input = direct_input.strip() # ์๋ค ๊ณต๋ฐฑ ์ ๊ฑฐ
|
47 |
-
used_excel = False # ์์
์ ์ฌ์ฉํ๋์ง ์ฌ๋ถ
|
48 |
-
|
49 |
-
# ํค์๋ ๋ชฉ๋ก๊ณผ ์นด์ดํธ ๊ธฐ๋ก์ ๋ด์ df
|
50 |
-
df = None
|
51 |
-
|
52 |
-
# CASE A) ์ง์ ์
๋ ฅ์ด ์์ผ๋ฉด โ ์ฐ์ ์ฌ์ฉ
|
53 |
if direct_input:
|
54 |
-
#
|
55 |
keywords = [kw.strip() for kw in direct_input.split('\n') if kw.strip()]
|
56 |
if not keywords:
|
57 |
-
return ("์ง์ ์
๋ ฅ ํค์๋๊ฐ
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
else:
|
|
|
63 |
if not excel_file:
|
64 |
-
return ("ํค์๋๋ฅผ ์ง์
|
65 |
-
|
66 |
-
# ์์
|
67 |
-
|
68 |
-
|
69 |
-
#
|
70 |
-
max_row = min(
|
71 |
-
sub_df =
|
72 |
-
|
73 |
-
|
|
|
74 |
if not keywords:
|
75 |
return ("A5~A10000 ๋ฒ์์ ํค์๋๊ฐ ์์ต๋๋ค.", None)
|
76 |
-
|
77 |
-
#
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
# 2) ์ค์ ์นด์ดํ
|
88 |
-
counts = [cleaned_text.count(kw) for kw in keywords]
|
89 |
-
|
90 |
-
# 3) 1ํ ์ด์์ธ ํค์๋๋ง ๊ฒฐ๊ณผ ํ(Markdown)์ ํ์
|
91 |
-
filtered = []
|
92 |
-
for kw, cnt in zip(keywords, counts):
|
93 |
-
if cnt > 0:
|
94 |
-
filtered.append((kw, cnt))
|
95 |
-
|
96 |
-
if not filtered:
|
97 |
-
msg_no_keywords = "๋ณธ๋ฌธ์ ํด๋น ํค์๋๊ฐ ์ ํ ๋ฑ์ฅํ์ง ์์์ต๋๋ค (0ํ)."
|
98 |
-
# ๊ทธ๋๋ ์์
ํ์ผ์ ๋ง๋ค์ด์ฃผ์ด์ผ ํจ
|
99 |
-
# CASE A) ์ง์ ์
๋ ฅ
|
100 |
-
if not used_excel:
|
101 |
-
# ์ df(A,B)
|
102 |
-
out_df = pd.DataFrame({"A": keywords, "B": counts})
|
103 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
104 |
-
out_df.to_excel(tmp.name, index=False)
|
105 |
-
tmp_path = tmp.name
|
106 |
-
return (msg_no_keywords, tmp_path)
|
107 |
-
else:
|
108 |
-
# CASE B) ์์
|
109 |
-
# N์ด์ counts ๊ธฐ๋ก (A5~A10000 โ N5~N10000)
|
110 |
for i, cnt_val in enumerate(counts):
|
111 |
-
row_idx = 4 + i
|
112 |
if row_idx < df.shape[0]:
|
113 |
-
df.
|
114 |
-
|
115 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
116 |
-
df.to_excel(tmp.name, index=False)
|
117 |
tmp_path = tmp.name
|
118 |
-
return (
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
if not used_excel:
|
129 |
-
# ์ง์ ์
๋ ฅ:
|
130 |
-
# A์ด=ํค์๋, B์ด=์นด์ดํธ
|
131 |
-
out_df = pd.DataFrame({"A": keywords, "B": counts})
|
132 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
133 |
-
out_df.to_excel(tmp.name, index=False)
|
134 |
-
tmp_path = tmp.name
|
135 |
-
return (result_md, tmp_path)
|
136 |
-
else:
|
137 |
-
# ์
๋ก๋ ์์
์ N์ด=์นด์ดํธ
|
138 |
for i, cnt_val in enumerate(counts):
|
139 |
-
row_idx = 4 + i
|
140 |
if row_idx < df.shape[0]:
|
141 |
-
df.
|
142 |
-
|
143 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
144 |
-
df.to_excel(tmp.name, index=False)
|
145 |
tmp_path = tmp.name
|
146 |
-
|
|
|
147 |
|
148 |
|
149 |
-
|
150 |
-
#
|
151 |
-
|
152 |
with gr.Blocks() as demo:
|
153 |
-
gr.Markdown("## ๋ณธ๋ฌธ & ํค์๋ ๋ถ์ (
|
154 |
|
155 |
with gr.Row():
|
156 |
with gr.Column():
|
157 |
-
|
158 |
label="๋ณธ๋ฌธ ํ
์คํธ",
|
159 |
-
|
160 |
-
|
161 |
)
|
162 |
with gr.Column():
|
163 |
-
|
164 |
-
label="์ง์ ์
๋ ฅ ํค์๋
|
165 |
-
|
166 |
-
|
167 |
)
|
168 |
-
|
169 |
-
label="์์
์
๋ก๋(A5~A10000
|
170 |
file_types=[".xlsx"]
|
171 |
)
|
172 |
-
|
173 |
|
174 |
-
output_md = gr.Markdown(label="๊ฒฐ๊ณผ
|
175 |
-
output_file = gr.File(label="์์
๋ค์ด๋ก๋
|
176 |
|
177 |
-
|
178 |
-
submit_btn.click(
|
179 |
fn=count_keywords,
|
180 |
-
inputs=[
|
181 |
outputs=[output_md, output_file]
|
182 |
)
|
183 |
|
|
|
3 |
import tempfile
|
4 |
import re
|
5 |
|
|
|
|
|
|
|
6 |
def preprocess_text(text: str) -> str:
|
7 |
"""
|
8 |
+
์ผํ, ๋ง์นจํ, ๊ณต๋ฐฑ, ์ซ์, ์์ด ๋ฑ
|
9 |
+
ํ๊ธ(๊ฐ-ํฃ) ์ด์ธ์ ๋ฌธ์๋ฅผ ๋ชจ๋ ์ ๊ฑฐํ๊ณ
|
10 |
ํ๊ธ๋ง ์ฐ์์ผ๋ก ๋จ๊ธด๋ค.
|
11 |
"""
|
12 |
return re.sub(r'[^๊ฐ-ํฃ]', '', text)
|
13 |
|
14 |
+
def expand_columns_if_needed(df, needed_index: int):
|
15 |
+
"""
|
16 |
+
df์ (needed_index + 1)๋ฒ์งธ ์ด์ด ์กด์ฌํ์ง ์์ผ๋ฉด
|
17 |
+
์์๋ก ํ์ฅํด์ ๋น ์ด์ ๋ง๋ ๋ค.
|
18 |
+
์) needed_index=13 โ N์ด(14๋ฒ์งธ ์ด)์ ์ฐ๋ ค๋ฉด
|
19 |
+
df.shape[1]์ด 14 ์ด์์ด ๋๋๋ก ํ์ฅ
|
20 |
+
"""
|
21 |
+
while df.shape[1] <= needed_index:
|
22 |
+
# ๋งจ ๋์ ๋น ์ด ์ถ๊ฐ
|
23 |
+
df[df.shape[1]] = None
|
24 |
+
|
25 |
|
|
|
|
|
|
|
26 |
def count_keywords(main_text, excel_file, direct_input):
|
27 |
"""
|
28 |
+
- ์ง์ ์
๋ ฅ ํค์๋(์ค๋ฐ๊ฟ ๊ตฌ๋ถ)๊ฐ ์์ผ๋ฉด ์ฐ์ ์ฌ์ฉ(A์ด=ํค์๋, B์ด=์นด์ดํธ)
|
29 |
+
- ์์ผ๋ฉด ์์
์ฌ์ฉ:
|
30 |
+
* ํค๋๋ฅผ ์ฌ์ฉํ์ง ์์(header=None) โ 1ํ ๊ทธ๋๋ก ๋ณด์กด
|
31 |
+
* A5~A10000: ํค์๋
|
32 |
+
* N5~N10000: ์นด์ดํธ ๊ธฐ๋ก(์ด ์ธ๋ฑ์ค 13)
|
33 |
+
- ๋ณธ๋ฌธ์ ํ๊ธ๋ง ๋จ๊ธฐ๊ณ .count(ํค์๋)๋ก ๋ฑ์ฅ ํ์๋ฅผ ๊ณ์ฐ
|
34 |
+
- 1ํ ์ด์์ธ ํค์๋๋ง ๊ฒฐ๊ณผ ํ(Markdown)์ ํ์
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
"""
|
36 |
+
# ๋ณธ๋ฌธ ์ ์ฒ๋ฆฌ
|
37 |
cleaned_text = preprocess_text(main_text)
|
38 |
|
39 |
+
direct_input = direct_input.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
if direct_input:
|
41 |
+
# ===== ์ง์ ์
๋ ฅ ํค์๋ ์ฌ์ฉ =====
|
42 |
keywords = [kw.strip() for kw in direct_input.split('\n') if kw.strip()]
|
43 |
if not keywords:
|
44 |
+
return ("์ง์ ์
๋ ฅ ํค์๋๊ฐ ์์ต๋๋ค.", None)
|
45 |
+
|
46 |
+
# counts
|
47 |
+
counts = [cleaned_text.count(k) for k in keywords]
|
48 |
+
|
49 |
+
# 1ํ ์ด์ ํํฐ
|
50 |
+
filtered = [(k, c) for k, c in zip(keywords, counts) if c > 0]
|
51 |
+
|
52 |
+
if not filtered:
|
53 |
+
# ์ ๋ถ 0ํ
|
54 |
+
msg = "๋ณธ๋ฌธ์ ํด๋น ํค์๋๊ฐ ์ ํ ๋ฑ์ฅํ์ง ์์์ต๋๋ค."
|
55 |
+
# ๊ทธ๋๋ ๊ฒฐ๊ณผ ์์
(A,B) ๋ง๋ค์ด์ ๋ฐํ
|
56 |
+
tmp_df = pd.DataFrame({"A": keywords, "B": counts})
|
57 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
58 |
+
tmp_df.to_excel(tmp.name, index=False, header=False)
|
59 |
+
# header=False โ 1ํ์ "A,B" ๊ฐ์ ์ด์ด๋ฆ ์ ์ฐ๋๋ก
|
60 |
+
tmp_path = tmp.name
|
61 |
+
return (msg, tmp_path)
|
62 |
+
|
63 |
+
# 1ํ ์ด์ ํ(Markdown)
|
64 |
+
lines = ["| ํค์๋ | ๋ฑ์ฅ ํ์ |", "|---|---|"]
|
65 |
+
for (k, c) in filtered:
|
66 |
+
lines.append(f"| {k} | {c} |")
|
67 |
+
md_table = "\n".join(lines)
|
68 |
+
|
69 |
+
# ์์
(A,B) ์ ์ฅ
|
70 |
+
tmp_df = pd.DataFrame({"A": keywords, "B": counts})
|
71 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
72 |
+
tmp_df.to_excel(tmp.name, index=False, header=False)
|
73 |
+
tmp_path = tmp.name
|
74 |
+
|
75 |
+
return (md_table, tmp_path)
|
76 |
+
|
77 |
else:
|
78 |
+
# ===== ์์
ํ์ผ ์ฌ์ฉ =====
|
79 |
if not excel_file:
|
80 |
+
return ("์์
ํ์ผ์ ์
๋ก๋ํ๊ฑฐ๋ ํค์๋๋ฅผ ์ง์ ์
๋ ฅํ์ธ์.", None)
|
81 |
+
|
82 |
+
# 1) ์์
์ ์ฒด๋ฅผ header=None๋ก ์ฝ์ โ 1ํ ๊ทธ๋๋ก ๋ณด์กด
|
83 |
+
df = pd.read_excel(excel_file.name, header=None)
|
84 |
+
|
85 |
+
# 2) A5~A10000 โ (์ธ๋ฑ์ค 4~9999) ํค์๋
|
86 |
+
max_row = min(df.shape[0], 10000) # ์ค์ ํ ๊ฐ์ vs 10000 ์ค ๋ ์์ ๊ฒ
|
87 |
+
sub_df = df.iloc[4:max_row, 0] # ์ฒซ ๋ฒ์งธ ์ด(์ธ๋ฑ์ค=0)
|
88 |
+
|
89 |
+
# strip + NaN ์ ๊ฑฐ
|
90 |
+
keywords = sub_df.dropna().astype(str).apply(lambda x: x.strip()).tolist()
|
91 |
if not keywords:
|
92 |
return ("A5~A10000 ๋ฒ์์ ํค์๋๊ฐ ์์ต๋๋ค.", None)
|
93 |
+
|
94 |
+
# counts
|
95 |
+
counts = [cleaned_text.count(k) for k in keywords]
|
96 |
+
|
97 |
+
# 1ํ ์ด์ ํํฐ
|
98 |
+
filtered = [(k, c) for k, c in zip(keywords, counts) if c > 0]
|
99 |
+
if not filtered:
|
100 |
+
msg = "๋ณธ๋ฌธ์ ํด๋น ํค์๋๊ฐ ์ ํ ๋ฑ์ฅํ์ง ์์์ต๋๋ค(0ํ)."
|
101 |
+
# ๊ทธ๋๋ N5~N10000์ ๊ธฐ๋ก
|
102 |
+
expand_columns_if_needed(df, 13) # N์ด=13
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
for i, cnt_val in enumerate(counts):
|
104 |
+
row_idx = 4 + i
|
105 |
if row_idx < df.shape[0]:
|
106 |
+
df.iloc[row_idx, 13] = cnt_val
|
107 |
+
|
108 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
109 |
+
df.to_excel(tmp.name, index=False, header=False)
|
110 |
tmp_path = tmp.name
|
111 |
+
return (msg, tmp_path)
|
112 |
+
|
113 |
+
# 1ํ ์ด์ ํ(Markdown)
|
114 |
+
lines = ["| ํค์๋ | ๋ฑ์ฅ ํ์ |", "|---|---|"]
|
115 |
+
for (k, c) in filtered:
|
116 |
+
lines.append(f"| {k} | {c} |")
|
117 |
+
md_table = "\n".join(lines)
|
118 |
+
|
119 |
+
# N5~N10000์ ๊ธฐ๋ก
|
120 |
+
expand_columns_if_needed(df, 13) # ์ด์ด 14๊ฐ ๋ฏธ๋ง์ด๋ฉด N์ด(13)๊น์ง ํ์ฅ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
for i, cnt_val in enumerate(counts):
|
122 |
+
row_idx = 4 + i
|
123 |
if row_idx < df.shape[0]:
|
124 |
+
df.iloc[row_idx, 13] = cnt_val
|
125 |
+
|
126 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
127 |
+
df.to_excel(tmp.name, index=False, header=False)
|
128 |
tmp_path = tmp.name
|
129 |
+
|
130 |
+
return (md_table, tmp_path)
|
131 |
|
132 |
|
133 |
+
########################
|
134 |
+
# Gradio ์ธํฐํ์ด์ค #
|
135 |
+
########################
|
136 |
with gr.Blocks() as demo:
|
137 |
+
gr.Markdown("## ๋ณธ๋ฌธ & ํค์๋ ๋ถ์ - (A5~A10000, N5~N10000)")
|
138 |
|
139 |
with gr.Row():
|
140 |
with gr.Column():
|
141 |
+
main_textbox = gr.Textbox(
|
142 |
label="๋ณธ๋ฌธ ํ
์คํธ",
|
143 |
+
lines=16,
|
144 |
+
placeholder="์ฌ๊ธฐ์ ๊ธด ๋ณธ๋ฌธ์ ๋ถ์ฌ๋ฃ์ผ์ธ์. ํ๊ธ๋ง ๋จ๊ธฐ๊ณ ๋๋จธ์ง๋ ์ ๊ฑฐ๋ฉ๋๋ค."
|
145 |
)
|
146 |
with gr.Column():
|
147 |
+
keyword_input = gr.Textbox(
|
148 |
+
label="(์ ํ) ์ง์ ์
๋ ฅ ํค์๋ - ์ํฐ๋ก ๊ตฌ๋ถ",
|
149 |
+
lines=6,
|
150 |
+
placeholder="์)\n์ด์ํ๊ฐ์ต๊ธฐ\n๊ฐ์ต๊ธฐ\n..."
|
151 |
)
|
152 |
+
excel_input = gr.File(
|
153 |
+
label="(์ ํ) ์์
์
๋ก๋ (A5~A10000=ํค์๋, N5~N10000=์นด์ดํธ)",
|
154 |
file_types=[".xlsx"]
|
155 |
)
|
156 |
+
run_button = gr.Button("์นด์ดํธํ๊ธฐ")
|
157 |
|
158 |
+
output_md = gr.Markdown(label="๊ฒฐ๊ณผ ํ")
|
159 |
+
output_file = gr.File(label="๊ฒฐ๊ณผ ์์
๋ค์ด๋ก๋")
|
160 |
|
161 |
+
run_button.click(
|
|
|
162 |
fn=count_keywords,
|
163 |
+
inputs=[main_textbox, excel_input, keyword_input],
|
164 |
outputs=[output_md, output_file]
|
165 |
)
|
166 |
|