kijeoung commited on
Commit
5de3413
Β·
verified Β·
1 Parent(s): 752f8c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -10
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import pandas as pd
3
  from datetime import datetime
4
  import logging
 
5
 
6
  # 둜그 μ„€μ •
7
  logging.basicConfig(level=logging.INFO)
@@ -29,19 +30,33 @@ def analyze_reviews(file_path):
29
  df_recent = df[df['λ¦¬λ·°λ‚ μ§œ'].dt.year >= start_year]
30
  logger.info("졜근 3λ…„ 데이터 필터링 μ™„λ£Œ")
31
 
32
- # 년도별 리뷰 건수 계산
33
- logger.info("년도별 리뷰 건수 집계 μ‹œμž‘")
34
- df_recent['년도'] = df_recent['λ¦¬λ·°λ‚ μ§œ'].dt.year
35
- yearly_review_counts = df_recent.groupby('년도').size().reset_index(name='리뷰건수')
36
- logger.info("년도별 리뷰 건수 집계 μ™„λ£Œ")
37
 
38
- # μƒˆλ‘œμš΄ μ‹œνŠΈμ— μ €μž₯
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  logger.info("μƒˆλ‘œμš΄ μ‹œνŠΈ 생성 μ‹œμž‘")
40
  with pd.ExcelWriter(file_path, engine='openpyxl', mode='a') as writer:
41
- yearly_review_counts.to_excel(writer, sheet_name='년도별 리뷰건수', index=False)
42
  logger.info("μƒˆλ‘œμš΄ μ‹œνŠΈ 생성 μ™„λ£Œ")
43
 
44
- return file_path
45
  except Exception as e:
46
  logger.error(f"뢄석 쀑 였λ₯˜ λ°œμƒ: {e}")
47
  return None
@@ -53,10 +68,10 @@ def main():
53
  gr.Markdown("### 리뷰 뢄석 슀페이슀")
54
  with gr.Row():
55
  file_input = gr.File(label="원본 μ—‘μ…€ 파일 μ—…λ‘œλ“œ", file_types=[".xlsx"])
56
- file_output = gr.File(label="λΆ„μ„λœ μ—‘μ…€ 파일 λ‹€μš΄λ‘œλ“œ", type="filepath")
57
  analyze_button = gr.Button("뢄석")
58
 
59
- analyze_button.click(fn=analyze_reviews, inputs=file_input, outputs=file_output)
60
 
61
  demo.launch()
62
 
 
2
  import pandas as pd
3
  from datetime import datetime
4
  import logging
5
+ import matplotlib.pyplot as plt
6
 
7
  # 둜그 μ„€μ •
8
  logging.basicConfig(level=logging.INFO)
 
30
  df_recent = df[df['λ¦¬λ·°λ‚ μ§œ'].dt.year >= start_year]
31
  logger.info("졜근 3λ…„ 데이터 필터링 μ™„λ£Œ")
32
 
33
+ # 월별 리뷰 건수 계산
34
+ logger.info("월별 리뷰 건수 집계 μ‹œμž‘")
35
+ df_recent['λ…„μ›”'] = df_recent['λ¦¬λ·°λ‚ μ§œ'].dt.to_period('M').astype(str)
36
+ monthly_review_counts = df_recent.groupby('λ…„μ›”').size().reset_index(name='리뷰건수')
37
+ logger.info("월별 리뷰 건수 집계 μ™„λ£Œ")
38
 
39
+ # κ·Έλž˜ν”„ 생성
40
+ logger.info("κ·Έλž˜ν”„ 생성 μ‹œμž‘")
41
+ plt.figure(figsize=(10, 6))
42
+ plt.bar(monthly_review_counts['λ…„μ›”'], monthly_review_counts['리뷰건수'], color='skyblue')
43
+ plt.xlabel('λ…„μ›”')
44
+ plt.ylabel('리뷰 건수')
45
+ plt.title('졜근 3λ…„ 월별 리뷰 건수')
46
+ plt.xticks(rotation=45)
47
+ graph_path = file_path.replace('.xlsx', '_graph.png')
48
+ plt.tight_layout()
49
+ plt.savefig(graph_path)
50
+ plt.close()
51
+ logger.info("κ·Έλž˜ν”„ 생성 μ™„λ£Œ")
52
+
53
+ # μƒˆλ‘œμš΄ μ‹œνŠΈμ™€ κ·Έλž˜ν”„ 경둜 μ €μž₯
54
  logger.info("μƒˆλ‘œμš΄ μ‹œνŠΈ 생성 μ‹œμž‘")
55
  with pd.ExcelWriter(file_path, engine='openpyxl', mode='a') as writer:
56
+ monthly_review_counts.to_excel(writer, sheet_name='월별 리뷰건수', index=False)
57
  logger.info("μƒˆλ‘œμš΄ μ‹œνŠΈ 생성 μ™„λ£Œ")
58
 
59
+ return graph_path
60
  except Exception as e:
61
  logger.error(f"뢄석 쀑 였λ₯˜ λ°œμƒ: {e}")
62
  return None
 
68
  gr.Markdown("### 리뷰 뢄석 슀페이슀")
69
  with gr.Row():
70
  file_input = gr.File(label="원본 μ—‘μ…€ 파일 μ—…λ‘œλ“œ", file_types=[".xlsx"])
71
+ graph_output = gr.File(label="μƒμ„±λœ κ·Έλž˜ν”„ 파일 λ‹€μš΄λ‘œλ“œ", type="filepath")
72
  analyze_button = gr.Button("뢄석")
73
 
74
+ analyze_button.click(fn=analyze_reviews, inputs=file_input, outputs=graph_output)
75
 
76
  demo.launch()
77