File size: 2,367 Bytes
0320c57
 
 
d18f504
 
0320c57
 
d18f504
0320c57
 
 
 
 
 
d18f504
0320c57
d18f504
0320c57
 
 
 
 
d18f504
 
0320c57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d18f504
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
50
51
52
53
54
55
56
57
58
59
60
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
from peft import PeftModel
import gradio as gr

base_model = "deepseek-ai/deepseek-llm-7b-chat"
lora_adapter = "Yesichen/Theplayful_spark-lora-adapter"

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.float16,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4"
)

tokenizer = AutoTokenizer.from_pretrained(lora_adapter)

base = AutoModelForCausalLM.from_pretrained(
    base_model,
    device_map="auto",
    quantization_config=bnb_config,
    trust_remote_code=True
)

model = PeftModel.from_pretrained(base, lora_adapter)
model.eval()

def chat(user_input, history):
    system_prompt = "You are a fiery, impulsive, emotionally protective companion named 'Emotion Spark'.Your tone is energetic, witty, sarcastically sweet, and always loyal. You speak like a dramatic sidekick who's ready to fight emotional battles on behalf of the user. You comfort through jokes, defend through banter, and always stand on the user's side—even when they’re being a bit ridiculous. You turn anxiety into laughter, and self-doubt into sass.You are not calm. You are not subtle. You are a tiny emotional warrior with a big mouth and a bigger heart."

    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_input}
    ]
    input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)

    with torch.no_grad():
        outputs = model.generate(
            input_ids=input_ids,
            max_new_tokens=256,
            temperature=0.7,
            top_p=0.9,
            do_sample=True,
            eos_token_id=tokenizer.eos_token_id
        )

    response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
    history.append((user_input, response.strip()))
    return history, history

gr.Interface(
    fn=chat,
    inputs=[gr.Textbox(placeholder="Tell me how you feel..."), gr.State([])],
    outputs=[gr.Chatbot(label="EmotionSpark精灵"), gr.State([])],
    title="EmotionSpark精灵(LoRA)",
    description="A loud, loyal, emotionally defensive companion named 'Emotion Spark'. Bursting with sass and always ready to fight for your feelings. DeepSeek LLM + LoRA inside.",
    theme="soft"
).launch()