Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_client import Client
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
import random
|
| 5 |
+
#ss_client = Client("https://omnibus-html-image-current-tab.hf.space/")
|
| 6 |
+
|
| 7 |
+
models=[
|
| 8 |
+
"google/gemma-7b",
|
| 9 |
+
"google/gemma-7b-it",
|
| 10 |
+
"google/gemma-2b",
|
| 11 |
+
"google/gemma-2b-it"
|
| 12 |
+
"meta-llama/Llama-2-7b-chat-hf",
|
| 13 |
+
"codellama/CodeLlama-70b-Instruct-hf",
|
| 14 |
+
"openchat/openchat-3.5-0106",
|
| 15 |
+
"NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
|
| 16 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 17 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.2"
|
| 18 |
+
]
|
| 19 |
+
'''clients=[
|
| 20 |
+
InferenceClient(models[0]),
|
| 21 |
+
InferenceClient(models[1]),
|
| 22 |
+
InferenceClient(models[2]),
|
| 23 |
+
InferenceClient(models[3]),
|
| 24 |
+
]'''
|
| 25 |
+
|
| 26 |
+
def format_prompt(message, history):
|
| 27 |
+
prompt = ""
|
| 28 |
+
if history:
|
| 29 |
+
#<start_of_turn>userHow does the brain work?<end_of_turn><start_of_turn>model
|
| 30 |
+
for user_prompt, bot_response in history:
|
| 31 |
+
prompt += f"{user_prompt}\n"
|
| 32 |
+
print(prompt)
|
| 33 |
+
prompt += f"{bot_response}\n"
|
| 34 |
+
print(prompt)
|
| 35 |
+
prompt += f"<start_of_turn>user{message}<end_of_turn><start_of_turn>model"
|
| 36 |
+
print(prompt)
|
| 37 |
+
return prompt
|
| 38 |
+
|
| 39 |
+
def chat_inf(system_prompt,prompt,history,client_choice,seed,temp,tokens,top_p,rep_p):
|
| 40 |
+
#token max=8192
|
| 41 |
+
client=clients[int(client_choice)-1]
|
| 42 |
+
if not history:
|
| 43 |
+
history = []
|
| 44 |
+
hist_len=0
|
| 45 |
+
if history:
|
| 46 |
+
hist_len=len(history)
|
| 47 |
+
print(hist_len)
|
| 48 |
+
in_len=len(system_prompt+prompt)+hist_len
|
| 49 |
+
print("\n#########"+in_len)
|
| 50 |
+
if (in_len+tokens) > 8000:
|
| 51 |
+
yield [(prompt,"Wait. I need to compress our Chat history...")]
|
| 52 |
+
history=compress_history(history,client_choice,seed,temp,tokens,top_p,rep_p)
|
| 53 |
+
yield [(prompt,"History has been compressed, processing request...")]
|
| 54 |
+
|
| 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 |
+
seed=seed,
|
| 62 |
+
)
|
| 63 |
+
#formatted_prompt=prompt
|
| 64 |
+
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 70 |
+
output = ""
|
| 71 |
+
|
| 72 |
+
for response in stream:
|
| 73 |
+
output += response.token.text
|
| 74 |
+
yield [(prompt,output)]
|
| 75 |
+
history.append((prompt,output))
|
| 76 |
+
yield history
|
| 77 |
+
|
| 78 |
+
def clear_fn():
|
| 79 |
+
return None,None,None
|
| 80 |
+
rand_val=random.randint(1,1111111111111111)
|
| 81 |
+
def check_rand(inp,val):
|
| 82 |
+
if inp==True:
|
| 83 |
+
return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=random.randint(1,1111111111111111))
|
| 84 |
+
else:
|
| 85 |
+
return gr.Slider(label="Seed", minimum=1, maximum=1111111111111111, value=int(val))
|
| 86 |
+
|
| 87 |
+
with gr.Blocks() as app:
|
| 88 |
+
gr.HTML("""<center><h1 style='font-size:xx-large;'>Google Gemma Models</h1><br><h3>running on Huggingface Inference Client</h3><br><h7>EXPERIMENTAL""")
|
| 89 |
+
with gr.Row():
|
| 90 |
+
chat_a = gr.Chatbot(height=500)
|
| 91 |
+
chat_b = gr.Chatbot(height=500)
|
| 92 |
+
with gr.Row():
|
| 93 |
+
chat_c = gr.Chatbot(height=500)
|
| 94 |
+
chat_d = gr.Chatbot(height=500)
|
| 95 |
+
with gr.Group():
|
| 96 |
+
with gr.Row():
|
| 97 |
+
with gr.Column(scale=3):
|
| 98 |
+
inp = gr.Textbox(label="Prompt")
|
| 99 |
+
sys_inp = gr.Textbox(label="System Prompt (optional)")
|
| 100 |
+
with gr.Row():
|
| 101 |
+
with gr.Column(scale=2):
|
| 102 |
+
btn = gr.Button("Chat")
|
| 103 |
+
with gr.Column(scale=1):
|
| 104 |
+
with gr.Group():
|
| 105 |
+
stop_btn=gr.Button("Stop")
|
| 106 |
+
clear_btn=gr.Button("Clear")
|
| 107 |
+
client_choice=gr.Dropdown(label="Models",type='index',choices=[c for c in models],value=models[0],interactive=True)
|
| 108 |
+
|
| 109 |
+
with gr.Column(scale=1):
|
| 110 |
+
with gr.Group():
|
| 111 |
+
rand = gr.Checkbox(label="Random Seed", value=True)
|
| 112 |
+
seed=gr.Slider(label="Seed", minimum=1, maximum=1111111111111111,step=1, value=rand_val)
|
| 113 |
+
tokens = gr.Slider(label="Max new tokens",value=3840,minimum=0,maximum=8000,step=64,interactive=True, visible=True,info="The maximum number of tokens")
|
| 114 |
+
temp=gr.Slider(label="Temperature",step=0.01, minimum=0.01, maximum=1.0, value=0.9)
|
| 115 |
+
top_p=gr.Slider(label="Top-P",step=0.01, minimum=0.01, maximum=1.0, value=0.9)
|
| 116 |
+
rep_p=gr.Slider(label="Repetition Penalty",step=0.1, minimum=0.1, maximum=2.0, value=1.0)
|
| 117 |
+
with gr.Accordion(label="Screenshot",open=False):
|
| 118 |
+
with gr.Row():
|
| 119 |
+
with gr.Column(scale=3):
|
| 120 |
+
im_btn=gr.Button("Screenshot")
|
| 121 |
+
img=gr.Image(type='filepath')
|
| 122 |
+
with gr.Column(scale=1):
|
| 123 |
+
with gr.Row():
|
| 124 |
+
im_height=gr.Number(label="Height",value=5000)
|
| 125 |
+
im_width=gr.Number(label="Width",value=500)
|
| 126 |
+
wait_time=gr.Number(label="Wait Time",value=3000)
|
| 127 |
+
theme=gr.Radio(label="Theme", choices=["light","dark"],value="light")
|
| 128 |
+
chatblock=gr.Dropdown(label="Chatblocks",info="Choose specific blocks of chat",choices=[c for c in range(1,40)],multiselect=True)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
#im_go=im_btn.click(get_screenshot,[chat_b,im_height,im_width,chatblock,theme,wait_time],img)
|
| 133 |
+
#chat_sub=inp.submit(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,client_choice,seed,temp,tokens,top_p,rep_p],chat_b)
|
| 134 |
+
#go=btn.click(check_rand,[rand,seed],seed).then(chat_inf,[sys_inp,inp,chat_b,client_choice,seed,temp,tokens,top_p,rep_p],chat_b)
|
| 135 |
+
#stop_btn.click(None,None,None,cancels=[go,im_go,chat_sub])
|
| 136 |
+
#clear_btn.click(clear_fn,None,[inp,sys_inp,chat_b])
|
| 137 |
+
app.queue(default_concurrency_limit=10).launch()
|