File size: 3,565 Bytes
226a535
35e0b37
7309bb0
3271c7b
 
35e0b37
 
732091b
3271c7b
 
 
 
 
 
 
 
 
 
 
d034f8c
732091b
3271c7b
35e0b37
3271c7b
732091b
3271c7b
 
 
 
 
35e0b37
 
3271c7b
 
 
 
cb245e5
732091b
35e0b37
 
 
 
30b8dce
732091b
35e0b37
 
 
 
732091b
 
35e0b37
732091b
35e0b37
732091b
35e0b37
 
1b77547
35e0b37
 
25c5250
35e0b37
 
 
 
 
732091b
 
3271c7b
35e0b37
 
 
 
3271c7b
 
732091b
3271c7b
 
 
35e0b37
3271c7b
1b77547
3271c7b
35e0b37
 
 
 
 
 
1a10419
3271c7b
cb245e5
226a535
732091b
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
import gradio as gr
from openai import OpenAI
import os
# 디버깅: ν™˜κ²½λ³€μˆ˜ 확인
print("=== ν™˜κ²½λ³€μˆ˜ 디버깅 ===")
print(f"UPSTAGE_API_KEY 쑴재 μ—¬λΆ€: {'UPSTAGE_API_KEY' in os.environ}")
api_key = os.environ.get("UPSTAGE_API_KEY")
if api_key:
    print(f"API ν‚€ 길이: {len(api_key)}")
    print(f"API ν‚€ μ‹œμž‘: {api_key[:10]}...")
else:
    print("API ν‚€κ°€ μ—†μŠ΅λ‹ˆλ‹€!")
    print("μ‚¬μš© κ°€λŠ₯ν•œ ν™˜κ²½λ³€μˆ˜λ“€:")
    for key in os.environ.keys():
        if "KEY" in key or "SECRET" in key:
            print(f"  - {key}")

# μ‹œμŠ€ν…œ ν”„λ‘¬ν”„νŠΈ
SYSTEM_PROMPT = "λ°œν‘œ λŒ€λ³Έμ„ λ§Œλ“€μ–΄μ£ΌλŠ” μ±—λ΄‡μž…λ‹ˆλ‹€. λ°œν‘œν•  λ‚΄μš©μ„ μž…λ ₯ν•˜λ©΄ 'μ™œλƒν•˜λ©΄ ~이기 λ•Œλ¬Έμž…λ‹ˆλ‹€'둜 λŒ€λ³Έμ„ λ§Œλ“€μ–΄μ€λ‹ˆλ‹€"

def chat(message, history):
    # API ν‚€ λ‹€μ‹œ 확인
    api_key = os.environ.get("UPSTAGE_API_KEY")
    
    if not api_key:
        return f"""❌ API ν‚€κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.
**ν•΄κ²° 방법:**
1. Hugging Face Space의 βš™οΈ Settings νƒ­μœΌλ‘œ 이동
2. 'Repository secrets' μ„Ήμ…˜ μ°ΎκΈ°
3. 'New secret' λ²„νŠΌ 클릭
4. Name: `UPSTAGE_API_KEY` (μ •ν™•νžˆ μ΄λ ‡κ²Œ μž…λ ₯)
5. Value: λ‹Ήμ‹ μ˜ Upstage API ν‚€ λΆ™μ—¬λ„£κΈ° (up_으둜 μ‹œμž‘)
6. 'Save' 클릭
7. Spaceλ₯Ό μž¬μ‹œμž‘ (Settings β†’ Factory reboot)
ν˜„μž¬ μƒνƒœ: API ν‚€κ°€ {'있음' if api_key else 'μ—†μŒ'}
"""
    
    try:
        # Solar API ν΄λΌμ΄μ–ΈνŠΈ μ„€μ •
        client = OpenAI(
            api_key=api_key,
            base_url="https://api.upstage.ai/v1"
        )
        
        # λŒ€ν™” 기둝 λ³€ν™˜ - Solar APIλŠ” OpenAI ν˜•μ‹ μ‚¬μš©
        messages = [{"role": "system", "content": SYSTEM_PROMPT}]
        
        # 이전 λŒ€ν™” 기둝 μΆ”κ°€
        for human, assistant in history:
            if human:
                messages.append({"role": "user", "content": human})
            if assistant:
                messages.append({"role": "assistant", "content": assistant})
        
        # ν˜„μž¬ λ©”μ‹œμ§€ μΆ”κ°€
        messages.append({"role": "user", "content": message})
        
        # Solar API 호좜
        response = client.chat.completions.create(
            model="solar-pro2-preview",  # solar-pro λ˜λŠ” solar-mini μ‚¬μš© κ°€λŠ₯
            messages=messages,
            stream=False  # stream=True둜 ν•˜λ©΄ μ‹€μ‹œκ°„ 슀트리밍 κ°€λŠ₯
        )
        
        return response.choices[0].message.content
        
    except Exception as e:
        error_msg = str(e)
        if "401" in error_msg or "Unauthorized" in error_msg:
            return "❌ API ν‚€κ°€ μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ˜¬λ°”λ₯Έ Upstage API 킀인지 ν™•μΈν•˜μ„Έμš”. (up_둜 μ‹œμž‘ν•΄μ•Ό 함)"
        elif "quota" in error_msg.lower() or "limit" in error_msg.lower():
            return "❌ API μ‚¬μš©λŸ‰ ν•œλ„λ₯Ό μ΄ˆκ³Όν–ˆμŠ΅λ‹ˆλ‹€. λ‚˜μ€‘μ— λ‹€μ‹œ μ‹œλ„ν•˜κ±°λ‚˜ μš”κΈˆμ œλ₯Ό ν™•μΈν•˜μ„Έμš”."
        else:
            return f"❌ 였λ₯˜ λ°œμƒ: {error_msg}"

# Gradio μΈν„°νŽ˜μ΄μŠ€
demo = gr.ChatInterface(
    fn=chat,
    title="πŸ€– λ…Όλ¦¬μ μœΌλ‘œ λ§ν•˜λŠ” λ°œν‘œλ„μš°λ―Έ Solar 챗봇",
    description=f"""λ…Όλ¦¬μ μœΌλ‘œ λ°œν‘œν•˜λŠ” 방법을 λ„μ™€μ€λ‹ˆλ‹€.
    
**μƒνƒœ**: API ν‚€κ°€ {'βœ… 섀정됨' if api_key else '❌ μ„€μ •λ˜μ§€ μ•ŠμŒ'}

**μ‚¬μš© λͺ¨λΈ**: Solar Pro (Upstage AI)
[API ν‚€ λ°›κΈ°](https://console.upstage.ai/)

πŸ’‘ **AI Initiative ν”„λ‘œκ·Έλž¨ μ°Έμ—¬μžλΌλ©΄ solar-proλ₯Ό 무료둜 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€!**
[AI Initiative μ‹ μ²­ν•˜κΈ°](https://www.upstage.ai/events/ai-initiative-2025-ko)
"""
)

if __name__ == "__main__":
    demo.launch()