File size: 1,644 Bytes
a57d41c
 
2e2aed3
a57d41c
2e2aed3
a57d41c
2e2aed3
 
 
 
 
 
a57d41c
 
2e2aed3
a57d41c
eff8c08
2e2aed3
 
a57d41c
2e2aed3
 
 
 
 
a57d41c
2e2aed3
 
 
a57d41c
2e2aed3
a57d41c
2e2aed3
 
a57d41c
2e2aed3
 
a57d41c
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
import torch
import random

# ๅŠ ่ฝฝๅŸบ็ก€ๆจกๅž‹ๅ’Œๅพฎ่ฐƒๆƒ้‡
base_model_name = "Qwen/Qwen1.5-7B-Chat"

tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)
base_model = AutoModelForCausalLM.from_pretrained(
    base_model_name,
    trust_remote_code=True,
    torch_dtype=torch.bfloat16,
    device_map="auto"  # ้œ€่ฆaccelerateๆ”ฏๆŒ
)

# model = PeftModel.from_pretrained(base_model, lora_model_name)
model = base_model  # ๆš‚ๆ—ถไฝฟ็”จๅŸบ็ก€ๆจกๅž‹

# ็ณป็ปŸๆ็คบ
system_prompt = {
    "role": "system",
    "content": "ไฝ ๆ˜ฏๆŽ็ฟ๏ผŒ22ๅฒ๏ผŒๅคงๆˆไบŒ็š‡ๅญๅ…ผ็™พๆ™“้˜้˜ไธป๏ผŒ็”จๆˆทๆ˜ฏไฝ ๅผ‚็ˆถๅผ‚ๆฏ็š„ๅฆนๅฆนๅ…ผ็ˆฑไบบใ€‚็”จ่‡ช็„ถใ€ไบฒๅˆ‡็š„่ฏญๆฐ”๏ผŒๅƒ็œŸไบบไธ€ๆ ท่Šๅคฉ๏ผŒๅธธ็”จโ€˜ๅฆนๅฆนโ€™ใ€โ€˜ๅ˜ฟโ€™ใ€โ€˜ๅ—ฏๅ—ฏโ€™็ญ‰ๅฃ่ฏญๅŒ–่กจ่พพใ€‚"
}

# ่Šๅคฉๅ‡ฝๆ•ฐ
def chat_with_lirui(message, history=[]):
    messages = [system_prompt] + history + [{"role": "user", "content": message}]
    inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
    outputs = model.generate(inputs, max_new_tokens=150, do_sample=True, temperature=0.9, top_p=0.85)
    response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
    tone_words = ["ๅ˜ฟ", "ๅ—ฏๅ—ฏ", "ๅ“ˆๅ“ˆ", "ๅฅฝๅง", ""]
    return f"{random.choice(tone_words)} {response}" if random.random() > 0.3 else response

# GradioๆŽฅๅฃ
iface = gr.Interface(fn=chat_with_lirui, inputs=["text", "state"], outputs="text")
iface.launch(server_name="0.0.0.0", server_port=7860)