File size: 3,116 Bytes
752f8c7
b3c0282
 
3def092
5de3413
3def092
b3c0282
3def092
b3c0282
3def092
b3c0282
 
 
 
 
 
3def092
b3c0282
 
 
3def092
b3c0282
 
 
 
 
 
 
 
 
 
3def092
5de3413
 
 
 
 
b3c0282
5de3413
 
 
 
 
 
 
 
 
 
 
 
 
 
1110eae
752f8c7
b3c0282
1110eae
752f8c7
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
79
80
import gradio as gr
import pandas as pd
from datetime import datetime
import logging
import matplotlib.pyplot as plt

# ๋กœ๊ทธ ์„ค์ •
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.to_period('M').astype(str)
        monthly_review_counts = df_recent.groupby('๋…„์›”').size().reset_index(name='๋ฆฌ๋ทฐ๊ฑด์ˆ˜')
        logger.info("์›”๋ณ„ ๋ฆฌ๋ทฐ ๊ฑด์ˆ˜ ์ง‘๊ณ„ ์™„๋ฃŒ")

        # ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
        logger.info("๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ ์‹œ์ž‘")
        plt.figure(figsize=(10, 6))
        plt.bar(monthly_review_counts['๋…„์›”'], monthly_review_counts['๋ฆฌ๋ทฐ๊ฑด์ˆ˜'], color='skyblue')
        plt.xlabel('๋…„์›”')
        plt.ylabel('๋ฆฌ๋ทฐ ๊ฑด์ˆ˜')
        plt.title('์ตœ๊ทผ 3๋…„ ์›”๋ณ„ ๋ฆฌ๋ทฐ ๊ฑด์ˆ˜')
        plt.xticks(rotation=45)
        graph_path = file_path.replace('.xlsx', '_graph.png')
        plt.tight_layout()
        plt.savefig(graph_path)
        plt.close()
        logger.info("๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ ์™„๋ฃŒ")

        # ์ƒˆ๋กœ์šด ์‹œํŠธ์™€ ๊ทธ๋ž˜ํ”„ ์ €์žฅ
        logger.info("์ƒˆ๋กœ์šด ์‹œํŠธ ์ƒ์„ฑ ์‹œ์ž‘")
        with pd.ExcelWriter(file_path, engine='openpyxl', mode='a') as writer:
            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()