Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import re | |
| from collections import Counter | |
| from openpyxl import Workbook | |
| from openpyxl.chart import BarChart, Reference | |
| def process_excel(file): | |
| # 엑셀 파일 읽기 | |
| df = pd.read_excel(file) | |
| # D열(D4부터)의 상품명 데이터 가져오기 | |
| data = df.iloc[3:, 3].dropna().astype(str) # D4부터 D열 전체 가져오기 (4번째 행부터 시작) | |
| # 키워드 추출 및 처리 | |
| keyword_list = [] | |
| for item in data: | |
| keywords = re.findall(r'\b\w+\b', item) # 특수문자 제거하고 키워드 추출 | |
| keywords = list(set(keywords)) # 중복 제거 | |
| keyword_list.extend(keywords) | |
| # 키워드 빈도수 계산 | |
| keyword_count = Counter(keyword_list) | |
| # 결과를 데이터프레임으로 변환 | |
| result_df = pd.DataFrame(keyword_count.items(), columns=['키워드', '빈도']).sort_values(by='빈도', ascending=False).reset_index(drop=True) | |
| # A4와 B4 셀부터 데이터가 들어가도록 엑셀에 작성 | |
| output_file = 'keyword_result.xlsx' | |
| with pd.ExcelWriter(output_file, engine='openpyxl') as writer: | |
| result_df.to_excel(writer, index=False, startrow=3, startcol=0, sheet_name="Sheet1") # A4 셀에 해당하는 3번째 행, 0번째 열부터 시작 | |
| # 워크북 및 시트 가져오기 | |
| workbook = writer.book | |
| sheet = writer.sheets['Sheet1'] | |
| # 차트 생성 | |
| chart = BarChart() | |
| data = Reference(sheet, min_col=2, min_row=4, max_row=3 + len(result_df), max_col=2) | |
| categories = Reference(sheet, min_col=1, min_row=4, max_row=3 + len(result_df)) | |
| chart.add_data(data, titles_from_data=True) | |
| chart.set_categories(categories) | |
| chart.title = "키워드 빈도수" | |
| chart.x_axis.title = "키워드" | |
| chart.y_axis.title = "빈도" | |
| # 차트를 시트에 추가 | |
| sheet.add_chart(chart, "E4") # E4 셀에 차트를 추가 | |
| return output_file | |
| # Gradio 인터페이스 생성 | |
| interface = gr.Interface( | |
| fn=process_excel, | |
| inputs=gr.File(label="엑셀 파일 업로드"), | |
| outputs=gr.File(label="분석 결과 파일") | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() | |