|
import spaces |
|
import gradio as gr |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
from huggingface_hub import whoami |
|
import os |
|
|
|
os.system("rm -rf /data-nvme/zerogpu-offload/*") |
|
|
|
|
|
system_prompt = """你是 Skywork-o1,Skywork AI 开发的思维模型,擅长通过深度思考解决涉及数学、编码和逻辑推理的复杂问题。面对用户请求时,你首先会进行一段漫长而深入的思考过程,探索问题的可能解决方案。完成思考后,你会在回复中详细解释解决过程。""" |
|
|
|
|
|
model_name = "Skywork/Skywork-o1-Open-Llama-3.1-8B" |
|
model = AutoModelForCausalLM.from_pretrained( |
|
model_name, |
|
torch_dtype="auto", |
|
device_map="auto" |
|
) |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
|
|
|
@spaces.GPU |
|
def respond( |
|
message, |
|
history: list[tuple[str, str]], |
|
system_message, |
|
max_tokens, |
|
temperature, |
|
top_p, |
|
): |
|
|
|
conversation = [{"role": "system", "content": system_message}] |
|
for user_msg, assistant_msg in history: |
|
if user_msg: |
|
conversation.append({"role": "user", "content": user_msg}) |
|
if assistant_msg: |
|
conversation.append({"role": "assistant", "content": assistant_msg}) |
|
|
|
conversation.append({"role": "user", "content": message}) |
|
|
|
|
|
input_ids = tokenizer.apply_chat_template( |
|
conversation, |
|
tokenize=True, |
|
add_generation_prompt=True, |
|
return_tensors="pt" |
|
).to(model.device) |
|
|
|
|
|
generation = model.generate( |
|
input_ids=input_ids, |
|
max_new_tokens=max_tokens, |
|
do_sample=True, |
|
temperature=temperature, |
|
top_p=top_p, |
|
pad_token_id=tokenizer.pad_token_id, |
|
) |
|
|
|
|
|
completion = tokenizer.decode( |
|
generation[0][len(input_ids[0]):], |
|
skip_special_tokens=True, |
|
clean_up_tokenization_spaces=True |
|
) |
|
return completion |
|
|
|
|
|
demo = gr.ChatInterface( |
|
fn=respond, |
|
additional_inputs=[ |
|
gr.Textbox(value=system_prompt, label="System message"), |
|
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), |
|
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), |
|
gr.Slider( |
|
minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)" |
|
), |
|
], |
|
|
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|