Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,58 +8,68 @@ from io import BytesIO
|
|
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 |
-
|
|
|
|
|
|
|
|
|
18 |
current_year = 2025
|
19 |
-
start_year = current_year - 3 #
|
20 |
|
21 |
-
logging.info("리뷰 날짜
|
22 |
# B열에 있는 리뷰 날짜를 datetime 형식으로 변환
|
23 |
-
df['
|
24 |
-
#
|
25 |
-
df_filtered = df[(df['
|
26 |
-
logging.info(
|
27 |
|
28 |
-
# 년월별 리뷰 건수
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
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 |
-
|
42 |
-
|
|
|
|
|
|
|
43 |
output.seek(0)
|
44 |
-
logging.info("
|
45 |
|
46 |
return output
|
47 |
-
|
48 |
except Exception as e:
|
49 |
-
logging.error(f"
|
50 |
-
return
|
51 |
|
52 |
# Gradio 인터페이스 구성
|
53 |
with gr.Blocks() as demo:
|
54 |
-
gr.Markdown("## 리뷰
|
|
|
55 |
with gr.Row():
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
analyze_button.click(fn=analyze_reviews, inputs=file_input, outputs=
|
62 |
|
63 |
-
#
|
64 |
if __name__ == "__main__":
|
65 |
demo.launch()
|
|
|
8 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
9 |
|
10 |
def analyze_reviews(file):
|
11 |
+
logging.info("파일 업로드 시작")
|
12 |
try:
|
|
|
13 |
# 엑셀 파일 읽기
|
14 |
df = pd.read_excel(file, engine='openpyxl')
|
15 |
logging.info("엑셀 파일 읽기 완료")
|
16 |
+
except Exception as e:
|
17 |
+
logging.error(f"엑셀 파일 읽기 오류: {e}")
|
18 |
+
return f"엑셀 파일을 읽는 중 오류가 발생했습니다: {e}"
|
19 |
+
|
20 |
+
try:
|
21 |
+
# 현재 기준 연도 설정 (2025년 기준)
|
22 |
current_year = 2025
|
23 |
+
start_year = current_year - 3 # 최근 3년
|
24 |
|
25 |
+
logging.info("리뷰 날짜 처리 시작")
|
26 |
# B열에 있는 리뷰 날짜를 datetime 형식으로 변환
|
27 |
+
df['리뷰날짜'] = pd.to_datetime(df.iloc[:, 1], errors='coerce')
|
28 |
+
# 연도 및 월 필터링
|
29 |
+
df_filtered = df[(df['리뷰날짜'].dt.year >= start_year) & (df['리뷰날짜'].dt.year < current_year)]
|
30 |
+
logging.info("리뷰 날짜 필터링 완료")
|
31 |
|
32 |
+
# 년월별 리뷰 건수 집계
|
33 |
+
logging.info("년월별 리뷰 건수 집계 시작")
|
34 |
+
df_filtered['년월'] = df_filtered['리뷰날짜'].dt.strftime('%Y-%m')
|
35 |
+
review_counts = df_filtered.groupby('년월').size().reset_index(name='리뷰건수')
|
36 |
+
logging.info("년월별 리뷰 건수 집계 완료")
|
37 |
|
38 |
+
# 새로운 시트에 데이터 작성
|
39 |
+
logging.info("새로운 시트 생성 및 데이터 작성 시작")
|
40 |
with pd.ExcelWriter(file, engine='openpyxl', mode='a') as writer:
|
41 |
+
review_counts.to_excel(writer, sheet_name='월별 리뷰건수', index=False, header=['A열', 'B열'])
|
42 |
+
logging.info("새로운 시트에 데이터 작성 완료")
|
43 |
|
44 |
+
# 결과 파일 저장
|
45 |
+
logging.info("결과 파일 저장 시작")
|
46 |
output = BytesIO()
|
47 |
with pd.ExcelWriter(output, engine='openpyxl') as writer:
|
48 |
+
writer.book = pd.ExcelFile(file).book
|
49 |
+
for sheet in pd.ExcelFile(file).sheet_names:
|
50 |
+
df_sheet = pd.read_excel(file, sheet_name=sheet, engine='openpyxl')
|
51 |
+
df_sheet.to_excel(writer, sheet_name=sheet, index=False)
|
52 |
+
review_counts.to_excel(writer, sheet_name='월별 리뷰건수', index=False, header=['년월', '리뷰건수'])
|
53 |
output.seek(0)
|
54 |
+
logging.info("결과 파일 저장 완료")
|
55 |
|
56 |
return output
|
|
|
57 |
except Exception as e:
|
58 |
+
logging.error(f"분석 과정 중 오류: {e}")
|
59 |
+
return f"분석 과정에서 오류가 발생했습니다: {e}"
|
60 |
|
61 |
# Gradio 인터페이스 구성
|
62 |
with gr.Blocks() as demo:
|
63 |
+
gr.Markdown("## 리뷰 분석 스페이스")
|
64 |
+
|
65 |
with gr.Row():
|
66 |
+
file_input = gr.File(label="원본 엑셀 파일 업로드", type="file")
|
67 |
+
|
68 |
+
analyze_button = gr.Button("분석")
|
69 |
+
output_download = gr.File(label="분석된 엑셀 파일 다운로드")
|
70 |
+
|
71 |
+
analyze_button.click(fn=analyze_reviews, inputs=file_input, outputs=output_download)
|
72 |
|
73 |
+
# 스페이스 실행
|
74 |
if __name__ == "__main__":
|
75 |
demo.launch()
|