Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,99 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import re
|
| 4 |
-
import io
|
| 5 |
import openpyxl
|
| 6 |
from openpyxl import Workbook
|
|
|
|
| 7 |
import tempfile
|
| 8 |
|
| 9 |
-
def remove_special_characters(text: str):
|
| 10 |
-
return re.sub(r"[^\w\sใฑ-ํฃ]", "", text)
|
| 11 |
-
|
| 12 |
-
def extract_keywords(text_list, top_n=3):
|
| 13 |
-
combined_text = " ".join(text_list)
|
| 14 |
-
combined_text = remove_special_characters(combined_text)
|
| 15 |
-
words = combined_text.split()
|
| 16 |
-
freq = {}
|
| 17 |
-
for w in words:
|
| 18 |
-
freq[w] = freq.get(w, 0) + 1
|
| 19 |
-
sorted_words = sorted(freq.items(), key=lambda x: x[1], reverse=True)
|
| 20 |
-
keywords = [w[0] for w in sorted_words[:top_n]]
|
| 21 |
-
return ", ".join(keywords)
|
| 22 |
-
|
| 23 |
def process_excel(file):
|
| 24 |
-
# ์์
์ฝ๊ธฐ
|
|
|
|
| 25 |
df = pd.read_excel(file.name, header=None)
|
| 26 |
-
df.columns = ["๊ณ ๊ฐID", "๋ฆฌ๋ทฐ๋ ์ง", "๋ฆฌ๋ทฐ๋ด์ฉ", "๋ฆฌ๋ทฐ์ ์"] + list(df.columns[4:])
|
| 27 |
-
df["๋ฆฌ๋ทฐ๋ด์ฉ"] = df["๋ฆฌ๋ทฐ๋ด์ฉ"].astype(str).apply(remove_special_characters)
|
| 28 |
-
df_use = df[["๊ณ ๊ฐID", "๋ฆฌ๋ทฐ๋ ์ง", "๋ฆฌ๋ทฐ๋ด์ฉ", "๋ฆฌ๋ทฐ์ ์"]]
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
score_mean = df_use["๋ฆฌ๋ทฐ์ ์"].mean()
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
wb = Workbook()
|
| 40 |
ws = wb.active
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
ws.cell(row=i, column=
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
output = io.BytesIO()
|
| 59 |
wb.save(output)
|
| 60 |
output.seek(0)
|
| 61 |
|
| 62 |
-
# ์ฌ๊ธฐ์ dict๋ก ๋ฐํํ์ง ์๊ณ , ์์ ํ์ผ์ ์๋๋ค.
|
| 63 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
| 64 |
tmp.write(output.getvalue())
|
| 65 |
tmp_path = tmp.name
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
def create_app():
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
-
gr.Markdown("##
|
| 72 |
|
| 73 |
file_input = gr.File(label="์์
ํ์ผ ์
๋ก๋", file_types=[".xlsx"])
|
| 74 |
generate_button = gr.Button("๋ฐ์ดํฐ ์ถ์ถ ๋ฐ ๊ฒฐ๊ณผ ์์ฑ")
|
| 75 |
|
| 76 |
-
output_df = gr.DataFrame(label="
|
| 77 |
download_file = gr.File(label="๊ฒฐ๊ณผ ์์
๋ค์ด๋ก๋")
|
| 78 |
|
| 79 |
def on_click(file):
|
| 80 |
df_preview, tmp_path = process_excel(file)
|
| 81 |
-
# ์ฒซ ๋ฒ์งธ ์ถ๋ ฅ: DataFrame ๋ฏธ๋ฆฌ๋ณด๊ธฐ
|
| 82 |
-
# ๋ ๋ฒ์งธ ์ถ๋ ฅ: ์ค์ ํ์ผ ๊ฒฝ๋ก(str)๋ฅผ ๋ฐํ
|
| 83 |
return df_preview, tmp_path
|
| 84 |
|
| 85 |
generate_button.click(
|
|
@@ -93,3 +107,4 @@ def create_app():
|
|
| 93 |
if __name__ == "__main__":
|
| 94 |
app = create_app()
|
| 95 |
app.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
| 3 |
import openpyxl
|
| 4 |
from openpyxl import Workbook
|
| 5 |
+
import io
|
| 6 |
import tempfile
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def process_excel(file):
|
| 9 |
+
# ----- (1) ์์
ํ์ผ ์ฝ๊ธฐ -----
|
| 10 |
+
# header=None ์ ํตํด ํค๋๋ฅผ ๋ฌด์ํ๊ณ , ๋ชจ๋ ์
์ DataFrame์ผ๋ก ์ฝ์ต๋๋ค.
|
| 11 |
df = pd.read_excel(file.name, header=None)
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# ----- (2) ํน์ ๋ฒ์์ ์
๋ด์ฉ ์ถ์ถ -----
|
| 14 |
+
# A1~K1 => ์์
์์ผ๋ก๋ ์ฒซ ๋ฒ์งธ ํ(row=0), ์ปฌ๋ผ A~K๋ 0~10๋ฒ ์ปฌ๋ผ
|
| 15 |
+
row1 = df.iloc[0, 0:11].fillna('').tolist()
|
| 16 |
+
|
| 17 |
+
# A2~A21 => ์์
์์ผ๋ก๋ ๋ ๋ฒ์งธ ํ(row=1)๋ถํฐ 21๋ฒ์งธ ํ(row=20)๊น์ง, ์ปฌ๋ผ A=0
|
| 18 |
+
colA = df.iloc[1:21, 0].fillna('').tolist()
|
| 19 |
|
| 20 |
+
# B2~B21 => ์ปฌ๋ผ B=1
|
| 21 |
+
colB = df.iloc[1:21, 1].fillna('').tolist()
|
|
|
|
| 22 |
|
| 23 |
+
# C2~C21 => ์ปฌ๋ผ C=2
|
| 24 |
+
colC = df.iloc[1:21, 2].fillna('').tolist()
|
| 25 |
+
|
| 26 |
+
# D2~D21 => ์ปฌ๋ผ D=3
|
| 27 |
+
colD = df.iloc[1:21, 3].fillna('').tolist()
|
| 28 |
+
|
| 29 |
+
# E2~E21 => ์ปฌ๋ผ E=4
|
| 30 |
+
colE = df.iloc[1:21, 4].fillna('').tolist()
|
| 31 |
+
|
| 32 |
+
# ----- (3) ์๋ก์ด Workbook์ ๊ฐ ์ฐ๊ธฐ -----
|
| 33 |
wb = Workbook()
|
| 34 |
ws = wb.active
|
| 35 |
+
|
| 36 |
+
# 3-1) A1~K1 ๋ด์ฉ (์์
1ํ์ ์์๋๋ก ์ฐ๊ธฐ)
|
| 37 |
+
for col_idx, val in enumerate(row1, start=1):
|
| 38 |
+
ws.cell(row=1, column=col_idx, value=val)
|
| 39 |
+
|
| 40 |
+
# 3-2) A2~A21 ๋ด์ฉ (์์
2ํ๋ถํฐ 21ํ๊น์ง A์ด)
|
| 41 |
+
for i, val in enumerate(colA, start=2):
|
| 42 |
+
ws.cell(row=i, column=1, value=val)
|
| 43 |
+
|
| 44 |
+
# 3-3) B2~B21 ๋ด์ฉ (์์
2ํ๋ถํฐ 21ํ๊น์ง B์ด)
|
| 45 |
+
for i, val in enumerate(colB, start=2):
|
| 46 |
+
ws.cell(row=i, column=2, value=val)
|
| 47 |
+
|
| 48 |
+
# 3-4) C2~C21 ๋ด์ฉ
|
| 49 |
+
for i, val in enumerate(colC, start=2):
|
| 50 |
+
ws.cell(row=i, column=3, value=val)
|
| 51 |
+
|
| 52 |
+
# 3-5) D2~D21 ๋ด์ฉ
|
| 53 |
+
for i, val in enumerate(colD, start=2):
|
| 54 |
+
ws.cell(row=i, column=4, value=val)
|
| 55 |
+
|
| 56 |
+
# 3-6) E2~E21 ๋ด์ฉ
|
| 57 |
+
for i, val in enumerate(colE, start=2):
|
| 58 |
+
ws.cell(row=i, column=5, value=val)
|
| 59 |
+
|
| 60 |
+
# ----- (4) ๋ฏธ๋ฆฌ๋ณด๊ธฐ์ฉ DataFrame ๊ตฌ์ฑ -----
|
| 61 |
+
# ์ค์ ๋ก๋ ์ฌ์ฉ์๊ฐ ์ํ๋ ํํ๋๋ก ๊ตฌ์ฑํ๋ฉด ๋ฉ๋๋ค.
|
| 62 |
+
# ์ฌ๊ธฐ์๋ ๋จ์ํ ์ถ์ถ ๋ฐ์ดํฐ๋ค์ ํฉ์ณ์ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ํํ๋ก ๋ณด์ฌ์ฃผ๋ ์์์
๋๋ค.
|
| 63 |
+
preview_data = {
|
| 64 |
+
"A1~K1": [row1], # ๋ฆฌ์คํธ ํ ์ค
|
| 65 |
+
"A2~A21": [colA], # ๋ฆฌ์คํธ ํ ์ค
|
| 66 |
+
"B2~B21": [colB],
|
| 67 |
+
"C2~C21": [colC],
|
| 68 |
+
"D2~D21": [colD],
|
| 69 |
+
"E2~E21": [colE],
|
| 70 |
+
}
|
| 71 |
+
df_preview = pd.DataFrame.from_dict(preview_data, orient='index').T
|
| 72 |
+
|
| 73 |
+
# ----- (5) ๊ฒฐ๊ณผ๋ฅผ ์์ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ ํ, ์์ ํ์ผ๋ก ์ฐ๊ธฐ -----
|
| 74 |
output = io.BytesIO()
|
| 75 |
wb.save(output)
|
| 76 |
output.seek(0)
|
| 77 |
|
|
|
|
| 78 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp:
|
| 79 |
tmp.write(output.getvalue())
|
| 80 |
tmp_path = tmp.name
|
| 81 |
|
| 82 |
+
# Gradio๋ก ๋ฐํ: (1) ๋ฏธ๋ฆฌ๋ณด๊ธฐ DataFrame, (2) ์ค์ ํ์ผ ๊ฒฝ๋ก
|
| 83 |
+
return df_preview, tmp_path
|
| 84 |
|
| 85 |
def create_app():
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
+
gr.Markdown("## ํน์ ๋ฒ์์ ์
๋ด์ฉ ์ถ์ถ ๋ฐ๋ชจ")
|
| 88 |
|
| 89 |
file_input = gr.File(label="์์
ํ์ผ ์
๋ก๋", file_types=[".xlsx"])
|
| 90 |
generate_button = gr.Button("๋ฐ์ดํฐ ์ถ์ถ ๋ฐ ๊ฒฐ๊ณผ ์์ฑ")
|
| 91 |
|
| 92 |
+
output_df = gr.DataFrame(label="์ถ์ถ ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
|
| 93 |
download_file = gr.File(label="๊ฒฐ๊ณผ ์์
๋ค์ด๋ก๋")
|
| 94 |
|
| 95 |
def on_click(file):
|
| 96 |
df_preview, tmp_path = process_excel(file)
|
|
|
|
|
|
|
| 97 |
return df_preview, tmp_path
|
| 98 |
|
| 99 |
generate_button.click(
|
|
|
|
| 107 |
if __name__ == "__main__":
|
| 108 |
app = create_app()
|
| 109 |
app.launch()
|
| 110 |
+
|