Spaces:
Sleeping
Sleeping
0.42 Pharia added
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ import os
|
|
8 |
import traceback
|
9 |
|
10 |
from threading import Thread
|
11 |
-
from random import shuffle
|
12 |
|
13 |
logging.basicConfig(level=logging.DEBUG)
|
14 |
|
@@ -17,7 +17,6 @@ SPACER = '\n' + '*' * 40 + '\n'
|
|
17 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
18 |
login(token=HF_TOKEN)
|
19 |
|
20 |
-
|
21 |
system_prompts = {
|
22 |
"English": "You are a helpful chatbot that answers user input in a concise and witty way.",
|
23 |
"German": "Du bist ein hilfreicher Chatbot, der Usereingaben knapp und originell beantwortet.",
|
@@ -25,12 +24,19 @@ system_prompts = {
|
|
25 |
"Spanish": "Eres un chatbot servicial que responde a las entradas de los usuarios de forma concisa y original."
|
26 |
}
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
29 |
"name": "Meta Llama 3.1 8B Instruct"},
|
30 |
{"id": "mistralai/Mistral-7B-Instruct-v0.3",
|
31 |
"name": "Mistral 7B Instruct v0.3"}]
|
|
|
|
|
32 |
shuffle(model_info)
|
33 |
-
logging.debug('
|
34 |
|
35 |
device = "cuda"
|
36 |
|
@@ -50,15 +56,17 @@ try:
|
|
50 |
device_map="auto",
|
51 |
trust_remote_code=True,
|
52 |
)
|
53 |
-
|
54 |
except Exception as e:
|
55 |
logging.error(f'{SPACER} Error: {e}, Traceback {traceback.format_exc()}')
|
56 |
|
57 |
|
58 |
-
def
|
59 |
-
"""
|
|
|
60 |
add_generation_prompt:bool extends tmplate for generation.
|
61 |
"""
|
|
|
62 |
pharia_template = """<|begin_of_text|>"""
|
63 |
role_map = {
|
64 |
"system": "<|start_header_id|>system<|end_header_id|>\n",
|
@@ -99,21 +107,29 @@ def generate_both(system_prompt, input_text, chatbot_a, chatbot_b, max_new_token
|
|
99 |
new_messages_a = system_prompt_list + chat_history_a + input_text_list
|
100 |
new_messages_b = system_prompt_list + chat_history_b + input_text_list
|
101 |
|
102 |
-
|
103 |
-
new_messages_a,
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
generation_kwargs_a = dict(
|
119 |
input_ids=input_ids_a,
|
@@ -200,7 +216,7 @@ def reveal_bot(selection, chatbot_a, chatbot_b):
|
|
200 |
with gr.Blocks() as demo:
|
201 |
try:
|
202 |
with gr.Column():
|
203 |
-
gr.HTML(
|
204 |
with gr.Row(variant="compact"):
|
205 |
with gr.Column(scale=0):
|
206 |
language_dropdown = gr.Dropdown(
|
|
|
8 |
import traceback
|
9 |
|
10 |
from threading import Thread
|
11 |
+
from random import shuffle, choice
|
12 |
|
13 |
logging.basicConfig(level=logging.DEBUG)
|
14 |
|
|
|
17 |
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
18 |
login(token=HF_TOKEN)
|
19 |
|
|
|
20 |
system_prompts = {
|
21 |
"English": "You are a helpful chatbot that answers user input in a concise and witty way.",
|
22 |
"German": "Du bist ein hilfreicher Chatbot, der Usereingaben knapp und originell beantwortet.",
|
|
|
24 |
"Spanish": "Eres un chatbot servicial que responde a las entradas de los usuarios de forma concisa y original."
|
25 |
}
|
26 |
|
27 |
+
htmL_info = "<center><h1>Pharia Battle Royale</h1><p>Let the games begin: Try a prompt in a language you like. Set the parameters and vote for the best answers. After casting your vote, the bots reveal their identity.</p></center>"
|
28 |
+
|
29 |
+
model_info = [{"id": "Aleph-Alpha/Pharia-1-LLM-7B-control-hf",
|
30 |
+
"name": "Pharia 1 LLM 7B control hf"}]
|
31 |
+
|
32 |
+
challenger_models = [{"id": "NousResearch/Meta-Llama-3.1-8B-Instruct",
|
33 |
"name": "Meta Llama 3.1 8B Instruct"},
|
34 |
{"id": "mistralai/Mistral-7B-Instruct-v0.3",
|
35 |
"name": "Mistral 7B Instruct v0.3"}]
|
36 |
+
|
37 |
+
model_info.append(choice(challenger_models))
|
38 |
shuffle(model_info)
|
39 |
+
logging.debug(f'models shuffled. model[0]: {model_info[0]['name']}, model[1]: {model_info[1]['name']}')
|
40 |
|
41 |
device = "cuda"
|
42 |
|
|
|
56 |
device_map="auto",
|
57 |
trust_remote_code=True,
|
58 |
)
|
59 |
+
|
60 |
except Exception as e:
|
61 |
logging.error(f'{SPACER} Error: {e}, Traceback {traceback.format_exc()}')
|
62 |
|
63 |
|
64 |
+
def apply_pharia_template(messages, add_generation_prompt=False):
|
65 |
+
"""Chat template not defined in Pharia model configs.
|
66 |
+
Adds chat template for Pharia. Expects a list of messages.
|
67 |
add_generation_prompt:bool extends tmplate for generation.
|
68 |
"""
|
69 |
+
|
70 |
pharia_template = """<|begin_of_text|>"""
|
71 |
role_map = {
|
72 |
"system": "<|start_header_id|>system<|end_header_id|>\n",
|
|
|
107 |
new_messages_a = system_prompt_list + chat_history_a + input_text_list
|
108 |
new_messages_b = system_prompt_list + chat_history_b + input_text_list
|
109 |
|
110 |
+
if "Pharia" in model_info[0]['id']:
|
111 |
+
formatted_conversation = apply_pharia_template(messages=new_messages_a, add_generation_prompt=True)
|
112 |
+
input_ids_a = tokenizer_a(formatted_conversation, return_tensors="pt").to(device)
|
113 |
+
|
114 |
+
else:
|
115 |
+
input_ids_a = tokenizer_a.apply_chat_template(
|
116 |
+
new_messages_a,
|
117 |
+
add_generation_prompt=True,
|
118 |
+
dtype=torch.float16,
|
119 |
+
return_tensors="pt"
|
120 |
+
).to(device)
|
121 |
+
|
122 |
+
if "Pharia" in model_info[1]['id']:
|
123 |
+
formatted_conversation = apply_pharia_template(messages=new_messages_a, add_generation_prompt=True)
|
124 |
+
input_ids_a = tokenizer_a(formatted_conversation, return_tensors="pt").to(device)
|
125 |
+
|
126 |
+
else:
|
127 |
+
input_ids_b = tokenizer_b.apply_chat_template(
|
128 |
+
new_messages_b,
|
129 |
+
add_generation_prompt=True,
|
130 |
+
dtype=torch.float16,
|
131 |
+
return_tensors="pt"
|
132 |
+
).to(device)
|
133 |
|
134 |
generation_kwargs_a = dict(
|
135 |
input_ids=input_ids_a,
|
|
|
216 |
with gr.Blocks() as demo:
|
217 |
try:
|
218 |
with gr.Column():
|
219 |
+
gr.HTML(htmL_info)
|
220 |
with gr.Row(variant="compact"):
|
221 |
with gr.Column(scale=0):
|
222 |
language_dropdown = gr.Dropdown(
|