File size: 2,765 Bytes
226a535
cb245e5
 
d034f8c
3271c7b
 
 
732091b
 
3271c7b
 
 
 
 
 
 
 
 
 
 
d034f8c
732091b
3271c7b
 
 
732091b
3271c7b
 
 
 
 
 
 
 
 
 
 
 
 
cb245e5
732091b
3271c7b
 
 
30b8dce
1b77547
30b8dce
 
732091b
 
 
 
 
 
 
 
 
3271c7b
732091b
1b77547
3271c7b
 
 
732091b
 
3271c7b
 
 
 
 
732091b
3271c7b
 
 
 
 
1b77547
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
import gradio as gr
import google.generativeai as genai
import os

# 디버깅: ν™˜κ²½λ³€μˆ˜ 확인
print("=== ν™˜κ²½λ³€μˆ˜ 디버깅 ===")
print(f"GEMINI_API_KEY 쑴재 μ—¬λΆ€: {'GEMINI_API_KEY' in os.environ}")
api_key = os.environ.get("GEMINI_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("GEMINI_API_KEY")
    
    if not api_key:
        return f"""❌ API ν‚€κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

**ν•΄κ²° 방법:**
1. Hugging Face Space의 βš™οΈ Settings νƒ­μœΌλ‘œ 이동
2. 'Repository secrets' μ„Ήμ…˜ μ°ΎκΈ°
3. 'New secret' λ²„νŠΌ 클릭
4. Name: `GEMINI_API_KEY` (μ •ν™•νžˆ μ΄λ ‡κ²Œ μž…λ ₯)
5. Value: λ‹Ήμ‹ μ˜ Gemini API ν‚€ λΆ™μ—¬λ„£κΈ°
6. 'Save' 클릭
7. Spaceλ₯Ό μž¬μ‹œμž‘ (Settings β†’ Factory reboot)

ν˜„μž¬ μƒνƒœ: API ν‚€κ°€ {'있음' if api_key else 'μ—†μŒ'}
"""
    
    try:
        # API μ„€μ •
        genai.configure(api_key=api_key)
        
        model = genai.GenerativeModel(
            'gemini-1.5-flash',
            system_instruction=SYSTEM_PROMPT
        )
        
        # λŒ€ν™” 기둝 λ³€ν™˜
        chat_history = []
        for human, assistant in history:
            if human:
                chat_history.append({"role": "user", "parts": [human]})
            if assistant:
                chat_history.append({"role": "model", "parts": [assistant]})
        
        # μ±„νŒ… μ„Έμ…˜ μ‹œμž‘
        chat_session = model.start_chat(history=chat_history)
        
        # 응닡 생성
        response = chat_session.send_message(message)
        return response.text
        
    except Exception as e:
        error_msg = str(e)
        if "API_KEY_INVALID" in error_msg:
            return "❌ API ν‚€κ°€ μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ˜¬λ°”λ₯Έ Gemini API 킀인지 ν™•μΈν•˜μ„Έμš”."
        else:
            return f"❌ 였λ₯˜ λ°œμƒ: {error_msg}"

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

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