Upload 2 files
Browse files- app (2).py +95 -0
- requirements (1).txt +4 -0
app (2).py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
all_ids = df_use["고객ID"].dropna().unique()
|
| 31 |
+
combined_id = ", ".join(map(str, all_ids))
|
| 32 |
+
all_dates = df_use["리뷰날짜"].dropna().unique()
|
| 33 |
+
combined_dates = ", ".join(map(str, sorted(all_dates)))
|
| 34 |
+
|
| 35 |
+
keywords = extract_keywords(df_use["리뷰내용"].tolist())
|
| 36 |
+
df_use["리뷰점수"] = pd.to_numeric(df_use["리뷰점수"], errors='coerce')
|
| 37 |
+
score_mean = df_use["리뷰점수"].mean()
|
| 38 |
+
|
| 39 |
+
wb = Workbook()
|
| 40 |
+
ws = wb.active
|
| 41 |
+
ws["A1"] = "고객ID"
|
| 42 |
+
ws["B1"] = "리뷰날짜"
|
| 43 |
+
ws["C1"] = "리뷰내용"
|
| 44 |
+
ws["D1"] = "리뷰점수"
|
| 45 |
+
|
| 46 |
+
for i, row in enumerate(df_use.values, start=2):
|
| 47 |
+
ws.cell(row=i, column=1, value=row[0])
|
| 48 |
+
ws.cell(row=i, column=2, value=row[1])
|
| 49 |
+
ws.cell(row=i, column=3, value=row[2])
|
| 50 |
+
ws.cell(row=i, column=4, value=row[3])
|
| 51 |
+
|
| 52 |
+
ws["A8"] = combined_id
|
| 53 |
+
ws["B8"] = combined_dates
|
| 54 |
+
ws["C8"] = keywords
|
| 55 |
+
ws["D8"] = score_mean
|
| 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 |
+
return df_use, tmp_path
|
| 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(
|
| 86 |
+
fn=on_click,
|
| 87 |
+
inputs=file_input,
|
| 88 |
+
outputs=[output_df, download_file]
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
return demo
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
app = create_app()
|
| 95 |
+
app.launch()
|
requirements (1).txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
openpyxl
|
| 4 |
+
xlrd
|