Views
No views yet
1import json
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_name = "blacker521/NewsPicGen"
5
6model = AutoModelForCausalLM.from_pretrained(
7 model_name,
8 torch_dtype="auto",
9 device_map="auto"
10)
11tokenizer = AutoTokenizer.from_pretrained(model_name)
12
13
14title = "孙颖莎谈大满贯最大的挑战"
15content = "#孙颖莎希望找到赛场上拼搏的状态# 9月24日,是WTT中国大满贯2024倒计时2天,球员@孙颖莎 接受专访。孙颖莎在采访中谈及大满贯中最大的挑战,她表示大满贯已经是很顶尖的赛事水平了,所以每场球都会有挑战,希望自己能找到积极专注的在赛场上拼搏的状态。"
16prompt = f'以下是一篇新闻,标题“{title}”。新闻内容:{content},请根据新闻内容生成绘画指令,图片要符合新闻内容,并且有创意。'
17messages = [
18 {"role": "user", "content": prompt}
19]
20text = tokenizer.apply_chat_template(
21 messages,
22 tokenize=False,
23 add_generation_prompt=True
24)
25model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
26
27generated_ids = model.generate(
28 **model_inputs,
29 max_new_tokens=512
30)
31generated_ids = [
32 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
33]
34
35response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
36print(json.dumps(response, ensure_ascii=False))
37# {
38# "ch_keyword": "挑战大满贯,全力以赴",
39# "ch_prompt": "画一个正在比赛中奋力拼搏的女子乒乓球运动员,她的面庞充满斗志和决心,手中握着乒乓球拍,眼睛紧盯着对手,背景为观众席上的欢呼声。",
40# "en_keyword": "Challenging Grand Slam",
41# "en_prompt": "Draw a female table tennis player in the middle of an intense match, her face filled with determination and resolve, holding a ping pong paddle in her hand, staring at her opponent closely, and the cheering from the audience in the background.",
42# "type": "2"
43# }