File size: 3,488 Bytes
7cfcd5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
import os
import pandas as pd
import gradio as gr
from datetime import datetime
from dotenv import load_dotenv

# ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋กœ๋“œ
load_dotenv()

# OpenAI API ํ‚ค ์„ค์ • (ํ˜„์žฌ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์ง€๋งŒ ํ•„์š” ์‹œ ์œ ์ง€)
openai_api_key = os.getenv("OPENAI_API_KEY")

# ์—‘์…€ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ ํ•จ์ˆ˜
def read_excel_data(file):
    df = pd.read_excel(file, usecols="A, B, C, D, E", skiprows=1,
                       names=["ID", "Review Date", "Option", "Review", "ReviewScore"], engine='openpyxl')
    df['Review Date'] = pd.to_datetime(df['Review Date']).dt.tz_localize(None).dt.date
    df['Year'] = df['Review Date'].astype(str).str.slice(0, 4)
    df['Option1'] = df['Option'].astype(str).str.split(" / ").str[0]  # 1์ฐจ ์˜ต์…˜ ์ถ”์ถœ
    df['Review Length'] = df['Review'].str.len()  # ๋ฆฌ๋ทฐ ๊ธธ์ด ๊ณ„์‚ฐ
    return df

# ๊ธ์ •์ ์ธ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
def get_positive_reviews(df):
    # ๋ฆฌ๋ทฐ ์ ์ˆ˜๊ฐ€ 4 ์ด์ƒ์ธ ๊ธ์ • ๋ฆฌ๋ทฐ ํ•„ํ„ฐ๋ง
    positive_reviews = df[df['ReviewScore'] >= 4].sort_values(by='Review Length', ascending=False)
    positive_reviews = positive_reviews.head(20)  # ์ƒ์œ„ 20๊ฐœ ๋ฆฌ๋ทฐ ์„ ํƒ

    positive_reviews.reset_index(drop=True, inplace=True)
    positive_reviews.index += 1
    positive_reviews['์ˆœ๋ฒˆ'] = positive_reviews.index

    # ๋ฆฌ๋ทฐ ํ˜•์‹ ์ง€์ •
    positive_output = "\n\n".join(positive_reviews.apply(
        lambda x: f"{x['์ˆœ๋ฒˆ']}. **{x['Review Date']} / {x['ID']} / {x['Option']}**\n\n{x['Review']}", axis=1))
    return positive_output

# ๋ถ€์ •์ ์ธ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
def get_negative_reviews(df):
    # ๋ฆฌ๋ทฐ ์ ์ˆ˜๊ฐ€ 2 ์ดํ•˜์ธ ๋ถ€์ • ๋ฆฌ๋ทฐ ํ•„ํ„ฐ๋ง
    negative_reviews = df[df['ReviewScore'] <= 2].sort_values(by='Review Length', ascending=False)
    negative_reviews = negative_reviews.head(30)  # ์ƒ์œ„ 30๊ฐœ ๋ฆฌ๋ทฐ ์„ ํƒ

    negative_reviews.reset_index(drop=True, inplace=True)
    negative_reviews.index += 1
    negative_reviews['์ˆœ๋ฒˆ'] = negative_reviews.index

    # ๋ฆฌ๋ทฐ ํ˜•์‹ ์ง€์ •
    negative_output = "\n\n".join(negative_reviews.apply(
        lambda x: f"{x['์ˆœ๋ฒˆ']}. **{x['Review Date']} / {x['ID']} / {x['Option']}**\n\n{x['Review']}", axis=1))
    return negative_output

# ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ ๊ธ์ • ๋ฐ ๋ถ€์ • ๋ฆฌ๋ทฐ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜
def process_reviews(file):
    df = read_excel_data(file)
    positive_reviews = get_positive_reviews(df)
    negative_reviews = get_negative_reviews(df)
    return positive_reviews, negative_reviews

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ
def create_interface():
    with gr.Blocks() as demo:
        gr.Markdown("### ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ ์—…๋กœ๋“œ")
        file_input = gr.File(label="์—‘์…€ ํŒŒ์ผ ์—…๋กœ๋“œ", file_types=[".xlsx"])
        analyze_button = gr.Button("๋ฆฌ๋ทฐ ์ถ”์ถœํ•˜๊ธฐ")
        
        with gr.Column():
            gr.Markdown("### ๊ธ์ •์ ์ธ ์ฃผ์š” ๋ฆฌ๋ทฐ (์ตœ๋Œ€ 20๊ฐœ)")
            positive_reviews_output = gr.Textbox(label="๊ธ์ •์ ์ธ ์ฃผ์š” ๋ฆฌ๋ทฐ", interactive=False, lines=20)
            
            gr.Markdown("### ๋ถ€์ •์ ์ธ ์ฃผ์š” ๋ฆฌ๋ทฐ (์ตœ๋Œ€ 30๊ฐœ)")
            negative_reviews_output = gr.Textbox(label="๋ถ€์ •์ ์ธ ์ฃผ์š” ๋ฆฌ๋ทฐ", interactive=False, lines=30)
        
        analyze_button.click(
            fn=process_reviews,
            inputs=[file_input],
            outputs=[positive_reviews_output, negative_reviews_output]
        )
    
    return demo

if __name__ == "__main__":
    interface = create_interface()
    interface.launch()