Spaces:
Sleeping
Sleeping
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() | |