import gradio as gr import openai # ✅ OpenAI API 키 입력 (안전한 저장 방식 권장) openai.api_key = "YOUR_OPENAI_API_KEY" # GPT에게 보낼 프롬프트를 구성하고 응답 받기 def chat_with_gpt(message, history): # 시스템 프롬프트: 역할 설정 system_prompt = "너는 문장을 공손하고 예의 있게 바꿔주는 한국어 전문가야." # 사용자 프롬프트 구성 user_prompt = f"""아래 문장의 무례하거나 공격적인 표현을 찾아내고, 더 예의 있는 표현으로 바꿔줘. 간단한 이유도 함께 알려줘. 문장: "{message}" 응답 형식: 1. 지적 사항: (무례한 표현이 있다면 어떤 부분인지 설명) 2. 제안 문장: (더 예의 있게 바꾼 문장 제안) """ try: response = openai.ChatCompletion.create( model="gpt-4", # 또는 "gpt-3.5-turbo" messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt} ], temperature=0.7, ) reply = response["choices"][0]["message"]["content"].strip() return history + [[message, reply]] except Exception as e: return history + [[message, f"⚠️ 오류 발생: {str(e)}"]] # Gradio 앱 구성 with gr.Blocks() as demo: chatbot = gr.Chatbot() msg = gr.Textbox(label="문장을 입력하세요", placeholder="예: 너 정말 왜 그렇게 말해?") def respond_and_clear(user_input, history): updated_history = chat_with_gpt(user_input, history) return "", updated_history msg.submit(respond_and_clear, [msg, chatbot], [msg, chatbot]) demo.launch()