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