File size: 4,924 Bytes
87791c3
233a83b
 
a9bd4ef
ab73867
a9bd4ef
6981f1b
7de0cb4
87791c3
a9bd4ef
 
 
 
233a83b
 
a9bd4ef
 
 
 
233a83b
 
 
a9bd4ef
233a83b
a9bd4ef
233a83b
 
 
 
 
 
 
 
 
 
a9bd4ef
233a83b
 
 
 
 
 
 
 
a9bd4ef
233a83b
 
 
2e230c5
233a83b
 
 
 
 
 
a9bd4ef
 
233a83b
 
 
 
 
 
 
 
a9bd4ef
233a83b
 
 
 
 
 
a9bd4ef
233a83b
 
 
 
 
 
 
 
 
a9bd4ef
233a83b
 
 
 
 
 
 
 
 
 
 
a9bd4ef
233a83b
 
 
 
 
 
 
a9bd4ef
 
 
233a83b
a9bd4ef
 
233a83b
a9bd4ef
233a83b
a9bd4ef
233a83b
a9bd4ef
233a83b
 
 
a9bd4ef
 
233a83b
 
a9bd4ef
 
 
87791c3
 
233a83b
 
a9bd4ef
 
 
 
 
233a83b
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import gradio as gr
import requests
import json
import os # os 라이브러리 μΆ”κ°€

# ν™˜κ²½ λ³€μˆ˜μ—μ„œ API ν‚€λ₯Ό κ°€μ Έμ˜΄
API_KEY = os.getenv("UPSTAGE_API_KEY")
API_URL = "https://api.upstage.ai/v1/"

# API ν‚€κ°€ μ—†λŠ” 경우 μ—λŸ¬ λ°œμƒ
if API_KEY is None:
    raise ValueError("ν™˜κ²½ λ³€μˆ˜ 'UPSTAGE_API_KEY'κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. API ν‚€λ₯Ό μ„€μ •ν•΄μ£Όμ„Έμš”.")

def chat_with_solar(message, history):
    """Solar Pro2와 λŒ€ν™”ν•˜λŠ” ν•¨μˆ˜"""
    messages = [
        {"role": "system", "content": "당신은 도움이 λ˜λŠ” AI μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€. ν•œκ΅­μ–΄λ‘œ μΉœκ·Όν•˜κ³  μ •ν™•ν•˜κ²Œ λ‹΅λ³€ν•΄μ£Όμ„Έμš”."}
    ]

    for human_msg, ai_msg in history:
        messages.append({"role": "user", "content": human_msg})
        messages.append({"role": "assistant", "content": ai_msg})

    messages.append({"role": "user", "content": message})

    data = {
        "model": "solar-pro-2",
        "messages": messages,
        "max_tokens": 1000,
        "temperature": 0.7
    }
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    try:
        response = requests.post(API_URL, headers=headers, json=data)
        response.raise_for_status()
        result = response.json()
        ai_response = result['choices'][0]['message']['content']
        return ai_response
    except requests.exceptions.RequestException as e:
        return f"API μš”μ²­ 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
    except (KeyError, IndexError) as e:
        return f"응닡 νŒŒμ‹± 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"
    except Exception as e:
        return f"μ˜ˆμƒμΉ˜ λͺ»ν•œ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {str(e)}"

