Kims12 commited on
Commit
e4febc9
·
verified ·
1 Parent(s): de1cc66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -10,13 +10,15 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
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
@@ -37,18 +39,21 @@ def analyze_reviews(file):
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("결과 파일 저장 완료")
@@ -61,13 +66,13 @@ def analyze_reviews(file):
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
  # 스페이스 실행
 
10
  def analyze_reviews(file):
11
  logging.info("파일 업로드 시작")
12
  try:
13
+ # 바이너리 데이터를 BytesIO로 변환
14
+ file_bytes = BytesIO(file)
15
  # 엑셀 파일 읽기
16
+ df = pd.read_excel(file_bytes, engine='openpyxl')
17
  logging.info("엑셀 파일 읽기 완료")
18
  except Exception as e:
19
  logging.error(f"엑셀 파일 읽기 오류: {e}")
20
  return f"엑셀 파일을 읽는 중 오류가 발생했습니다: {e}"
21
+
22
  try:
23
  # 현재 기준 연도 설정 (2025년 기준)
24
  current_year = 2025
 
39
 
40
  # 새로운 시트에 데이터 작성
41
  logging.info("새로운 시트 생성 및 데이터 작성 시작")
42
+ with pd.ExcelWriter(file_bytes, engine='openpyxl', mode='a', if_sheet_exists='replace') as writer:
43
+ review_counts.to_excel(writer, sheet_name='월별 리뷰건수', index=False, header=['년월', '리뷰건수'])
44
  logging.info("새로운 시트에 데이터 작성 완료")
45
 
46
  # 결과 파일 저장
47
  logging.info("결과 파일 저장 시작")
48
  output = BytesIO()
49
+ file_bytes.seek(0) # 원본 파일 포인터 초기화
50
  with pd.ExcelWriter(output, engine='openpyxl') as writer:
51
+ # 모든 시트 복사
52
+ with pd.ExcelFile(file_bytes) as xls:
53
+ for sheet_name in xls.sheet_names:
54
+ df_sheet = pd.read_excel(xls, sheet_name=sheet_name, engine='openpyxl')
55
+ df_sheet.to_excel(writer, sheet_name=sheet_name, index=False)
56
+ # 추가된 '월별 리뷰건수' 시트 추가
57
  review_counts.to_excel(writer, sheet_name='월별 리뷰건수', index=False, header=['년월', '리뷰건수'])
58
  output.seek(0)
59
  logging.info("결과 파일 저장 완료")
 
66
  # Gradio 인터페이스 구성
67
  with gr.Blocks() as demo:
68
  gr.Markdown("## 리뷰 분석 스페이스")
69
+
70
  with gr.Row():
71
+ file_input = gr.File(label="원본 엑셀 파일 업로드", type="binary")
72
+
73
  analyze_button = gr.Button("분석")
74
  output_download = gr.File(label="분석된 엑셀 파일 다운로드")
75
+
76
  analyze_button.click(fn=analyze_reviews, inputs=file_input, outputs=output_download)
77
 
78
  # 스페이스 실행