Kims12's picture
Update app.py
00dc9b3 verified
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.strftime('%Y-%m')
review_counts = df_recent.groupby('년월').size().reset_index(name='리뷰건수')
logger.info("월별 리뷰 건수 집계 완료")
# 새로운 시트에 저장
logger.info("새로운 시트 '월별 리뷰건수' 생성 시작")
with pd.ExcelWriter(file_path, engine='openpyxl', mode='a') as writer:
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()