mychatbot / app.py
huchiahsi's picture
mode2
7277f03
raw
history blame
1.29 kB
import os
import gradio as gr
from google import genai
# 初始化 Gemini client
api_key = os.getenv("GOOGLE_API_KEY")
client = genai.Client(api_key=api_key)
chat = client.chats.create(model="gemini-2.0-flash")
# 回覆函數
def respond(
message,
history: list[dict],
system_message,
max_tokens,
temperature,
top_p,
api_key="GEMINI_API_KEY",
):
response = chat.send_message(message)
return response.text
# 建立 Chat 介面
demo = gr.ChatInterface(
fn=respond,
chatbot=gr.Chatbot(type="messages"), # 避免預設 deprecated 的 tuples
textbox=gr.Textbox(placeholder="輸入訊息...", container=False, scale=7),
title="Gemini 2 Chat",
description="Gemini 2 多輪對話範例(自動記憶上下文)",
theme="soft",
cache_examples=False,
additional_inputs=[
gr.Textbox(value="你是只會說繁體中文的助理。You are a friendly Chatbot.", label="System message"),
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
],
)
if __name__ == "__main__":
demo.launch()