gradio-example / app.py
JUNGU's picture
Update app.py
732091b verified
raw
history blame
1.47 kB
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()