Spaces:
Sleeping
Sleeping
| 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() |