Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
+
import gradio as gr
|
3 |
+
import random
|
4 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
5 |
+
|
6 |
+
from prompts import GAME_MASTER, COMPRESS_HISTORY
|
7 |
+
def format_prompt(message, history):
|
8 |
+
prompt = "<s>"
|
9 |
+
for user_prompt, bot_response in history:
|
10 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
11 |
+
prompt += f" {bot_response}</s> "
|
12 |
+
prompt += f"[INST] {message} [/INST]"
|
13 |
+
return prompt
|
14 |
+
|
15 |
+
def compress_history(purpose, task, history):
|
16 |
+
resp = run_gpt(
|
17 |
+
COMPRESS_HISTORY,
|
18 |
+
stop_tokens=["observation:", "task:", "action:", "thought:"],
|
19 |
+
max_tokens=512,
|
20 |
+
seed=random.randint(1,1000000000),
|
21 |
+
purpose=purpose,
|
22 |
+
task=task,
|
23 |
+
history=history,
|
24 |
+
)
|
25 |
+
history = resp
|
26 |
+
return history
|
27 |
+
MAX_HISTORY=100
|
28 |
+
|
29 |
+
def generate(
|
30 |
+
prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
|
31 |
+
):
|
32 |
+
temperature = float(temperature)
|
33 |
+
if temperature < 1e-2:
|
34 |
+
temperature = 1e-2
|
35 |
+
top_p = float(top_p)
|
36 |
+
|
37 |
+
generate_kwargs = dict(
|
38 |
+
temperature=temperature,
|
39 |
+
max_new_tokens=max_new_tokens,
|
40 |
+
top_p=top_p,
|
41 |
+
repetition_penalty=repetition_penalty,
|
42 |
+
do_sample=True,
|
43 |
+
seed=random.randint(1,99999999999)
|
44 |
+
#seed=42,
|
45 |
+
)
|
46 |
+
cnt=0
|
47 |
+
for ea in history:
|
48 |
+
print (ea)
|
49 |
+
for l in ea:
|
50 |
+
print (l)
|
51 |
+
cnt+=len(l.split("\n"))
|
52 |
+
print(f'cnt:: {cnt}')
|
53 |
+
if cnt > MAX_HISTORY:
|
54 |
+
history = compress_history(history, prompt, history)
|
55 |
+
formatted_prompt = format_prompt(f"{GAME_MASTER}, {prompt}", history)
|
56 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
57 |
+
output = ""
|
58 |
+
|
59 |
+
for response in stream:
|
60 |
+
output += response.token.text
|
61 |
+
yield output
|
62 |
+
return output
|
63 |
+
|
64 |
+
|
65 |
+
additional_inputs=[
|
66 |
+
gr.Textbox(
|
67 |
+
label="System Prompt",
|
68 |
+
max_lines=1,
|
69 |
+
interactive=True,
|
70 |
+
),
|
71 |
+
gr.Slider(
|
72 |
+
label="Temperature",
|
73 |
+
value=0.9,
|
74 |
+
minimum=0.0,
|
75 |
+
maximum=1.0,
|
76 |
+
step=0.05,
|
77 |
+
interactive=True,
|
78 |
+
info="Higher values produce more diverse outputs",
|
79 |
+
),
|
80 |
+
gr.Slider(
|
81 |
+
label="Max new tokens",
|
82 |
+
value=1048,
|
83 |
+
minimum=0,
|
84 |
+
maximum=1048*10,
|
85 |
+
step=64,
|
86 |
+
interactive=True,
|
87 |
+
info="The maximum numbers of new tokens",
|
88 |
+
),
|
89 |
+
gr.Slider(
|
90 |
+
label="Top-p (nucleus sampling)",
|
91 |
+
value=0.90,
|
92 |
+
minimum=0.0,
|
93 |
+
maximum=1,
|
94 |
+
step=0.05,
|
95 |
+
interactive=True,
|
96 |
+
info="Higher values sample more low-probability tokens",
|
97 |
+
),
|
98 |
+
gr.Slider(
|
99 |
+
label="Repetition penalty",
|
100 |
+
value=1.2,
|
101 |
+
minimum=1.0,
|
102 |
+
maximum=2.0,
|
103 |
+
step=0.05,
|
104 |
+
interactive=True,
|
105 |
+
info="Penalize repeated tokens",
|
106 |
+
)
|
107 |
+
]
|
108 |
+
|
109 |
+
examples=[["Start the Game", None, None, None, None, None, ],
|
110 |
+
["Start a Game based in the year 1322", None, None, None, None, None,],
|
111 |
+
]
|
112 |
+
|
113 |
+
gr.ChatInterface(
|
114 |
+
fn=generate,
|
115 |
+
chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
|
116 |
+
additional_inputs=additional_inputs,
|
117 |
+
title="Mixtral 46.7B",
|
118 |
+
examples=examples,
|
119 |
+
concurrency_limit=20,
|
120 |
+
).launch(share=True,show_api=True)
|