justinahsu commited on
Commit
494e380
·
verified ·
1 Parent(s): 940b76c

Create app.py

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