Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import logging
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
|
| 7 |
+
# 로그 설정
|
| 8 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 9 |
+
|
| 10 |
+
def analyze_reviews(file):
|
| 11 |
+
try:
|
| 12 |
+
logging.info("파일 업로드 시작")
|
| 13 |
+
# 엑셀 파일 읽기
|
| 14 |
+
df = pd.read_excel(file, engine='openpyxl')
|
| 15 |
+
logging.info("엑셀 파일 읽기 완료")
|
| 16 |
+
|
| 17 |
+
# 현재 연도 설정 (2025년 기준)
|
| 18 |
+
current_year = 2025
|
| 19 |
+
start_year = current_year - 3 # 2022년부터
|
| 20 |
+
|
| 21 |
+
logging.info("리뷰 날짜 데이터 처리 시작")
|
| 22 |
+
# B열에 있는 리뷰 날짜를 datetime 형식으로 변환
|
| 23 |
+
df['B'] = pd.to_datetime(df.iloc[:, 1], errors='coerce')
|
| 24 |
+
# 최근 3년 데이터 필터링
|
| 25 |
+
df_filtered = df[(df['B'].dt.year >= start_year) & (df['B'].dt.year < current_year)]
|
| 26 |
+
logging.info(f"필터링된 데이터 개수: {len(df_filtered)}")
|
| 27 |
+
|
| 28 |
+
# 년월별 리뷰 건수 계산
|
| 29 |
+
df_filtered['Year-Month'] = df_filtered['B'].dt.strftime('%Y-%m')
|
| 30 |
+
review_counts = df_filtered.groupby('Year-Month').size().reset_index(name='Review Count')
|
| 31 |
+
logging.info("월별 리뷰 건수 계산 완료")
|
| 32 |
+
|
| 33 |
+
# 새로운 시트에 작성
|
| 34 |
+
with pd.ExcelWriter(file, engine='openpyxl', mode='a') as writer:
|
| 35 |
+
review_counts.to_excel(writer, sheet_name='월별 리뷰건수', index=False)
|
| 36 |
+
logging.info("새로운 시트 '월별 리뷰건수' 추가 완료")
|
| 37 |
+
|
| 38 |
+
# 결과 파일을 바이트 형태로 변환
|
| 39 |
+
output = BytesIO()
|
| 40 |
+
with pd.ExcelWriter(output, engine='openpyxl') as writer:
|
| 41 |
+
df.to_excel(writer, index=False)
|
| 42 |
+
review_counts.to_excel(writer, sheet_name='월별 리뷰건수', index=False)
|
| 43 |
+
output.seek(0)
|
| 44 |
+
logging.info("분석된 엑셀 파일 생성 완료")
|
| 45 |
+
|
| 46 |
+
return output
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
logging.error(f"에러 발생: {e}")
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
# Gradio 인터페이스 구성
|
| 53 |
+
with gr.Blocks() as demo:
|
| 54 |
+
gr.Markdown("## 리뷰 분석기")
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column():
|
| 57 |
+
file_input = gr.File(label="원본 엑셀 파일 업로드", type="file")
|
| 58 |
+
analyze_button = gr.Button("분석")
|
| 59 |
+
with gr.Column():
|
| 60 |
+
download_output = gr.File(label="분석된 엑셀 파일 다운로드")
|
| 61 |
+
analyze_button.click(fn=analyze_reviews, inputs=file_input, outputs=download_output)
|
| 62 |
+
|
| 63 |
+
# 애플리케이션 실행
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
demo.launch()
|