CoolFace
Apppublic

losttiger/therapist_chatbot

sourceHugging Facemitupdated 5mo agoView on Hugging Face
0likes
app.py102 linesDownload Raw Back to root
1from flask import Flask, render_template, request, jsonify2import re3import random4 5app = Flask(__name__)6 7# --- 扩展后的心理医生规则库 ---8rules = {9    # 表达需求10    r'I need (.*)': [11        "It sounds like {0} is very important to you. What would changing that mean for your life?",12        "I hear you. If you had {0} right now, how would your day feel different?",13        "Could you tell me more about what's making you feel the need for {0}?"14    ],15    # 感到压力或负面情绪16    r'I feel (.*)': [17        "I'm sorry you're feeling {0}. When did you first notice this feeling starting?",18        "It's completely valid to feel {0}. How does that feeling manifest in your body?",19        "If that feeling of {0} had a voice, what do you think it would be trying to tell you?"20    ],21    # 提到家庭(心理学核心)22    r'.* mother .*|.* father .*|.* parents .*': [23        "Family dynamics often shape how we see the world. How has your relationship with them evolved lately?",24        "Tell me more about how that family connection influences your current situation.",25        "It sounds like there's deep history there. What's one thing you wish they understood about you?"26    ],27    # 提到“总是”或“从不”(挑战极端思维)28    r'.* always .*|.* never .*': [29        "That sounds like a very heavy burden. Can you think of even one small exception to that?",30        "When we feel like it's 'always' this way, it can be exhausting. What's one thing you *can* control today?",31    ],32    # 询问为什么33    r'Why (.*)': [34        "That's a profound question. What's your own intuition telling you about the answer?",35        "Sometimes 'why' is harder to answer than 'how'. How are you coping with not knowing yet?",36    ],37    # 默认回复:使用共情引导38    r'.*': [39        "I'm listening. Please continue with that thought.",40        "Thank you for sharing that with me. How does it feel to say that out loud?",41        "That's interesting. Can you help me understand more about that from your perspective?",42        "I see. And what else is on your mind today?"43    ]44}45 46pronoun_swap = {47    "i": "you", "you": "i", "me": "you", "my": "your",48    "am": "are", "are": "am", "was": "were", "i'd": "you would",49    "i've": "you have", "i'll": "you will", "yours": "mine", "mine": "yours"50}51 52def swap_pronouns(phrase):53    words = phrase.lower().split()54    swapped_words = [pronoun_swap.get(word, word) for word in words]55    return " ".join(swapped_words)56 57def get_response(user_input):58    for pattern, responses in rules.items():59        match = re.search(pattern, user_input, re.IGNORECASE)60        if match:61            captured_group = match.group(1) if match.groups() else ''62            swapped_group = swap_pronouns(captured_group)63            response = random.choice(responses)64            if "{0}" in response:65                return response.format(swapped_group)66            return response67    return random.choice(rules[r'.*'])68 69# --- 网页路由 ---70 71@app.route('/')72def index():73    return render_template('index.html')74 75@app.route('/chat', methods=['POST'])76def chat():77    user_text = request.json.get("message")78    if not user_text:79        return jsonify({"response": "I'm here for you. Please take your time."})80    81    bot_response = get_response(user_text)82    return jsonify({"response": bot_response})83 84# --- 新增:用户反馈路由 ---85@app.route('/feedback', methods=['POST'])86def feedback():87    user_feedback = request.json.get("feedback")88    89    # 根据反馈类型给出不同的回复90    if user_feedback == "Helpful":91        response = "I'm so glad I could support you. We're making progress together. ✨"92    else:93        response = "I appreciate your honesty. I'm still learning—how can I better support you right now? ❤️"94        95    # 在后台日志中记录(可以在 Hugging Face 的 Logs 里查看)96    print(f"USER FEEDBACK RECEIVED: {user_feedback}")97    98    return jsonify({"response": response})99 100if __name__ == '__main__':101    # 必须监听 0.0.0.0 和 7860 端口以适配 Hugging Face102    app.run(host="0.0.0.0", port=7860)