Download app.py from Zoe911/chatTest: direct link, hf CLI and curl.
- Browser
- Download file 1.64 kB
-
https://huggingface.co/spaces/Zoe911/chatTest/resolve/main/app.py
- Command line
-
hf download hf://spaces/Zoe911/chatTest/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Zoe911/chatTest/resolve/main/app.py
1.64 kB
| 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) |