joys631 commited on
Commit
dd5d418
1 Parent(s): 25bdda6

Create app-as_automodel.py

Browse files
Files changed (1) hide show
  1. app-as_automodel.py +81 -0
app-as_automodel.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from huggingface_hub import InferenceClient
2
+ import gradio as gr
3
+ import time
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+
6
+ model_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
8
+
9
+ model = AutoModelForCausalLM.from_pretrained(model_id)
10
+
11
+
12
+ # client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
13
+ #client = InferenceClient("meta-llama/Llama-2-70b-chat-hf")
14
+
15
+ def split_list(lst, chunk_size):
16
+ return [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)]
17
+
18
+ def format_prompt(message, history, system_prompt):
19
+ prompt = f"<s>[INST] <<SYS>>{system_prompt}<</SYS>> [/INST] </s>" if system_prompt else "<s>"
20
+ for user_prompt, bot_response in history:
21
+ prompt += f"[INST] {user_prompt} [/INST]"
22
+ prompt += f" {bot_response}</s> "
23
+ prompt += f"[INST] {message} [/INST]"
24
+ return prompt
25
+
26
+ def format_sys_prompt(username, botname, botpersonality, botexamplechat, chatsituation):
27
+ return f"You are a fictional character named \"{botname}\"\nHowever, don\'t say we\'re roleplaying, as to not break the immersion.\n\nPersonality of {botname}:\n{botpersonality}\n\nCurrent situation/place/time of this conversation:\n{chatsituation}\n\nIn this case, you will be talking to \"{username}\". You should not help to talk or act as \"{username}\". Leave everything by the user.\n\nChat formatting:\nChat: <\"Hello, this is a chat text. Chat text is enclosed with < as the starting point and > as the ending point.\">\nExample:\n{botexamplechat}"
28
+
29
+ def generate(
30
+ prompt, history, username, botname, botpersonality, botexamplechat, chatsituation, shouldoverridehistory, historyoverride, max_new_tokens=1024, temperature=1.2, top_p=0.95, repetition_penalty=1.0,
31
+ ):
32
+ print(history)
33
+ print(historyoverride)
34
+ temperature = float(temperature)
35
+ if temperature < 1e-2:
36
+ temperature = 1e-2
37
+ top_p = float(top_p)
38
+
39
+ generate_kwargs = dict(
40
+ temperature=temperature,
41
+ max_new_tokens=max_new_tokens,
42
+ top_p=top_p,
43
+ repetition_penalty=repetition_penalty,
44
+ do_sample=True,
45
+ seed=round(time.time()),
46
+ )
47
+
48
+ if shouldoverridehistory:
49
+ history = split_list(historyoverride[0], 2)
50
+
51
+ print(history)
52
+
53
+ formatted_prompt = format_prompt(prompt, history, format_sys_prompt(username, botname, botpersonality, botexamplechat, chatsituation))
54
+
55
+ inputs = tokenizer(formatted_prompt, return_tensors="pt")
56
+ outputs = model.generate(formatted_prompt, **generate_kwargs)
57
+
58
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
59
+
60
+
61
+ mychatbot = gr.Chatbot(
62
+ avatar_images=["./user.png", "./user.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
63
+
64
+ demo = gr.ChatInterface(fn=generate,
65
+ chatbot=mychatbot,
66
+ title="Joystick's Mixtral Chat-optimized interface",
67
+ retry_btn="🔁 Regenerate",
68
+ undo_btn="↩️ Undo",
69
+ additional_inputs=[
70
+ gr.Textbox(label="Name of user", lines=1, value="Jake"),
71
+ gr.Textbox(label="Name of bot", lines=1, value="Janet"),
72
+ gr.Textbox(label="Personality of bot", lines=3, value="Janet's a lovely person. A woman, blue eyed, glasses, smart and looks stunning."),
73
+ gr.Textbox(label="Example of bot chat", lines=3, value='<"Oh hey Jake!"> She said to Jake as he hurries to him. <"How are you?">'),
74
+ gr.Textbox(label="Current conversation situation", lines=2, value="It was a Friday afternoon, after-school hours, it was outside of school. Jake and Janet met each other at the entrance of the school."),
75
+ gr.Checkbox(label="Override history: History should be in the following format: user-bot-user-bot-user-...\nOverride history should be checked in order for it to be effective. Override primarily only used for APIs.", value=False),
76
+ gr.List(label="History", value=None, row_count=(1, "fixed"), headers=None),
77
+ gr.Slider(label="Max new tokens", maximum=2048, value=512)
78
+ ]
79
+ )
80
+
81
+ demo.queue().launch(show_api=True)