Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,40 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import re
|
4 |
from io import BytesIO
|
|
|
5 |
|
6 |
def process_excel(file_bytes):
|
7 |
"""
|
8 |
μ
λ‘λλ μμ
νμΌμμ D4μ
λΆν° Dμ΄μ μνλͺ
μ μΆμΆνμ¬,
|
9 |
κ° μ
μμ νΉμλ¬Έμλ₯Ό μ κ±°ν ν 곡백 κΈ°μ€μΌλ‘ ν€μλλ₯Ό μΆμΆν©λλ€.
|
10 |
ν μ
λ΄μμ μ€λ³΅λ ν€μλλ ν λ²λ§ μΉ΄μ΄νΈνκ³ , μ 체 μ
μ λν΄
|
11 |
-
ν€μλμ λΉλλ₯Ό κ³μ°νμ¬ μμ
|
|
|
12 |
"""
|
|
|
13 |
try:
|
14 |
-
# μ
λ‘λλ νμΌμ μ½μ΄μ΅λλ€. ν€λκ° μλ μνλ‘ μ 체 λ°μ΄ν°λ₯Ό λΆλ¬μ΅λλ€.
|
15 |
df = pd.read_excel(BytesIO(file_bytes), header=None, engine="openpyxl")
|
16 |
except Exception as e:
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
# μμ
|
20 |
if df.shape[1] < 4 or df.shape[0] < 4:
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
# Dμ΄(4λ²μ§Έ μ΄, μΈλ±μ€ 3)μμ 4ν(μΈλ±μ€ 3)λΆν° λ°μ΄ν°λ₯Ό
|
24 |
product_names_series = df.iloc[3:, 3]
|
25 |
product_names_series = product_names_series.dropna()
|
26 |
|
@@ -29,49 +43,59 @@ def process_excel(file_bytes):
|
|
29 |
# λ¬Έμμ΄μ΄ μλ κ²½μ° λ¬Έμμ΄λ‘ λ³ν
|
30 |
if not isinstance(cell, str):
|
31 |
cell = str(cell)
|
32 |
-
# νΉμλ¬Έμ μ κ±° (μ«μ, μλ¬Έ, νκΈ, 곡백μ μ μΈν
|
33 |
cleaned = re.sub(r'[^0-9a-zA-Zκ°-ν£\s]', '', cell)
|
34 |
-
# 곡백 κΈ°μ€μΌλ‘
|
35 |
keywords = cleaned.split()
|
36 |
-
# νλμ
|
37 |
unique_keywords = set(keywords)
|
38 |
for keyword in unique_keywords:
|
39 |
keyword_counts[keyword] = keyword_counts.get(keyword, 0) + 1
|
40 |
|
41 |
-
# ν€μλλ₯Ό λΉλ
|
42 |
sorted_keywords = sorted(keyword_counts.items(), key=lambda x: (-x[1], x[0]))
|
43 |
|
44 |
-
# κ²°κ³Όλ₯Ό DataFrameμΌλ‘ μμ± (A1
|
45 |
result_df = pd.DataFrame(sorted_keywords, columns=["ν€μλ", "λΉλ"])
|
46 |
|
47 |
-
# μμ
|
48 |
output = BytesIO()
|
49 |
try:
|
50 |
with pd.ExcelWriter(output, engine="openpyxl") as writer:
|
51 |
-
# ν€λ μμ΄ A2μ
λΆν° λ°μ΄ν° μ μ₯
|
52 |
result_df.to_excel(writer, index=False, startrow=1, header=False)
|
53 |
worksheet = writer.sheets["Sheet1"]
|
54 |
-
# A1μ
κ³Ό B1μ
μ ν€λ μμ±
|
55 |
worksheet.cell(row=1, column=1, value="ν€μλ")
|
56 |
worksheet.cell(row=1, column=2, value="λΉλ")
|
57 |
output.seek(0)
|
58 |
except Exception as e:
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
#
|
62 |
-
|
|
|
|
|
63 |
|
64 |
-
|
|
|
|
|
65 |
iface = gr.Interface(
|
66 |
fn=process_excel,
|
67 |
-
inputs=gr.File(label="μμ
νμΌ μ
λ‘λ"),
|
68 |
-
outputs=
|
|
|
|
|
|
|
69 |
title="μμ
μνλͺ
ν€μλ μΆμΆ λ° λΉλ λΆμ",
|
70 |
description=(
|
71 |
"μμ
νμΌμ D4μ
λΆν° Dμ΄μ μλ μνλͺ
λ°μ΄ν°λ₯Ό λΆμνμ¬, "
|
72 |
"νΉμλ¬Έμλ₯Ό μ κ±°ν ν 곡백 κΈ°μ€μΌλ‘ ν€μλλ₯Ό μΆμΆν©λλ€. "
|
73 |
"ν μ
λ΄μμ μ€λ³΅λ ν€μλλ ν λ²λ§ μΉ΄μ΄νΈνλ©°, μ΅μ’
μ μΌλ‘ ν€μλμ λΉλλ₯Ό "
|
74 |
-
"λ΄λ¦Όμ°¨μμΌλ‘ μ 리ν κ²°κ³Όλ₯Ό μμ
|
75 |
)
|
76 |
)
|
77 |
|
|
|
1 |
+
python
|
2 |
+
볡μ¬
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
import re
|
6 |
from io import BytesIO
|
7 |
+
import tempfile
|
8 |
|
9 |
def process_excel(file_bytes):
|
10 |
"""
|
11 |
μ
λ‘λλ μμ
νμΌμμ D4μ
λΆν° Dμ΄μ μνλͺ
μ μΆμΆνμ¬,
|
12 |
κ° μ
μμ νΉμλ¬Έμλ₯Ό μ κ±°ν ν 곡백 κΈ°μ€μΌλ‘ ν€μλλ₯Ό μΆμΆν©λλ€.
|
13 |
ν μ
λ΄μμ μ€λ³΅λ ν€μλλ ν λ²λ§ μΉ΄μ΄νΈνκ³ , μ 체 μ
μ λν΄
|
14 |
+
ν€μλμ λΉλλ₯Ό κ³μ°νμ¬ μμ
νμΌκ³Ό ν(λ°μ΄ν°νλ μ)λ‘ κ²°κ³Όλ₯Ό μΆλ ₯ν©λλ€.
|
15 |
+
μλ¬ λ°μ μ, μλ¬ λ©μμ§λ₯Ό ν
μ€νΈ νμΌκ³Ό λ°μ΄ν°νλ μ ννλ‘ λ°νν©λλ€.
|
16 |
"""
|
17 |
+
# μμ
νμΌ μ½κΈ°
|
18 |
try:
|
|
|
19 |
df = pd.read_excel(BytesIO(file_bytes), header=None, engine="openpyxl")
|
20 |
except Exception as e:
|
21 |
+
error_message = "μμ
νμΌμ μ½λ μ€ μ€λ₯κ° λ°μνμμ΅λλ€: " + str(e)
|
22 |
+
temp_error = tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="wb")
|
23 |
+
temp_error.write(error_message.encode("utf-8"))
|
24 |
+
temp_error.close()
|
25 |
+
error_df = pd.DataFrame({"μλ¬": [error_message]})
|
26 |
+
return temp_error.name, error_df
|
27 |
|
28 |
+
# μμ
νμΌ νμ μ²΄ν¬ (μ΅μ 4μ΄, μ΅μ 4ν)
|
29 |
if df.shape[1] < 4 or df.shape[0] < 4:
|
30 |
+
error_message = "μμ
νμΌμ νμμ΄ μ¬λ°λ₯΄μ§ μμ΅λλ€."
|
31 |
+
temp_error = tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="wb")
|
32 |
+
temp_error.write(error_message.encode("utf-8"))
|
33 |
+
temp_error.close()
|
34 |
+
error_df = pd.DataFrame({"μλ¬": [error_message]})
|
35 |
+
return temp_error.name, error_df
|
36 |
|
37 |
+
# Dμ΄(4λ²μ§Έ μ΄, μΈλ±μ€ 3)μμ 4ν(μΈλ±μ€ 3)λΆν° λ°μ΄ν°λ₯Ό κ°μ Έμ΄
|
38 |
product_names_series = df.iloc[3:, 3]
|
39 |
product_names_series = product_names_series.dropna()
|
40 |
|
|
|
43 |
# λ¬Έμμ΄μ΄ μλ κ²½μ° λ¬Έμμ΄λ‘ λ³ν
|
44 |
if not isinstance(cell, str):
|
45 |
cell = str(cell)
|
46 |
+
# νΉμλ¬Έμ μ κ±° (μ«μ, μλ¬Έ, νκΈ, 곡백μ μ μΈν λ¬Έμ μ κ±°)
|
47 |
cleaned = re.sub(r'[^0-9a-zA-Zκ°-ν£\s]', '', cell)
|
48 |
+
# 곡백 κΈ°μ€μΌλ‘ ν€μλ λΆλ¦¬
|
49 |
keywords = cleaned.split()
|
50 |
+
# νλμ μ
λ΄ μ€λ³΅ ν€μλλ μ κ±°
|
51 |
unique_keywords = set(keywords)
|
52 |
for keyword in unique_keywords:
|
53 |
keyword_counts[keyword] = keyword_counts.get(keyword, 0) + 1
|
54 |
|
55 |
+
# ν€μλλ₯Ό λΉλ λ΄λ¦Όμ°¨μ(λΉλκ° κ°μΌλ©΄ ν€μλ μ€λ¦μ°¨μ)μΌλ‘ μ λ ¬
|
56 |
sorted_keywords = sorted(keyword_counts.items(), key=lambda x: (-x[1], x[0]))
|
57 |
|
58 |
+
# κ²°κ³Όλ₯Ό DataFrameμΌλ‘ μμ± (A1: "ν€μλ", B1: "λΉλ")
|
59 |
result_df = pd.DataFrame(sorted_keywords, columns=["ν€μλ", "λΉλ"])
|
60 |
|
61 |
+
# μμ
νμΌ μμ± (A1, B1 μ
μ ν€λ μΆκ°)
|
62 |
output = BytesIO()
|
63 |
try:
|
64 |
with pd.ExcelWriter(output, engine="openpyxl") as writer:
|
|
|
65 |
result_df.to_excel(writer, index=False, startrow=1, header=False)
|
66 |
worksheet = writer.sheets["Sheet1"]
|
|
|
67 |
worksheet.cell(row=1, column=1, value="ν€μλ")
|
68 |
worksheet.cell(row=1, column=2, value="λΉλ")
|
69 |
output.seek(0)
|
70 |
except Exception as e:
|
71 |
+
error_message = "μμ
νμΌμ μμ±νλ μ€ μ€λ₯κ° λ°μνμμ΅λλ€: " + str(e)
|
72 |
+
temp_error = tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="wb")
|
73 |
+
temp_error.write(error_message.encode("utf-8"))
|
74 |
+
temp_error.close()
|
75 |
+
error_df = pd.DataFrame({"μλ¬": [error_message]})
|
76 |
+
return temp_error.name, error_df
|
77 |
|
78 |
+
# μμ μμ
νμΌ μμ± ν κ²½λ‘ λ°ν
|
79 |
+
temp_excel = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx", mode="wb")
|
80 |
+
temp_excel.write(output.getvalue())
|
81 |
+
temp_excel.close()
|
82 |
|
83 |
+
return temp_excel.name, result_df
|
84 |
+
|
85 |
+
# κ·ΈλΌλμ€ μΈν°νμ΄μ€ μμ± (μμ
νμΌκ³Ό κ²°κ³Ό ν λ κ°μ§ ννλ‘ μΆλ ₯)
|
86 |
iface = gr.Interface(
|
87 |
fn=process_excel,
|
88 |
+
inputs=gr.File(label="μμ
νμΌ μ
λ‘λ", type="binary"),
|
89 |
+
outputs=[
|
90 |
+
gr.File(label="κ²°κ³Ό μμ
νμΌ"),
|
91 |
+
gr.DataFrame(label="ν€μλ λΆμ ν")
|
92 |
+
],
|
93 |
title="μμ
μνλͺ
ν€μλ μΆμΆ λ° λΉλ λΆμ",
|
94 |
description=(
|
95 |
"μμ
νμΌμ D4μ
λΆν° Dμ΄μ μλ μνλͺ
λ°μ΄ν°λ₯Ό λΆμνμ¬, "
|
96 |
"νΉμλ¬Έμλ₯Ό μ κ±°ν ν 곡백 κΈ°μ€μΌλ‘ ν€μλλ₯Ό μΆμΆν©λλ€. "
|
97 |
"ν μ
λ΄μμ μ€λ³΅λ ν€μλλ ν λ²λ§ μΉ΄μ΄νΈνλ©°, μ΅μ’
μ μΌλ‘ ν€μλμ λΉλλ₯Ό "
|
98 |
+
"λ΄λ¦Όμ°¨μμΌλ‘ μ 리ν κ²°κ³Όλ₯Ό μμ
νμΌκ³Ό ν(λ°μ΄ν°νλ μ)λ‘ μΆλ ₯ν©λλ€."
|
99 |
)
|
100 |
)
|
101 |
|