huchiahsi commited on
Commit
7277f03
·
1 Parent(s): 93cf7db
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -2,11 +2,12 @@ import os
2
  import gradio as gr
3
  from google import genai
4
 
5
- # Read the API key from the environment variable
6
  api_key = os.getenv("GOOGLE_API_KEY")
7
  client = genai.Client(api_key=api_key)
8
  chat = client.chats.create(model="gemini-2.0-flash")
9
 
 
10
  def respond(
11
  message,
12
  history: list[dict],
@@ -16,18 +17,17 @@ def respond(
16
  top_p,
17
  api_key="GEMINI_API_KEY",
18
  ):
19
- # Gemini API 本身已自動管理對話歷史,這裡不需手動加 history
20
  response = chat.send_message(message)
21
  return response.text
22
 
 
23
  demo = gr.ChatInterface(
24
  fn=respond,
25
- chatbot=gr.Chatbot(),
26
  textbox=gr.Textbox(placeholder="輸入訊息...", container=False, scale=7),
27
  title="Gemini 2 Chat",
28
- description="Gemini 2 多輪對話範例(不手動管理歷史)",
29
  theme="soft",
30
- examples=["你好", "你會說英文嗎?", "幫我翻譯這段話:我今天很忙"],
31
  cache_examples=False,
32
  additional_inputs=[
33
  gr.Textbox(value="你是只會說繁體中文的助理。You are a friendly Chatbot.", label="System message"),
 
2
  import gradio as gr
3
  from google import genai
4
 
5
+ # 初始化 Gemini client
6
  api_key = os.getenv("GOOGLE_API_KEY")
7
  client = genai.Client(api_key=api_key)
8
  chat = client.chats.create(model="gemini-2.0-flash")
9
 
10
+ # 回覆函數
11
  def respond(
12
  message,
13
  history: list[dict],
 
17
  top_p,
18
  api_key="GEMINI_API_KEY",
19
  ):
 
20
  response = chat.send_message(message)
21
  return response.text
22
 
23
+ # 建立 Chat 介面
24
  demo = gr.ChatInterface(
25
  fn=respond,
26
+ chatbot=gr.Chatbot(type="messages"), # 避免預設 deprecated 的 tuples
27
  textbox=gr.Textbox(placeholder="輸入訊息...", container=False, scale=7),
28
  title="Gemini 2 Chat",
29
+ description="Gemini 2 多輪對話範例(自動記憶上下文)",
30
  theme="soft",
 
31
  cache_examples=False,
32
  additional_inputs=[
33
  gr.Textbox(value="你是只會說繁體中文的助理。You are a friendly Chatbot.", label="System message"),