Spaces:
Sleeping
Sleeping
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() |