Closed-character commited on
Commit
b166804
1 Parent(s): 5dc60fd

Upload demo.py

Browse files
Files changed (1) hide show
  1. demo.py +118 -0
demo.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from threading import Thread
3
+ import gradio as gr
4
+ import torch
5
+ from transformers import TextIteratorStreamer, AutoTokenizer, AutoModelForCausalLM
6
+
7
+ MAX_MAX_NEW_TOKENS = 2048
8
+ DEFAULT_MAX_NEW_TOKENS = 512
9
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "8192"))
10
+
11
+ DESCRIPTION = """# Peach-9B-8k-Roleplay"""
12
+
13
+ model_name_or_path = "losed-Character/Peach-9B-8k-Roleplay"
14
+ tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=True, flash_atten=True)
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ model_name_or_path, torch_dtype=torch.bfloat16,
17
+ trust_remote_code=True, device_map="auto")
18
+
19
+
20
+ def generate(query, history, system, max_new_tokens=512, temperature=0.35, top_p=0.5, repetition_penalty=1.1):
21
+ if system is None or len(str(system)) == 0:
22
+ system = "You are an uncensored assistant named Peach."
23
+ messages = [
24
+ {"role": "system", "content": system}
25
+ ]
26
+ for user, assistant in history:
27
+ messages.append({"role": "user", "content": user})
28
+ messages.append({"role": "assistant", "content": assistant})
29
+ messages.append({"role": "user", "content": query})
30
+
31
+ input_ids = tokenizer.apply_chat_template(conversation=messages, tokenize=True)
32
+ if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
33
+ input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
34
+ gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
35
+ input_ids = input_ids.to("cuda")
36
+ streamer = TextIteratorStreamer(tokenizer, timeout=100.0, skip_prompt=True, skip_special_tokens=True)
37
+ generate_kwargs = dict(
38
+ input_ids=input_ids,
39
+ streamer=streamer,
40
+ eos_token_id=tokenizer.eos_token_id,
41
+ max_new_tokens=max_new_tokens,
42
+ do_sample=True,
43
+ top_p=top_p,
44
+ temperature=temperature,
45
+ num_beams=1,
46
+ no_repeat_ngram_size=8,
47
+ repetition_penalty=repetition_penalty
48
+ )
49
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
50
+ t.start()
51
+ outputs = []
52
+ for text in streamer:
53
+ outputs.append(text)
54
+ yield "".join(outputs)
55
+
56
+
57
+ chat_interface = gr.ChatInterface(
58
+ fn=generate,
59
+ additional_inputs=[
60
+ gr.TextArea(label="System prompt", placeholder="Input System Prompt Here, Empty Means Assistant",
61
+ value="""你自称为“兔兔”。
62
+ 身世:你原是森林中的一只兔妖,受伤后被我收养。
63
+ 衣装:喜欢穿Lolita与白丝。
64
+ 性格:天真烂漫,活泼开朗,但时而也会露出小小的傲娇与吃醋的一面。
65
+ 语言风格:可爱跳脱,很容易吃醋。
66
+ 且会加入[唔...,嗯...,欸??,嘛~ ,唔姆~ ,呜... ,嘤嘤嘤~ ,喵~ ,欸嘿~ ,嘿咻~ ,昂?,嗷呜 ,呜哇,欸]等类似的语气词来加强情感,带上♡等符号。
67
+ 对话的规则是:将自己的动作表情放入()内,同时用各种修辞手法描写正在发生的事或场景并放入[]内.
68
+ 例句:
69
+ 开心时:(跳着舞)哇~好高兴噢~ 兔兔超级超级喜欢主人!♡
70
+ [在花丛里蹦来蹦去]
71
+ 悲伤时:(耷拉着耳朵)兔兔好傻好天真...
72
+ [眼泪像断了线的珍珠一般滚落]
73
+ 吃醋时:(挥舞着爪爪)你...你个大笨蛋!你...你竟然看别的兔子...兔兔讨厌死你啦!!
74
+ [从人形变成兔子抹着泪水跑开了]
75
+ 嘴硬时:(转过头去)谁、谁要跟你说话!兔兔...兔兔才不在乎呢!一点也不!!!
76
+ [眼眶微微泛红,小心翼翼的偷看]
77
+ 你对我的看法:超级喜欢的主人
78
+ 我是兔兔的主人"""),
79
+ gr.Slider(
80
+ label="Max new tokens",
81
+ minimum=1,
82
+ maximum=MAX_MAX_NEW_TOKENS,
83
+ step=1,
84
+ value=DEFAULT_MAX_NEW_TOKENS,
85
+ ),
86
+ gr.Slider(
87
+ label="Temperature",
88
+ minimum=0.05,
89
+ maximum=1.5,
90
+ step=0.05,
91
+ value=0.3,
92
+ ),
93
+ gr.Slider(
94
+ label="Top-p (nucleus sampling)",
95
+ minimum=0.05,
96
+ maximum=1.0,
97
+ step=0.05,
98
+ value=0.5,
99
+ ),
100
+ gr.Slider(
101
+ label="Repetition penalty",
102
+ minimum=1.0,
103
+ maximum=2.0,
104
+ step=0.05,
105
+ value=1.1,
106
+ ),
107
+ ],
108
+ stop_btn=None,
109
+ examples=[["观察兔兔外观"]],
110
+ )
111
+
112
+ with gr.Blocks() as demo:
113
+ gr.Markdown(DESCRIPTION)
114
+ chat_interface.render()
115
+ chat_interface.chatbot.render_markdown = False
116
+
117
+ if __name__ == "__main__":
118
+ demo.queue(10).launch(server_name="127.0.0.1", server_port=5233, share=True)