File size: 3,367 Bytes
752f8c7
b3c0282
 
3def092
 
b3c0282
3def092
b3c0282
3def092
b3c0282
 
 
 
 
 
3def092
b3c0282
 
 
3def092
b3c0282
 
 
 
 
 
 
 
 
 
3def092
4fd2d6c
 
 
 
 
 
3cd95df
 
 
 
53abbee
 
 
 
 
 
 
b3c0282
4b73621
 
b3c0282
4fd2d6c
3cd95df
4b73621
b3c0282
1110eae
b3c0282
 
 
 
 
 
 
 
 
 
 
1110eae
b3c0282
 
1110eae
3def092
b3c0282
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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()