kijeoung's picture
Create app.py
2b3cd9e verified
raw
history blame
4.05 kB
import os
import pandas as pd
import gradio as gr
import openai
from datetime import datetime
# OpenAI API ํด๋ผ์ด์–ธํŠธ ์„ค์ •
openai.api_key = os.getenv("OPENAI_API_KEY")
# LLM ํ˜ธ์ถœ ํ•จ์ˆ˜
def call_api(content, system_message="๋ฆฌ๋ทฐ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ์š”์•ฝํ•ด ์ฃผ์„ธ์š”.", max_tokens=200, temperature=0.7, top_p=0.9):
response = openai.ChatCompletion.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": system_message},
{"role": "user", "content": content},
],
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
)
return response.choices[0].message['content']
# ์—‘์…€ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ ํ•จ์ˆ˜
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]
df['Review Length'] = df['Review'].str.len()
return df
# ๊ธ์ •์ ์ธ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
def get_positive_reviews(df):
positive_reviews = df[df['ReviewScore'] >= 4].sort_values(by='Review Length', ascending=False)
positive_reviews = positive_reviews.head(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))
analysis = call_api(positive_output)
return positive_output, analysis
# ๋ถ€์ •์ ์ธ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
def get_negative_reviews(df):
negative_reviews = df[df['ReviewScore'] <= 2].sort_values(by='Review Length', ascending=False)
negative_reviews = negative_reviews.head(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))
analysis = call_api(negative_output)
return negative_output, analysis
# ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ ๊ธ์ • ๋ฐ ๋ถ€์ • ๋ฆฌ๋ทฐ๋ฅผ ์ถ”์ถœํ•˜๋Š” ํ•จ์ˆ˜
def process_reviews(file):
df = read_excel_data(file)
positive_reviews, positive_analysis = get_positive_reviews(df)
negative_reviews, negative_analysis = get_negative_reviews(df)
return positive_reviews, positive_analysis, negative_reviews, negative_analysis
# 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)
positive_analysis_output = gr.Textbox(label="๊ธ์ • ๋ฆฌ๋ทฐ ๋ถ„์„ ๊ฒฐ๊ณผ", interactive=False, lines=5)
gr.Markdown("### ๋ถ€์ •์ ์ธ ์ฃผ์š” ๋ฆฌ๋ทฐ (์ตœ๋Œ€ 30๊ฐœ)")
negative_reviews_output = gr.Textbox(label="๋ถ€์ •์ ์ธ ์ฃผ์š” ๋ฆฌ๋ทฐ", interactive=False, lines=30)
negative_analysis_output = gr.Textbox(label="๋ถ€์ • ๋ฆฌ๋ทฐ ๋ถ„์„ ๊ฒฐ๊ณผ", interactive=False, lines=5)
analyze_button.click(
fn=process_reviews,
inputs=[file_input],
outputs=[positive_reviews_output, positive_analysis_output, negative_reviews_output, negative_analysis_output]
)
return demo
if __name__ == "__main__":
interface = create_interface()
interface.launch()