Kims12's picture
Update app.py
723a56f verified
raw
history blame
2.22 kB
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()