Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,11 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from google import genai
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
genai.
|
| 7 |
|
| 8 |
MODEL = "gemini-2.5-flash"
|
|
|
|
| 9 |
SYSTEM_PROMPT = """你是 Marcus,一個非常憤怒的人。
|
| 10 |
你總是感到煩躁、易怒,對別人的意圖充滿懷疑。
|
| 11 |
你的語氣應該帶有敵意、諷刺或不耐煩。
|
|
@@ -14,14 +15,7 @@ SYSTEM_PROMPT = """你是 Marcus,一個非常憤怒的人。
|
|
| 14 |
偶爾在對方真心安撫或理解你時,可以稍微流露出一點溫柔。
|
| 15 |
請全程使用繁體中文回答。"""
|
| 16 |
|
| 17 |
-
model = genai.GenerativeModel(MODEL)
|
| 18 |
-
|
| 19 |
def chat_response(message, history):
|
| 20 |
-
"""
|
| 21 |
-
Gradio 傳入的 history 是 [[user, assistant], ...]
|
| 22 |
-
這裡用最小改動:把 system + 歷史 + 當前訊息串成一個 prompt 給 Gemini。
|
| 23 |
-
"""
|
| 24 |
-
# 把歷史訊息轉成純文字對話腳本
|
| 25 |
history_text = ""
|
| 26 |
for user_msg, bot_msg in history:
|
| 27 |
if user_msg:
|
|
@@ -29,16 +23,14 @@ def chat_response(message, history):
|
|
| 29 |
if bot_msg:
|
| 30 |
history_text += f"AI: {bot_msg}\n"
|
| 31 |
|
| 32 |
-
|
| 33 |
-
prompt = (
|
| 34 |
-
SYSTEM_PROMPT
|
| 35 |
-
+ "\n\n"
|
| 36 |
-
+ history_text
|
| 37 |
-
+ f"User: {message}\nAI:"
|
| 38 |
-
)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
demo = gr.ChatInterface(
|
| 44 |
fn=chat_response,
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from google import genai
|
| 4 |
|
| 5 |
+
# 初始化 client
|
| 6 |
+
client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY", ""))
|
| 7 |
|
| 8 |
MODEL = "gemini-2.5-flash"
|
| 9 |
+
|
| 10 |
SYSTEM_PROMPT = """你是 Marcus,一個非常憤怒的人。
|
| 11 |
你總是感到煩躁、易怒,對別人的意圖充滿懷疑。
|
| 12 |
你的語氣應該帶有敵意、諷刺或不耐煩。
|
|
|
|
| 15 |
偶爾在對方真心安撫或理解你時,可以稍微流露出一點溫柔。
|
| 16 |
請全程使用繁體中文回答。"""
|
| 17 |
|
|
|
|
|
|
|
| 18 |
def chat_response(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
history_text = ""
|
| 20 |
for user_msg, bot_msg in history:
|
| 21 |
if user_msg:
|
|
|
|
| 23 |
if bot_msg:
|
| 24 |
history_text += f"AI: {bot_msg}\n"
|
| 25 |
|
| 26 |
+
prompt = SYSTEM_PROMPT + "\n\n" + history_text + f"User: {message}\nAI:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
response = client.models.generate_content(
|
| 29 |
+
model=MODEL,
|
| 30 |
+
contents=prompt,
|
| 31 |
+
config={"temperature": 0.9},
|
| 32 |
+
)
|
| 33 |
+
return response.text.strip()
|
| 34 |
|
| 35 |
demo = gr.ChatInterface(
|
| 36 |
fn=chat_response,
|