# 필요한 라이브러리를 가져옵니다. import gradio as gr import google.generativeai as genai import os # --- UI 및 챗봇 설명 --- # Gradio Blocks를 사용하여 좀 더 유연한 UI를 구성합니다. with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo: gr.Markdown( """ # ♊️ Gemini API 챗봇 (Secrets 사용) Google Gemini API를 사용하는 챗봇입니다. Hugging Face Spaces의 'Settings' 탭에 있는 'Repository secrets'에 `GEMINI_API_KEY`가 설정되어 있어야 합니다. [API 키 발급받기](https://aistudio.google.com/app/apikey) """ ) # Gradio 챗봇 UI 컴포넌트 chatbot = gr.Chatbot(label="Gemini 챗봇", height=600) with gr.Row(): # 사용자 메시지 입력란 msg = gr.Textbox( label="메시지 입력", placeholder="무엇이든 물어보세요...", scale=7, ) # 전송 버튼 submit_button = gr.Button("전송", variant="primary", scale=1) with gr.Accordion("고급 설정", open=False): # LLM의 역할을 정의하는 시스템 메시지 system_message = gr.Textbox( value="You are a helpful and friendly chatbot.", label="시스템 메시지" ) # 모델의 창의성을 조절하는 슬라이더 temperature = gr.Slider( minimum=0.0, maximum=1.0, value=0.7, step=0.1, label="Temperature" ) # 생성할 최대 토큰 수를 조절하는 슬라이더 max_tokens = gr.Slider( minimum=1, maximum=4096, value=1024, step=1, label="Max new tokens" ) # --- Gemini API 호출 함수 --- def respond(message, chat_history, system_prompt, temp, max_output_tokens): # 함수가 호출될 때마다 환경변수에서 API 키를 직접 가져옵니다. # 이렇게 하면 앱 시작 시점에 키를 못 불러오는 문제를 해결할 수 있습니다. GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY") # 환경변수에서 가져온 API 키가 없으면 안내 메시지를 띄웁니다. if not GEMINI_API_KEY: # UI에 직접 경고를 표시하기 위해 gr.Warning을 사용할 수 있지만, # 여기서는 채팅 응답으로 처리합니다. yield "⚠️ **오류**: `GEMINI_API_KEY`가 설정되지 않았습니다.\n\nHugging Face Spaces의 **Settings > Repository secrets**에 API 키를 추가했는지 확인해주세요." return try: # API 키를 설정합니다. genai.configure(api_key=GEMINI_API_KEY) except Exception as e: yield f"API 키 설정에 오류가 발생했습니다: {e}" return # 사용할 모델과 시스템 프롬프트를 설정합니다. model = genai.GenerativeModel( model_name='gemini-2.0-flash', # 최신 Flash 모델 사용 system_instruction=system_prompt ) # Gradio의 대화 기록을 Gemini API가 이해할 수 있는 형식으로 변환합니다. gemini_history = [] for user_msg, model_msg in chat_history: if user_msg: gemini_history.append({"role": "user", "parts": [user_msg]}) if model_msg: gemini_history.append({"role": "model", "parts": [model_msg]}) # 이전 대화 기록을 바탕으로 채팅 세션을 시작합니다. chat = model.start_chat(history=gemini_history) # 모델 생성 관련 설정을 구성합니다. generation_config = genai.types.GenerationConfig( temperature=temp, max_output_tokens=int(max_output_tokens), ) try: # 스트리밍 방식으로 메시지를 보내고 응답을 받습니다. response = chat.send_message( message, stream=True, generation_config=generation_config ) # 스트리밍 응답을 실시간으로 UI에 표시합니다. full_response = "" for chunk in response: if hasattr(chunk, 'text'): full_response += chunk.text yield full_response except Exception as e: # API 호출 중 에러가 발생하면 UI에 표시합니다. yield f"응답 생성 중 오류가 발생했습니다: {e}" # --- Gradio 이벤트 리스너 --- def on_submit(message, chat_history, system_prompt, temp, max_output_tokens): chat_history.append((message, None)) bot_response_stream = respond(message, chat_history, system_prompt, temp, max_output_tokens) for partial_response in bot_response_stream: chat_history[-1] = (message, partial_response) yield "", chat_history msg.submit( on_submit, [msg, chatbot, system_message, temperature, max_tokens], [msg, chatbot] ) submit_button.click( on_submit, [msg, chatbot, system_message, temperature, max_tokens], [msg, chatbot] ) if __name__ == "__main__": demo.launch(debug=True)