huchiahsi commited on
Commit
93cf7db
·
1 Parent(s): 2751b13

use chatgpt to modify code

Browse files
Files changed (1) hide show
  1. app.py +11 -28
app.py CHANGED
@@ -4,11 +4,9 @@ from google import genai
4
 
5
  # Read the API key from the environment variable
6
  api_key = os.getenv("GOOGLE_API_KEY")
7
-
8
  client = genai.Client(api_key=api_key)
9
  chat = client.chats.create(model="gemini-2.0-flash")
10
 
11
- # Removed helper functions and simplified the respond function.
12
  def respond(
13
  message,
14
  history: list[dict],
@@ -18,39 +16,24 @@ def respond(
18
  top_p,
19
  api_key="GEMINI_API_KEY",
20
  ):
21
- global chat
22
-
23
- # Send the user message to the chat
24
  response = chat.send_message(message)
 
25
 
26
- # Retrieve the chat history
27
- history = chat.get_history()
28
-
29
- # Format the response and history for display
30
- formatted_history = [
31
- {"role": msg.role, "content": msg.parts[0].text} for msg in history
32
- ]
33
-
34
- return response.text, formatted_history
35
-
36
- """
37
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
38
- """
39
- # Update the ChatInterface to use type='messages' and fix the respond function to handle history properly.
40
  demo = gr.ChatInterface(
41
- respond,
42
- type="messages", # Updated to use OpenAI-style 'role' and 'content' keys
 
 
 
 
 
 
43
  additional_inputs=[
44
  gr.Textbox(value="你是只會說繁體中文的助理。You are a friendly Chatbot.", label="System message"),
45
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
46
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
47
- gr.Slider(
48
- minimum=0.1,
49
- maximum=1.0,
50
- value=0.95,
51
- step=0.05,
52
- label="Top-p (nucleus sampling)",
53
- ),
54
  ],
55
  )
56
 
 
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
  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"),
34
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
35
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
36
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
37
  ],
38
  )
39