def create_chatbot():
    """챗봇 μΈν„°νŽ˜μ΄μŠ€ 생성"""
    with gr.Blocks(
        title="Solar Pro2 챗봇",
        theme=gr.themes.Soft(),
        css="""
        .container { max-width: 900px; margin: auto; }
        .header { text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px; }
        """
    ) as demo:
        gr.HTML("""
        <div class="header">
            <h1>🌞 Solar Pro2 챗봇</h1>
            <p>Upstage의 Solar Pro2 λͺ¨λΈκ³Ό λŒ€ν™”ν•΄λ³΄μ„Έμš”!</p>
        </div>
        """)

        chatbot = gr.Chatbot(
            height=500,
            bubble_full_width=False,
            avatar_images=("πŸ‘€", "πŸ€–"),
            show_copy_button=True
        )

        with gr.Row():
            msg = gr.Textbox(
                placeholder="λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜μ„Έμš”...",
                container=False,
                scale=4,
                show_label=False
            )
            submit_btn = gr.Button("전솑", scale=1, variant="primary")
            clear_btn = gr.Button("λŒ€ν™” μ΄ˆκΈ°ν™”", scale=1)

        gr.Examples(
            examples=[
                "μ•ˆλ…•ν•˜μ„Έμš”! μžκΈ°μ†Œκ°œλ₯Ό ν•΄μ£Όμ„Έμš”.",
                "파이썬으둜 κ°„λ‹¨ν•œ 계산기λ₯Ό λ§Œλ“œλŠ” 방법을 μ•Œλ €μ£Όμ„Έμš”.",
                "였늘 날씨가 μ’‹λ„€μš”. μ–΄λ–€ ν™œλ™μ„ μΆ”μ²œν•˜μ‹œλ‚˜μš”?",
                "AI에 λŒ€ν•΄ κ°„λ‹¨νžˆ μ„€λͺ…ν•΄μ£Όμ„Έμš”.",
                "λ§›μžˆλŠ” ν•œκ΅­ μŒμ‹ λ ˆμ‹œν”Όλ₯Ό ν•˜λ‚˜ μΆ”μ²œν•΄μ£Όμ„Έμš”."
            ],
            inputs=msg,
            label="μ˜ˆμ‹œ μ§ˆλ¬Έλ“€"
        )

        with gr.Accordion("ℹ️ μ‚¬μš© 정보", open=False):
            gr.Markdown("""
            ### Solar Pro2 챗봇에 λŒ€ν•΄
            - **λͺ¨λΈ**: Upstage Solar Pro2
            - **κΈ°λŠ₯**: ν•œκ΅­μ–΄ λŒ€ν™”, 질문 λ‹΅λ³€, μ½”λ“œ 생성 λ“±
            - **νŠΉμ§•**: 높은 μ„±λŠ₯의 ν•œκ΅­μ–΄ μ–Έμ–΄λͺ¨λΈ
            ### μ‚¬μš© 방법
            1. μœ„μ˜ ν…μŠ€νŠΈ λ°•μŠ€μ— μ§ˆλ¬Έμ΄λ‚˜ λŒ€ν™”λ₯Ό μž…λ ₯ν•˜μ„Έμš”.
            2. '전솑' λ²„νŠΌμ„ ν΄λ¦­ν•˜κ±°λ‚˜ Enterλ₯Ό λˆ„λ₯΄μ„Έμš”.
            3. AI의 응닡을 ν™•μΈν•˜κ³  계속 λŒ€ν™”ν•˜μ„Έμš”.
            ### μ£Όμ˜μ‚¬ν•­
            - `UPSTAGE_API_KEY` ν™˜κ²½ λ³€μˆ˜κ°€ μ˜¬λ°”λ₯΄κ²Œ μ„€μ •λ˜μ–΄ μžˆλŠ”μ§€ ν™•μΈν•΄μ£Όμ„Έμš”.
            - 인터넷 연결이 ν•„μš”ν•©λ‹ˆλ‹€.
            """)

        def respond(message, chat_history):
            if not message or not message.strip():
                return "", chat_history

            bot_message = chat_with_solar(message, chat_history)
            chat_history.append((message, bot_message))
            return "", chat_history

        # 이벀트 ν•Έλ“€λŸ¬ μ—°κ²° (쀑볡 제거)
        msg.submit(respond, [msg, chatbot], [msg, chatbot])
        submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
        clear_btn.click(lambda: (None, []), outputs=[msg, chatbot])

        return demo

if __name__ == "__main__":
    demo = create_chatbot()
    demo.launch(
        share=True,
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True,
        debug=True
    )