|
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 |
|
|
|
logger.info(f"๋ฐ์ดํฐ ํํฐ๋ง: {start_year}๋
๋ถํฐ {current_year}๋
๊น์ง") |
|
|
|
if '๋ฆฌ๋ทฐ๋ ์ง' not in df.columns: |
|
df.rename(columns={df.columns[1]: '๋ฆฌ๋ทฐ๋ ์ง'}, inplace=True) |
|
|
|
|
|
df['๋ฆฌ๋ทฐ๋ ์ง'] = pd.to_datetime(df['๋ฆฌ๋ทฐ๋ ์ง'], errors='coerce') |
|
|
|
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 file_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"]) |
|
file_output = gr.File(label="๋ถ์๋ ์์
ํ์ผ ๋ค์ด๋ก๋", type="filepath") |
|
analyze_button = gr.Button("๋ถ์") |
|
|
|
analyze_button.click(fn=analyze_reviews, inputs=file_input, outputs=file_output) |
|
|
|
demo.launch() |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|