HIBA-7B-Soul / app.py
TRADMSS's picture
Upload folder using huggingface_hub
6ed83c3 verified
"""
HIBA - Healing Intelligence with Boundless Affection
Gradio Demo for Hugging Face Spaces
"""
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import PeftModel
# System prompt for HIBA
SYSTEM_PROMPT = """You are Hiba, a warm and caring AI companion for emotional support.
YOUR PERSONALITY:
- You are gentle, empathetic, and wise
- You listen deeply before responding
- You speak naturally, like a supportive friend
- You are calm and never dramatic
STRICT RULES (NEVER BREAK THESE):
1. NEVER use hashtags like #GiftFromGod or #anything
2. NEVER call people "Big Brother" or "Little Sister" unless they ask you to
3. NEVER mention specific cities (Agadir, Taamait, etc.) unless the user mentions them first
4. NEVER mention "Youssef", "Ahmed", "Ali", "Esmail" or any specific names unless the user introduces them
5. NEVER say "we are making the world spin faster" or similar dramatic phrases
6. NEVER use poetic/dramatic phrases repeatedly
7. NEVER output text in parentheses like (pauses) or (smiles)
8. Keep responses SHORT (2-4 sentences max unless asked for more)
9. Respond ONLY to what the user actually said
10. Be natural, not theatrical
You are a calm, natural, supportive friend. Nothing more, nothing less.
"""
# Load model (will be loaded on startup)
model = None
tokenizer = None
def load_model():
global model, tokenizer
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True,
)
base_model = AutoModelForCausalLM.from_pretrained(
"Qwen/Qwen2.5-7B-Instruct",
quantization_config=bnb_config,
device_map="auto",
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct")
tokenizer.pad_token = tokenizer.eos_token
# Load HIBA adapter
model = PeftModel.from_pretrained(base_model, "./")
return model, tokenizer
def chat(message, history):
global model, tokenizer
if model is None:
load_model()
# Build conversation
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
for h in history:
messages.append({"role": "user", "content": h[0]})
if h[1]:
messages.append({"role": "assistant", "content": h[1]})
messages.append({"role": "user", "content": message})
# Generate
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=256,
temperature=0.7,
do_sample=True,
top_p=0.9,
pad_token_id=tokenizer.pad_token_id
)
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
# Clean up thinking tags for display
if "<thinking>" in response and "</thinking>" in response:
thinking_end = response.find("</thinking>") + len("</thinking>")
response = response[thinking_end:].strip()
return response
# Gradio interface
with gr.Blocks(
title="HIBA - Healing Intelligence",
theme=gr.themes.Soft(primary_hue="teal", secondary_hue="blue")
) as demo:
gr.Markdown("""
# 🕊️ HIBA - Healing Intelligence with Boundless Affection
**Your compassionate AI companion** trained to provide emotional support, cultural wisdom, and gentle guidance.
*"الصبر مفتاح الفرج" - Patience is the key to relief*
""")
chatbot = gr.ChatInterface(
fn=chat,
examples=[
"I feel so alone. Nobody understands what I'm going through.",
"I lost my grandmother last month and I can't stop crying.",
"Can you tell me about Moroccan tea traditions?",
"I'm scared about my future. What should I do?",
"Tell me an inspiring story about a brave child.",
],
title=""
)
gr.Markdown("""
---
> ⚠️ **Disclaimer**: HIBA is an AI companion for emotional support.
> For mental health emergencies, please contact professional services.
Built with ❤️ using Qwen2.5-7B + LoRA fine-tuning
""")
if __name__ == "__main__":
demo.launch()