dogdj / app.py
kijeoung's picture
Update app.py
53abbee 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.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()