chiachun1224 commited on
Commit
314d736
·
verified ·
1 Parent(s): 4c8bab7

Update gemini_simple.py

Browse files
Files changed (1) hide show
  1. gemini_simple.py +6 -27
gemini_simple.py CHANGED
@@ -1,30 +1,9 @@
1
- # === Gemini Chatbot ===
2
- # 這是一個簡單的 Gemini 聊天機器人範例,使用 Gradio 建立聊天介面
3
- # 這個範例使用 Google 的 Gemini API,請確保你已經安裝了 google-genai 套件
4
- # 並且在環境變數中設置了 GOOGLE_API_KEY
5
- # https://googleapis.github.io/python-genai/index.html
6
-
7
- import os
8
  import gradio as gr
9
- from google import genai
10
-
11
- # 初始化 Gemini client
12
- api_key = os.getenv("GOOGLE_API_KEY")
13
- client = genai.Client(api_key=api_key)
14
- chat = client.chats.create(model="gemini-2.0-flash")
15
- system_prompt = "You are a helpful assistant and always respond in Traditional Chinese."
16
-
17
- # 回應函數(符合 type="messages"的格式)
18
- def respond(message,history):
19
- response = chat.send_message(f"{system_prompt}:{message}")
20
- return response.text
21
 
22
- # Gradio Chat Interface
23
- demo = gr.ChatInterface(
24
- fn=respond,
25
- type="messages",
26
- title="B09000000 黃老師",
27
- )
28
 
29
- if __name__ == "__main__":
30
- demo.launch()
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ def chat(message, history):
4
+ history = history or []
5
+ response = f"Gemini 回應你:{message}"
6
+ history.append((message, response))
7
+ return history, history
 
8
 
9
+ gr.ChatInterface(chat, title="B10090087 張嘉峻").launch()