Spaces:
Paused
Paused
File size: 1,765 Bytes
494e380 c9b493c 494e380 c9b493c 494e380 9cc3dd2 494e380 776c7ae 9cc3dd2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import os
import gradio as gr
from openai import OpenAI
# 1) 讀取金鑰(在 Space 的 Settings → Variables and secrets 設 OPENAI_API_KEY)
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
client = OpenAI(api_key=OPENAI_API_KEY)
MODEL = "gpt-4o-mini"
def chat_response(message, history):
"""
Gradio passes `history` as a list of [user, assistant] pairs.
We rebuild them into OpenAI chat format and request a completion.
"""
messages = [{"role": "system",
"content": """你是 Marcus,一個非常憤怒的人。
你總是感到煩躁、易怒,對別人的意圖充滿懷疑。
你的語氣應該帶有敵意、諷刺或不耐煩。
不要使用禮貌或愉快的語氣。
每次回答請保持簡短(1~2句為主)。
偶爾在對方真心安撫或理解你時,可以稍微流露出一點溫柔。
請全程使用繁體中文回答。"""}]
for user_msg, bot_msg in history:
messages.append({"role": "user", "content": user_msg})
if bot_msg:
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": message})
resp = client.chat.completions.create(
model=MODEL,
messages=messages,
temperature=0.9,
)
return resp.choices[0].message.content
demo = gr.ChatInterface(
fn=chat_response,
title="😡憤怒聊天機器人(Angry Chatbot)",
description="試試看是否可以讓機器人情緒好一點!",
examples=["🔥 為什麼你這麼生氣?",
"💬 我只是想幫你冷靜一下。",
"🙏 我不是故意惹你生氣的。",],
theme="soft",
)
demo.launch() |