Spaces:
Sleeping
Sleeping
Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import gradio as gr
# 一些示範資料,可按需加入更多內容或改寫positive_quotes = [ "每一個黑暗的夜晚,都是迎接光明的前奏。", "黑暗之後,總有黎明。", "所有困難都是成長的墊腳石。",]
crisis_keywords = ["不想活", "死了算了", "結束痛苦", "想自殺", "受夠了"]
def ai_reply(user_input, chat_history): reply = "" user_text = user_input.lower() # 危機詞偵測 if any(keyword in user_text for keyword in crisis_keywords): reply = ( "聽到你這麼說,我很擔心你的安全。" "你並不孤單,請務必與身邊信任的人聊聊,或聯繫專業心理師。" "需要我幫你找心理求助資源嗎?" ) # 簡單放鬆練習觸發關鍵詞 elif "放鬆" in user_text or "呼吸" in user_text or "冥想" in user_text: reply = ( "讓我們一起做一個簡單的深呼吸練習:\n" "慢慢吸氣,數到4……1…2…3…4…\n" "慢慢呼氣,數到6……1…2…3…4…5…6…\n" "重複3次,感覺如何?" ) # 用戶表示情緒不好 elif "不好" in user_text or "難過" in user_text or "焦慮" in user_text: reply = ( "我聽到你感覺不太好,想和我聊聊原因嗎?我在這裡陪你。" ) # 用戶表示感覺孤單 elif "孤單" in user_text or "沒有人懂" in user_text: reply = ( "你並不孤單,很多人都有相似的感受。" "願意多和我說說你的想法嗎?" ) # 用戶想聽正能量 elif "正能量" in user_text or "鼓勵" in user_text: import random reply = "今天的心靈雞湯:" + random.choice(positive_quotes) # 預設回應 else: reply = ( "謝謝你分享。如果你願意,可以告訴我更多," "或者說說你今天的心情怎樣?我在這裡傾聽。" ) chat_history.append((user_input, reply)) return chat_history, chat_history
with gr.Blocks() as demo: gr.Markdown("# EmpathyPulse AI 心理互動陪伴") chatbot = gr.Chatbot() state = gr.State([])
with gr.Row(): txt = gr.Textbox(show_label=False, placeholder="跟我聊聊你的心情...")
def respond(message, chat_history): return ai_reply(message, chat_history)
txt.submit(respond, [txt, state], [chatbot, state]) txt.submit(lambda: "", None, txt) # 送出後清空輸入框
demo.launch(server_name="0.0.0.0", server_port=7860)app.pyAdded+70