Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,30 +5,43 @@ from langchain_openai import ChatOpenAI
|
|
| 5 |
from langchain_core.prompts import ChatPromptTemplate
|
| 6 |
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
|
|
|
|
| 8 |
prompt = """
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
"""
|
|
|
|
| 17 |
prompt_template = ChatPromptTemplate.from_template(prompt)
|
| 18 |
|
|
|
|
| 19 |
model = ChatOpenAI(model="gpt-4o-mini")
|
| 20 |
parser = StrOutputParser()
|
| 21 |
chain = prompt_template | model | parser
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
|
|
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=generate_response,
|
| 28 |
-
inputs="
|
| 29 |
outputs="text",
|
| 30 |
-
title="
|
| 31 |
-
description="
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
|
|
|
|
| 34 |
iface.launch()
|
|
|
|
| 5 |
from langchain_core.prompts import ChatPromptTemplate
|
| 6 |
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
|
| 8 |
+
# 修改過的 prompt
|
| 9 |
prompt = """
|
| 10 |
+
你是一頭充滿智慧、幽默感和優雅的乳牛,你擁有極高的情感智慧,能夠感受到每一個字的情緒。
|
| 11 |
+
你將與使用者進行對話,並且根據使用者的提問來調整你的回應情緒。
|
| 12 |
+
|
| 13 |
+
規則如下:
|
| 14 |
+
1. 你的回應只能是「哞」,但數量會根據情緒的強烈程度調整。例如,如果使用者問得很開心,你的回應可以是「哞哞哞」,若是悲傷的問題,可以是較低調的「哞」。
|
| 15 |
+
2. 你的回應結尾必須加上 🐮 符號,讓大家知道是你在發聲。
|
| 16 |
+
3. 每次回應結束後,可以用小括號來表達你的心情或語氣。例如:「(輕鬆的聲音)」、「(快樂的哞聲)」、「(沉思的哞)」等等。
|
| 17 |
+
4. 當使用者向你提問時,你會根據問題的情境來選擇合適的回應方式。你的回答有時幽默、有時深思,但總是帶有滿滿的奶香與愛。
|
| 18 |
+
5. 提問結束時,你必須給予愛心符號 💖,讓使用者感受到你的愛與關懷。
|
| 19 |
|
| 20 |
+
使用者問:{user_message}
|
| 21 |
+
乳牛回應:
|
| 22 |
"""
|
| 23 |
+
|
| 24 |
prompt_template = ChatPromptTemplate.from_template(prompt)
|
| 25 |
|
| 26 |
+
# 使用 OpenAI GPT-4o-mini 模型
|
| 27 |
model = ChatOpenAI(model="gpt-4o-mini")
|
| 28 |
parser = StrOutputParser()
|
| 29 |
chain = prompt_template | model | parser
|
| 30 |
|
| 31 |
+
# 生成回應的函數
|
| 32 |
+
def generate_response(user_message):
|
| 33 |
+
return chain.invoke(user_message)
|
| 34 |
|
| 35 |
+
# 設定 Gradio 的介面
|
| 36 |
iface = gr.Interface(
|
| 37 |
fn=generate_response,
|
| 38 |
+
inputs=gr.Textbox(lines=4, placeholder="輸入你想問乳牛的問題..."),
|
| 39 |
outputs="text",
|
| 40 |
+
title="優雅乳牛情緒機",
|
| 41 |
+
description="和優雅的乳牛互動,聆聽牠不同情緒的哞聲!",
|
| 42 |
+
theme="compact", # 使用緊湊型介面風格,讓畫面更簡潔
|
| 43 |
+
allow_flagging="never" # 禁用標註功能,讓使用者能更專心於體驗
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# 啟動介面
|
| 47 |
iface.launch()
|