Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import re
|
| 4 |
+
from collections import Counter
|
| 5 |
+
from openpyxl import Workbook
|
| 6 |
+
from openpyxl.chart import BarChart, Reference
|
| 7 |
+
|
| 8 |
+
def process_excel(file):
|
| 9 |
+
# 엑셀 파일 읽기
|
| 10 |
+
df = pd.read_excel(file)
|
| 11 |
+
|
| 12 |
+
# D열(D4부터)의 상품명 데이터 가져오기
|
| 13 |
+
data = df.iloc[3:, 3].dropna().astype(str) # D4부터 D열 전체 가져오기 (4번째 행부터 시작)
|
| 14 |
+
|
| 15 |
+
# 키워드 추출 및 처리
|
| 16 |
+
keyword_list = []
|
| 17 |
+
for item in data:
|
| 18 |
+
keywords = re.findall(r'\b\w+\b', item) # 특수문자 제거하고 키워드 추출
|
| 19 |
+
keywords = list(set(keywords)) # 중복 제거
|
| 20 |
+
keyword_list.extend(keywords)
|
| 21 |
+
|
| 22 |
+
# 키워드 빈도수 계산
|
| 23 |
+
keyword_count = Counter(keyword_list)
|
| 24 |
+
|
| 25 |
+
# 결과를 데이터프레임으로 변환
|
| 26 |
+
result_df = pd.DataFrame(keyword_count.items(), columns=['키워드', '빈도']).sort_values(by='빈도', ascending=False).reset_index(drop=True)
|
| 27 |
+
|
| 28 |
+
# A4와 B4 셀부터 데이터가 들어가도록 수정
|
| 29 |
+
with pd.ExcelWriter('keyword_result.xlsx', engine='openpyxl') as writer:
|
| 30 |
+
result_df.to_excel(writer, index=False, startrow=3, startcol=0) # A4 셀에 해당하는 3번째 행, 0번째 열부터 시작
|
| 31 |
+
|
| 32 |
+
# 워크북 및 시트 가져오기
|
| 33 |
+
workbook =
|
| 34 |
+
sheet = writer.sheets['Sheet1']
|
| 35 |
+
|
| 36 |
+
# 차트 생성
|
| 37 |
+
chart = BarChart()
|
| 38 |
+
data = Reference(sheet, min_col=2, min_row=4, max_row=3 + len(result_df), max_col=2)
|
| 39 |
+
categories = Reference(sheet, min_col=1, min_row=4, max_row=3 + len(result_df))
|
| 40 |
+
chart.add_data(data, titles_from_data=True)
|
| 41 |
+
chart.set_categories(categories)
|
| 42 |
+
chart.title = "키워드 빈도수"
|
| 43 |
+
chart.x_axis.title = "키워드"
|
| 44 |
+
chart.y_axis.title = "빈도"
|
| 45 |
+
|
| 46 |
+
# 차트를 시트에 추가
|
| 47 |
+
sheet.add_chart(chart, "E4") # E4 셀에 차트를 추가
|
| 48 |
+
|
| 49 |
+
return 'keyword_result.xlsx'
|
| 50 |
+
|
| 51 |
+
# Gradio 인터페이스 생성
|
| 52 |
+
interface = gr.Interface(
|
| 53 |
+
fn=process_excel,
|
| 54 |
+
inputs=gr.File(label="엑셀 파일 업로드"),
|
| 55 |
+
outputs=gr.File(label="분석 결과 파일")
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
interface.launch()
|