File size: 1,697 Bytes
531d477
1fcd7be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171f69d
fb5e935
 
171f69d
fb5e935
1fcd7be
 
171f69d
1fcd7be
531d477
1fcd7be
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
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()