ChatBot / app.py
huchiahsi's picture
use chatgpt to modify code
93cf7db
raw
history blame
1.4 kB
import os
import gradio as gr
from google import genai
# Read the API key from the environment variable
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",
):
# Gemini API 本身已自動管理對話歷史,這裡不需手動加 history
response = chat.send_message(message)
return response.text
demo = gr.ChatInterface(
fn=respond,
chatbot=gr.Chatbot(),
textbox=gr.Textbox(placeholder="輸入訊息...", container=False, scale=7),
title="Gemini 2 Chat",
description="Gemini 2 多輪對話範例(不手動管理歷史)",
theme="soft",
examples=["你好", "你會說英文嗎?", "幫我翻譯這段話:我今天很忙"],
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()