File size: 3,133 Bytes
752f8c7 b3c0282 3def092 5de3413 3def092 b3c0282 3def092 b3c0282 3def092 b3c0282 3def092 b3c0282 3def092 b3c0282 3def092 5de3413 b3c0282 5de3413 752f8c7 b3c0282 5de3413 752f8c7 b3c0282 5de3413 b3c0282 5de3413 b3c0282 5de3413 3def092 b3c0282 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import gradio as gr
import pandas as pd
from datetime import datetime
import logging
import matplotlib.pyplot as plt
# ๋ก๊ทธ ์ค์
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def analyze_reviews(file_path):
try:
logger.info("์์
ํ์ผ ์
๋ก๋ ์์")
# ์์
ํ์ผ ์ฝ๊ธฐ
df = pd.read_excel(file_path)
logger.info("์์
ํ์ผ ์ฝ๊ธฐ ์๋ฃ")
# ํ์ฌ ์ฐ๋
current_year = 2025
start_year = current_year - 3 # ์ต๊ทผ 3๋
logger.info(f"๋ฐ์ดํฐ ํํฐ๋ง: {start_year}๋
๋ถํฐ {current_year}๋
๊น์ง")
# B์ด์ด ๋ฆฌ๋ทฐ ๋ ์ง๋ผ๊ณ ๊ฐ์ ํ๊ณ , 'B' ์ด์ ์ด๋ฆ์ '๋ฆฌ๋ทฐ๋ ์ง'๋ก ๋ณ๊ฒฝ
if '๋ฆฌ๋ทฐ๋ ์ง' not in df.columns:
df.rename(columns={df.columns[1]: '๋ฆฌ๋ทฐ๋ ์ง'}, inplace=True)
# ๋ฆฌ๋ทฐ ๋ ์ง๋ฅผ datetime์ผ๋ก ๋ณํ
df['๋ฆฌ๋ทฐ๋ ์ง'] = pd.to_datetime(df['๋ฆฌ๋ทฐ๋ ์ง'], errors='coerce')
# ์ต๊ทผ 3๋
๋ฐ์ดํฐ ํํฐ๋ง
df_recent = df[df['๋ฆฌ๋ทฐ๋ ์ง'].dt.year >= start_year]
logger.info("์ต๊ทผ 3๋
๋ฐ์ดํฐ ํํฐ๋ง ์๋ฃ")
# ์๋ณ ๋ฆฌ๋ทฐ ๊ฑด์ ๊ณ์ฐ
logger.info("์๋ณ ๋ฆฌ๋ทฐ ๊ฑด์ ์ง๊ณ ์์")
df_recent['๋
์'] = df_recent['๋ฆฌ๋ทฐ๋ ์ง'].dt.to_period('M').astype(str)
monthly_review_counts = df_recent.groupby('๋
์').size().reset_index(name='๋ฆฌ๋ทฐ๊ฑด์')
logger.info("์๋ณ ๋ฆฌ๋ทฐ ๊ฑด์ ์ง๊ณ ์๋ฃ")
# ๊ทธ๋ํ ์์ฑ
logger.info("๊ทธ๋ํ ์์ฑ ์์")
plt.figure(figsize=(10, 6))
plt.bar(monthly_review_counts['๋
์'], monthly_review_counts['๋ฆฌ๋ทฐ๊ฑด์'], color='skyblue')
plt.xlabel('๋
์')
plt.ylabel('๋ฆฌ๋ทฐ ๊ฑด์')
plt.title('์ต๊ทผ 3๋
์๋ณ ๋ฆฌ๋ทฐ ๊ฑด์')
plt.xticks(rotation=45)
graph_path = file_path.replace('.xlsx', '_graph.png')
plt.tight_layout()
plt.savefig(graph_path)
plt.close()
logger.info("๊ทธ๋ํ ์์ฑ ์๋ฃ")
# ์๋ก์ด ์ํธ์ ๊ทธ๋ํ ๊ฒฝ๋ก ์ ์ฅ
logger.info("์๋ก์ด ์ํธ ์์ฑ ์์")
with pd.ExcelWriter(file_path, engine='openpyxl', mode='a') as writer:
monthly_review_counts.to_excel(writer, sheet_name='์๋ณ ๋ฆฌ๋ทฐ๊ฑด์', index=False)
logger.info("์๋ก์ด ์ํธ ์์ฑ ์๋ฃ")
return graph_path
except Exception as e:
logger.error(f"๋ถ์ ์ค ์ค๋ฅ ๋ฐ์: {e}")
return None
# ๊ทธ๋ผ๋์ค ์ธํฐํ์ด์ค ๊ตฌ์ฑ
def main():
logger.info("๊ทธ๋ผ๋์ค ์ธํฐํ์ด์ค ์์")
with gr.Blocks() as demo:
gr.Markdown("### ๋ฆฌ๋ทฐ ๋ถ์ ์คํ์ด์ค")
with gr.Row():
file_input = gr.File(label="์๋ณธ ์์
ํ์ผ ์
๋ก๋", file_types=[".xlsx"])
graph_output = gr.File(label="์์ฑ๋ ๊ทธ๋ํ ํ์ผ ๋ค์ด๋ก๋", type="filepath")
analyze_button = gr.Button("๋ถ์")
analyze_button.click(fn=analyze_reviews, inputs=file_input, outputs=graph_output)
demo.launch()
if __name__ == "__main__":
main()
|