CreitinGameplays
commited on
Commit
•
add6648
1
Parent(s):
d70f45a
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,107 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import random
|
4 |
|
5 |
+
models = [
|
6 |
+
"CreitinGameplays/bloom-3b-conversational"
|
7 |
+
]
|
8 |
+
|
9 |
+
clients = []
|
10 |
+
for model in models:
|
11 |
+
clients.append(InferenceClient(model))
|
12 |
+
|
13 |
+
|
14 |
+
def format_prompt(message, history):
|
15 |
+
prompt = ""
|
16 |
+
if history:
|
17 |
+
for user_prompt, bot_response in history:
|
18 |
+
prompt += f"<|prompter|>{user_prompt}</s>"
|
19 |
+
prompt += f"<|assistant|>{bot_response}"
|
20 |
+
prompt += f"<|prompter|>{message}</s><|assistant|>"
|
21 |
+
return prompt
|
22 |
+
|
23 |
+
|
24 |
+
def chat_inf(system_prompt, prompt, history, client_choice, seed, temp, tokens, top_p, rep_p):
|
25 |
+
client = clients[int(client_choice) - 1]
|
26 |
+
if not history:
|
27 |
+
history = []
|
28 |
+
hist_len = 0
|
29 |
+
if history:
|
30 |
+
hist_len = len(history)
|
31 |
+
print(hist_len)
|
32 |
+
|
33 |
+
generate_kwargs = dict(
|
34 |
+
temperature=temp,
|
35 |
+
max_new_tokens=tokens,
|
36 |
+
top_p=top_p,
|
37 |
+
repetition_penalty=rep_p,
|
38 |
+
do_sample=True,
|
39 |
+
seed=seed,
|
40 |
+
)
|
41 |
+
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
42 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True,
|
43 |
+
return_full_text=False)
|
44 |
+
output = ""
|
45 |
+
|
46 |
+
for response in stream:
|
47 |
+
output += response.token.text
|
48 |
+
yield [(prompt, output)]
|
49 |
+
history.append((prompt, output))
|
50 |
+
yield history
|
51 |
+
|
52 |
+
|
53 |
+
def clear_fn():
|
54 |
+
return None
|
55 |
+
|
56 |
+
|
57 |
+
rand_val = random.randint(1, 1111111111111111)
|
58 |
+
|
59 |
+
|
60 |
+
def check_rand(inp, val):
|
61 |
+
if inp is True:
|
62 |
+
return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=random.randint(1, 1111111111111111))
|
63 |
+
else:
|
64 |
+
return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
|
65 |
+
|
66 |
+
|
67 |
+
with gr.Blocks() as app:
|
68 |
+
gr.HTML(
|
69 |
+
"""<center><h1 style='font-size:xx-large;'>Models</h1></center>""")
|
70 |
+
with gr.Group():
|
71 |
+
with gr.Row():
|
72 |
+
client_choice = gr.Dropdown(label="Models", type='index', choices=[c for c in models], value=models[0],
|
73 |
+
interactive=True)
|
74 |
+
chat_b = gr.Chatbot(height=500)
|
75 |
+
with gr.Group():
|
76 |
+
with gr.Row():
|
77 |
+
with gr.Column(scale=1):
|
78 |
+
with gr.Group():
|
79 |
+
rand = gr.Checkbox(label="Random Seed", value=True)
|
80 |
+
seed = gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, step=1, value=rand_val)
|
81 |
+
tokens = gr.Slider(label="Max new tokens", value=6400, minimum=0, maximum=8000, step=64,
|
82 |
+
interactive=True, visible=True, info="The maximum number of tokens")
|
83 |
+
with gr.Column(scale=1):
|
84 |
+
with gr.Group():
|
85 |
+
temp = gr.Slider(label="Temperature", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
|
86 |
+
top_p = gr.Slider(label="Top-P", step=0.01, minimum=0.01, maximum=1.0, value=0.9)
|
87 |
+
rep_p = gr.Slider(label="Repetition Penalty", step=0.1, minimum=0.1, maximum=2.0, value=1.0)
|
88 |
+
|
89 |
+
with gr.Group():
|
90 |
+
with gr.Row():
|
91 |
+
with gr.Column(scale=3):
|
92 |
+
sys_inp = gr.Textbox(label="System Prompt (optional)")
|
93 |
+
inp = gr.Textbox(label="Prompt")
|
94 |
+
with gr.Row():
|
95 |
+
btn = gr.Button("Chat")
|
96 |
+
stop_btn = gr.Button("Stop")
|
97 |
+
clear_btn = gr.Button("Clear")
|
98 |
+
|
99 |
+
chat_sub = inp.submit(check_rand, [rand, seed], seed).then(chat_inf,
|
100 |
+
[sys_inp, inp, chat_b, client_choice, seed, temp, tokens,
|
101 |
+
top_p, rep_p], chat_b)
|
102 |
+
go = btn.click(check_rand, [rand, seed], seed).then(chat_inf,
|
103 |
+
[sys_inp, inp, chat_b, client_choice, seed, temp, tokens, top_p,
|
104 |
+
rep_p], chat_b)
|
105 |
+
stop_btn.click(None, None, None, cancels=[go, chat_sub])
|
106 |
+
clear_btn.click(clear_fn, None, [chat_b])
|
107 |
+
app.queue(default_concurrency_limit=10).launch()
|