File size: 2,222 Bytes
d3f9826
 
b6454ba
723a56f
d3f9826
723a56f
 
 
d3f9826
723a56f
b6454ba
723a56f
e4febc9
723a56f
 
d3f9826
723a56f
 
d3f9826
723a56f
 
d3f9826
723a56f
 
 
 
d3f9826
723a56f
 
 
e4febc9
723a56f
 
 
 
 
 
 
 
b6454ba
723a56f
d3f9826
723a56f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
from datetime import datetime
import os

# 로그 메시지를 출력하는 함수
def log_message(step):
    print(f"[LOG] {step} 단계가 실행되었습니다.")

# 엑셀 파일 처리 및 분석 함수
def analyze_reviews(file_path):
    log_message("1. 파일 업로드 처리 중")

    # 엑셀 파일 읽기
    df = pd.read_excel(file_path)

    if "B" not in df.columns:
        return "오류: 원본 데이터에 B열(리뷰 날짜)이 없습니다."

    # "B" 열에서 년도와 월을 추출하여 새로운 열 생성
    df["년도-월"] = pd.to_datetime(df["B"]).dt.to_period("M").astype(str)

    # 최근 3년 데이터를 필터링 (기준: 2025년)
    current_year = 2025
    recent_years = [str(year) for year in range(current_year - 2, current_year + 1)]
    filtered_df = df[df["년도-월"].str[:4].isin(recent_years)]

    # 년월별 리뷰 건수 계산
    review_counts = filtered_df["년도-월"].value_counts().sort_index().reset_index()
    review_counts.columns = ["년도-월", "리뷰 건수"]

    # 새 엑셀 파일 작성
    output_path = "review_analysis.xlsx"
    with pd.ExcelWriter(output_path) as writer:
        review_counts.to_excel(writer, sheet_name="월별 리뷰건수", index=False)
        writer.sheets["월별 리뷰건수"].cell(1, 1, "년도-월")
        writer.sheets["월별 리뷰건수"].cell(1, 2, "리뷰 건수")

    log_message("2. 리뷰 분석 완료 및 파일 생성됨")
    
    return output_path

# 그라디오 인터페이스 구성
def interface(file):
    log_message("3. 분석 버튼 클릭됨")
    analyzed_file_path = analyze_reviews(file.name)
    log_message("4. 엑셀 파일 다운로드 준비 완료")
    return analyzed_file_path

# 그라디오 UI 구성
with gr.Blocks() as demo:
    gr.Markdown("# 리뷰 분석 시스템")
    file_input = gr.File(label="엑셀 파일 업로드", type="file", file_types=[".xlsx"])
    analyze_button = gr.Button("분석하기")
    download_file = gr.File(label="분석된 엑셀 파일 다운로드", type="filepath")

    analyze_button.click(fn=interface, inputs=file_input, outputs=download_file)

# 애플리케이션 실행
demo.launch()