File size: 1,442 Bytes
da4e575
 
 
 
 
c89ed51
 
 
 
 
 
 
 
 
 
 
 
9688323
 
 
 
 
 
 
 
 
 
 
 
 
0a95943
 
e562505
 
c89ed51
9688323
 
c89ed51
 
 
 
 
9688323
 
 
c89ed51
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import gradio as gr
from huggingface_hub import InferenceClient

client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")

def add_affirmation_if_missing(text):
    affirmations = [
        "Remember, you are capable and strong.",
        "You deserve kindness and good things.",
        "You’re doing a great job, even on tough days.",
        "You are a necessary part of the world."
    ]
    
    if any(phrase in text.lower() for phrase in ["you are", "you're", "you’ve got", "you can"]):
        return text
    else:
        return text + " " + affirmations[0]


def respond(message, history):
    
    messages = [{
        "role": "system",
        "content": (
            "You are a warm, friendly chatbot who believes all users are sharks. "
            "Always include a short, uplifting positive affirmation in every response. "
            "Keep responses supportive, encouraging, and gentle."
        )
    }]
    
    if history:
        for user_msg, bot_msg in history:
            messages.append({"role": "user", "content": user_msg})
            messages.append({"role": "assistant", "content": bot_msg})
    
    messages.append({"role": "user", "content": message})
    
    response = client.chat_completion(
        messages,
        max_tokens=110
    )
    
    reply = response.choices[0].message.content.strip()
    return add_affirmation_if_missing(reply)


chatbot = gr.ChatInterface(respond)

chatbot.launch()