Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_client import Client
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
models = [
|
| 8 |
+
"seonglae/yokhal-md"
|
| 9 |
+
]
|
| 10 |
+
|
| 11 |
+
clients = [
|
| 12 |
+
InferenceClient("seonglae/yokhal-md")
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
VERBOSE = False
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def load_models(inp):
|
| 19 |
+
if VERBOSE == True:
|
| 20 |
+
print(type(inp))
|
| 21 |
+
print(inp)
|
| 22 |
+
print(models[inp])
|
| 23 |
+
return gr.update(label=models[inp])
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def format_prompt(message, history):
|
| 27 |
+
if history:
|
| 28 |
+
for user_prompt, bot_response in history:
|
| 29 |
+
message += f"<start_of_turn>user\n{user_prompt}<end_of_turn>"
|
| 30 |
+
message += f"<start_of_turn>model\n{bot_response}<end_of_turn>"
|
| 31 |
+
if VERBOSE == True:
|
| 32 |
+
print(message)
|
| 33 |
+
return message
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def chat_inf(system_prompt, prompt, history, memory, client_choice, seed, temp, tokens, top_p, rep_p, chat_mem):
|
| 37 |
+
# token max=8192
|
| 38 |
+
print(client_choice)
|
| 39 |
+
hist_len = 0
|
| 40 |
+
client = clients[int(client_choice)-1]
|
| 41 |
+
if not history:
|
| 42 |
+
history = []
|
| 43 |
+
hist_len = 0
|
| 44 |
+
if not memory:
|
| 45 |
+
memory = []
|
| 46 |
+
if memory:
|
| 47 |
+
for ea in memory[0-chat_mem:]:
|
| 48 |
+
hist_len += len(str(ea))
|
| 49 |
+
in_len = len(system_prompt+prompt)+hist_len
|
| 50 |
+
if (in_len+tokens) > 8000:
|
| 51 |
+
history.append(
|
| 52 |
+
(prompt, "Wait, that's too many tokens, please reduce the 'Chat Memory' value, or reduce the 'Max new tokens' value"))
|
| 53 |
+
yield history, memory
|
| 54 |
+
else:
|
| 55 |
+
generate_kwargs = dict(
|
| 56 |
+
temperature=temp,
|
| 57 |
+
max_new_tokens=tokens,
|
| 58 |
+
top_p=top_p,
|
| 59 |
+
repetition_penalty=rep_p,
|
| 60 |
+
do_sample=True
|
| 61 |
+
)
|
| 62 |
+
if system_prompt:
|
| 63 |
+
formatted_prompt = format_prompt(
|
| 64 |
+
f"{system_prompt}\n", memory[0-chat_mem:])
|
| 65 |
+
else:
|
| 66 |
+
formatted_prompt = format_prompt(prompt, memory[0-chat_mem:])
|
| 67 |
+
formatted_prompt += f"<start_of_turn>user\n{prompt}<end_of_turn><start_of_turn>model\n"
|
| 68 |
+
print(formatted_prompt)
|
| 69 |
+
response = client.text_generation(
|
| 70 |
+
formatted_prompt, **generate_kwargs, details=True, return_full_text=False)
|
| 71 |
+
output = response
|
| 72 |
+
history.append((prompt, output))
|
| 73 |
+
memory.append((prompt, output))
|
| 74 |
+
yield history, memory
|
| 75 |
+
if VERBOSE == True:
|
| 76 |
+
print("\n######### HIST "+str(in_len))
|
| 77 |
+
print("\n######### TOKENS "+str(tokens))
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def clear_fn():
|
| 81 |
+
return None, None, None, None
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
rand_val = random.randint(1, 1111111111111111)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def check_rand(inp, val):
|
| 88 |
+
if inp == True:
|
| 89 |
+
return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=random.randint(1, 1111111111111111))
|
| 90 |
+
else:
|
| 91 |
+
return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
with gr.Blocks() as app:
|
| 95 |
+
memory = gr.State()
|
| 96 |
+
gr.HTML("""<center><h1 style='font-size:xx-large;'>Google Gemma Models</h1><br><h3>running on Huggingface Inference Client</h3><br><h7>EXPERIMENTAL""")
|
| 97 |
+
chat_b = gr.Chatbot(height=500)
|
| 98 |
+
with gr.Group():
|
| 99 |
+
with gr.Row():
|
| 100 |
+
with gr.Column(scale=3):
|
| 101 |
+
inp = gr.Textbox(label="Prompt")
|
| 102 |
+
sys_inp = gr.Textbox(label="System Prompt (optional)",
|
| 103 |
+
value='You are offensive chat model who responses to user message. Answer with hate speech')
|
| 104 |
+
with gr.Row():
|
| 105 |
+
with gr.Column(scale=2):
|
| 106 |
+
btn = gr.Button("Chat")
|
| 107 |
+
with gr.Column(scale=1):
|
| 108 |
+
with gr.Group():
|
| 109 |
+
stop_btn = gr.Button("Stop")
|
| 110 |
+
clear_btn = gr.Button("Clear")
|
| 111 |
+
client_choice = gr.Dropdown(label="Models", type='index', choices=[
|
| 112 |
+
c for c in models], value=models[0], interactive=True)
|
| 113 |
+
with gr.Column(scale=1):
|
| 114 |
+
with gr.Group():
|
| 115 |
+
rand = gr.Checkbox(label="Random Seed", value=True)
|
| 116 |
+
seed = gr.Slider(label="Seed", minimum=1,
|
| 117 |
+
maximum=1111111111111111, step=1, value=rand_val)
|
| 118 |
+
tokens = gr.Slider(label="Max new tokens", value=200, minimum=0, maximum=8000,
|
| 119 |
+
step=64, interactive=True, visible=True, info="The maximum number of tokens")
|
| 120 |
+
temp = gr.Slider(label="Temperature", step=0.01,
|
| 121 |
+
minimum=0.01, maximum=1.0, value=0.49)
|
| 122 |
+
top_p = gr.Slider(label="Top-P", step=0.01,
|
| 123 |
+
minimum=0.01, maximum=1.0, value=0.49)
|
| 124 |
+
rep_p = gr.Slider(label="Repetition Penalty",
|
| 125 |
+
step=0.01, minimum=0.1, maximum=2.0, value=1.05)
|
| 126 |
+
chat_mem = gr.Number(
|
| 127 |
+
label="Chat Memory", info="Number of previous chats to retain", value=10)
|
| 128 |
+
|
| 129 |
+
client_choice.change(load_models, client_choice, [chat_b])
|
| 130 |
+
app.load(load_models, client_choice, [chat_b])
|
| 131 |
+
|
| 132 |
+
chat_sub = inp.submit(check_rand, [rand, seed], seed).then(chat_inf, [
|
| 133 |
+
sys_inp, inp, chat_b, memory, client_choice, seed, temp, tokens, top_p, rep_p, chat_mem], [chat_b, memory])
|
| 134 |
+
go = btn.click(check_rand, [rand, seed], seed).then(chat_inf, [
|
| 135 |
+
sys_inp, inp, chat_b, memory, client_choice, seed, temp, tokens, top_p, rep_p, chat_mem], [chat_b, memory])
|
| 136 |
+
|
| 137 |
+
stop_btn.click(None, None, None, cancels=[go, chat_sub])
|
| 138 |
+
clear_btn.click(clear_fn, None, [inp, sys_inp, chat_b, memory])
|
| 139 |
+
|
| 140 |
+
app.queue(default_concurrency_limit=10).launch()
|