File size: 1,471 Bytes
226a535
cb245e5
 
d034f8c
732091b
 
 
 
d034f8c
732091b
 
 
cb245e5
732091b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import google.generativeai as genai
import os

# API ν‚€ μ„€μ •
api_key = os.environ.get("GEMINI_API_KEY")
if api_key:
    genai.configure(api_key=api_key)

def chat(message, history):
    if not api_key:
        return "❌ API ν‚€κ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. HF Spaces Settingsμ—μ„œ GEMINI_API_KEYλ₯Ό μΆ”κ°€ν•˜μ„Έμš”."
    
    try:
        # Gemini λͺ¨λΈ μ΄ˆκΈ°ν™”
        model = genai.GenerativeModel('gemini-2.0-flash')
        
        # λŒ€ν™” 기둝 λ³€ν™˜
        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:
        return f"❌ 였λ₯˜ λ°œμƒ: {str(e)}"

# Gradio μΈν„°νŽ˜μ΄μŠ€
demo = gr.ChatInterface(
    fn=chat,
    title="πŸ€– Gemini 챗봇",
    description="Google Gemini APIλ₯Ό μ‚¬μš©ν•œ κ°„λ‹¨ν•œ μ±—λ΄‡μž…λ‹ˆλ‹€.",
    examples=["μ•ˆλ…•ν•˜μ„Έμš”!", "였늘 λ‚ μ”¨λŠ” μ–΄λ•Œ?", "νŒŒμ΄μ¬μ— λŒ€ν•΄ μ„€λͺ…ν•΄μ€˜"],
    retry_btn=None,
    undo_btn="이전 λŒ€ν™” μ‚­μ œ",
    clear_btn="전체 λŒ€ν™” μ‚­μ œ",
)

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