import gradio as gr import pandas as pd from datetime import datetime import logging # 로그 설정 logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) def analyze_reviews(file_path): try: logger.info("엑셀 파일 업로드 시작") # 엑셀 파일 읽기 df = pd.read_excel(file_path) logger.info("엑셀 파일 읽기 완료") # 현재 연도 current_year = 2025 start_year = current_year - 3 # 최근 3년 logger.info(f"데이터 필터링: {start_year}년부터 {current_year}년까지") # B열이 리뷰 날짜라고 가정하고, 'B' 열의 이름을 '리뷰날짜'로 변경 if '리뷰날짜' not in df.columns: df.rename(columns={df.columns[1]: '리뷰날짜'}, inplace=True) # 리뷰 날짜를 datetime으로 변환 df['리뷰날짜'] = pd.to_datetime(df['리뷰날짜'], errors='coerce') # 최근 3년 데이터 필터링 df_recent = df[df['리뷰날짜'].dt.year >= start_year] logger.info("최근 3년 데이터 필터링 완료") # 년도별 리뷰 건수 합산 계산 logger.info("년도별 리뷰 건수 집계 시작") df_recent['년도'] = df_recent['리뷰날짜'].dt.year yearly_review_counts = df_recent.groupby('년도').size().reset_index(name='리뷰건수') logger.info("년도별 리뷰 건수 집계 완료") # 월별 리뷰 건수 합산 계산 logger.info("월별 리뷰 건수 집계 시작") df_recent['년월'] = df_recent['리뷰날짜'].dt.to_period('M').astype(str) monthly_review_counts = df_recent.groupby('년월').size().reset_index(name='리뷰건수') # 누락된 월 채우기 logger.info("누락된 월 채우기 시작") all_months = pd.period_range(start=f"{start_year}-01", end=f"{current_year}-12", freq='M').astype(str) monthly_review_counts = monthly_review_counts.set_index('년월').reindex(all_months, fill_value=0).reset_index() monthly_review_counts.columns = ['년월', '리뷰건수'] logger.info("누락된 월 채우기 완료") # 새로운 시트에 저장 logger.info("새로운 시트에 데이터 저장 시작") with pd.ExcelWriter(file_path, engine='openpyxl', mode='a') as writer: yearly_review_counts.to_excel(writer, sheet_name='년도별리뷰수', index=False) monthly_review_counts.to_excel(writer, sheet_name='월별리뷰수', index=False) logger.info("새로운 시트에 데이터 저장 완료") return file_path except Exception as e: logger.error(f"분석 중 오류 발생: {e}") return None # 그라디오 인터페이스 구성 def main(): logger.info("그라디오 인터페이스 시작") with gr.Blocks() as demo: gr.Markdown("### 리뷰 분석 스페이스") with gr.Row(): file_input = gr.File(label="원본 엑셀 파일 업로드", file_types=[".xlsx"]) file_output = gr.File(label="분석된 엑셀 파일 다운로드", type="filepath") analyze_button = gr.Button("분석") analyze_button.click(fn=analyze_reviews, inputs=file_input, outputs=file_output) demo.launch() if __name__ == "__main__": main()