CoolFace
Apppublic

Omnibus/Chatbot-Screenshot-dev

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py105 linesDownload Raw Back to root
1from gradio_client import Client2from huggingface_hub import InferenceClient3import gradio as gr4import random5 6ss_client = Client("https://omnibus-html-image-current-tab.hf.space/")7client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")8 9 10def format_prompt(message, history):11    prompt = "<s>"12    if history:13        for user_prompt, bot_response in history:14            prompt += f"[INST] {user_prompt} [/INST]"15            prompt += f" {bot_response}</s> "16    prompt += f"[INST] {message} [/INST]"17    return prompt18 19 20def chat_inf(system_prompt,prompt,history):21    if not history:22        history = []23        hist_len=024    if history:25        hist_len=len(history)26        print(hist_len)27    seed = random.randint(1,1111111111111111)28    generate_kwargs = dict(29        temperature=0.9,30        max_new_tokens=10480,31        top_p=0.95,32        repetition_penalty=1.0,33        do_sample=True,34        seed=seed,35    )36        37    formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)38    stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)39    output = ""40        41    for response in stream:42        output += response.token.text43        yield [(prompt,output)]44    history.append((prompt,output))45    yield history46        47def get_screenshot(chat: list,height=5000,width=600,chatblock=[],theme="light",wait=3000,header=True):48    print(chatblock)49    tog = 050    if chatblock:51        tog = 352    53    result = ss_client.predict(str(chat),height,width,chatblock,header,theme,wait,api_name="/run_script")54		# str  in 'Chat: [('user','bot'),('user','bot')]' Textbox component55		# float  in 'Height' Number component56		# float  in 'Width' Number component57		# List[Literal['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20']]  in 'Chatblocks' Checkboxgroup component58		# bool  in 'Show Header' Checkbox component59		# Literal['light', 'dark']  in 'Theme' Radio component60		# float (numeric value between 1 and 10000) in 'Wait time' Slider component61		# api_name="/run_script"62 63        ## return types:64        # filepath representing output in 'value_25' Image component,65        # str representing output in 'value_20' Html component,66        # List[Dict(image: filepath, caption: str | None)] representing output in 'value_24' Gallery component,67        # filepath representing output in 'value_23' Image component,68    #out = f'https://omnibus-html-image-current-tab.hf.space/file={result[0]}'69    out = f'https://omnibus-html-image-current-tab.hf.space/file={result[tog]}'70    print(out)71    return out72 73chat=[('user','bot'),('user','bot')]74 75#get_screenshot(chat=[('user','bot'),('user','bot')])76with gr.Blocks() as app:77    with gr.Row():78        with gr.Column(scale=3):79            with gr.Group():80                chat_b = gr.Chatbot()81                with gr.Row():82                    with gr.Column(scale=3):83                        inp = gr.Textbox(label="Prompt")84                        sys_inp = gr.Textbox(label="System Prompt (optional)")85                        btn = gr.Button("Chat")86                        87                    with gr.Column(scale=1):88                        with gr.Group():89                            stop_btn=gr.Button("Stop")90                            clear_btn=gr.Button("Clear")91        with gr.Column(scale=1):92            with gr.Group():93                with gr.Row():94                    im_height=gr.Number(label="Height",value=5000)95                    im_width=gr.Number(label="Width",value=500)96                wait_time=gr.Number(label="Wait Time",value=3000)97                theme=gr.Radio(label="Theme", choices=["light","dark"],value="light")98                chatblock=gr.Dropdown(label="Chatblocks",choices=[c for c in range(1,40)],multiselect=True)99                100                im_btn=gr.Button("Screenshot")101                img=gr.Image(type='filepath')102    btn.click(chat_inf,[sys_inp,inp,chat_b],chat_b)103    im_btn.click(get_screenshot,[chat_b,im_height,im_width,chatblock,theme,wait_time],img)104    #app.load(get_screenshot,inp,img)105app.launch()