|
import gradio as gr |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
import torch |
|
import spaces |
|
|
|
|
|
model_name = "Zhihu-ai/Zhi-writing-dsr1-14b" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
model_name, |
|
torch_dtype=torch.float16, |
|
device_map="auto", |
|
trust_remote_code=True |
|
) |
|
@spaces.GPU() |
|
def predict(message, history): |
|
|
|
history_text = "" |
|
for human, assistant in history: |
|
history_text += f"Human: {human}\nAssistant: {assistant}\n" |
|
prompt = f"{history_text}Human: {message}\nAssistant:" |
|
|
|
|
|
inputs = tokenizer(prompt, return_tensors="pt").to(model.device) |
|
outputs = model.generate( |
|
**inputs, |
|
max_new_tokens=10000, |
|
do_sample=True, |
|
temperature=0.7, |
|
top_p=0.9, |
|
repetition_penalty=1.1, |
|
pad_token_id=tokenizer.eos_token_id |
|
) |
|
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True) |
|
|
|
return response.strip() |
|
|
|
|
|
demo = gr.ChatInterface( |
|
predict, |
|
title="测试Zhi-writing-dsr1-14b", |
|
description="Zhihu-ai/Zhi-writing-dsr1-14b", |
|
examples=["鲁迅口吻写五百字,描述桔猫的可爱!", "桔了个仔是谁", "介绍自己"], |
|
theme=gr.themes.Soft() |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch(share=True) |
|
|
|
|
|
|