Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | 
         @@ -0,0 +1,99 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            from huggingface_hub import InferenceClient
         
     | 
| 2 | 
         
            +
            import gradio as gr
         
     | 
| 3 | 
         
            +
            import random
         
     | 
| 4 | 
         
            +
            from html2image import Html2Image
         
     | 
| 5 | 
         
            +
            hti = Html2Image()
         
     | 
| 6 | 
         
            +
            client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
         
     | 
| 7 | 
         
            +
             
     | 
| 8 | 
         
            +
             
     | 
| 9 | 
         
            +
            def get_screenshot(html,css=None):
         
     | 
| 10 | 
         
            +
             
     | 
| 11 | 
         
            +
                html = html
         
     | 
| 12 | 
         
            +
                #css = css
         
     | 
| 13 | 
         
            +
                #hti.screenshot(html_str=html, save_as='red_page.png')
         
     | 
| 14 | 
         
            +
                hti.screenshot(html_str=html, css_str=css, save_as='red_page.png')
         
     | 
| 15 | 
         
            +
                return 'red_page.png'
         
     | 
| 16 | 
         
            +
             
     | 
| 17 | 
         
            +
             
     | 
| 18 | 
         
            +
            def format_prompt(message, history):
         
     | 
| 19 | 
         
            +
                prompt = "<s>"
         
     | 
| 20 | 
         
            +
                if history:
         
     | 
| 21 | 
         
            +
                    for user_prompt, bot_response in history:
         
     | 
| 22 | 
         
            +
                        prompt += f"[INST] {user_prompt} [/INST]"
         
     | 
| 23 | 
         
            +
                        prompt += f" {bot_response}</s> "
         
     | 
| 24 | 
         
            +
                prompt += f"[INST] {message} [/INST]"
         
     | 
| 25 | 
         
            +
                return prompt
         
     | 
| 26 | 
         
            +
             
     | 
| 27 | 
         
            +
             
     | 
| 28 | 
         
            +
            def chat_inf(system_prompt,prompt,history):
         
     | 
| 29 | 
         
            +
                if not history:
         
     | 
| 30 | 
         
            +
                    history = []
         
     | 
| 31 | 
         
            +
                    hist_len=0
         
     | 
| 32 | 
         
            +
                if history:
         
     | 
| 33 | 
         
            +
                    hist_len=len(history)
         
     | 
| 34 | 
         
            +
                    print(hist_len)
         
     | 
| 35 | 
         
            +
                seed = random.randint(1,1111111111111111)
         
     | 
| 36 | 
         
            +
                generate_kwargs = dict(
         
     | 
| 37 | 
         
            +
                    temperature=0.9,
         
     | 
| 38 | 
         
            +
                    max_new_tokens=10480,
         
     | 
| 39 | 
         
            +
                    top_p=0.95,
         
     | 
| 40 | 
         
            +
                    repetition_penalty=1.0,
         
     | 
| 41 | 
         
            +
                    do_sample=True,
         
     | 
| 42 | 
         
            +
                    seed=seed,
         
     | 
| 43 | 
         
            +
                )
         
     | 
| 44 | 
         
            +
                    
         
     | 
| 45 | 
         
            +
                formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
         
     | 
| 46 | 
         
            +
                stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
         
     | 
| 47 | 
         
            +
                output = ""
         
     | 
| 48 | 
         
            +
                    
         
     | 
| 49 | 
         
            +
                for response in stream:
         
     | 
| 50 | 
         
            +
                    output += response.token.text
         
     | 
| 51 | 
         
            +
                    yield [(prompt,output)]
         
     | 
| 52 | 
         
            +
                history.append((prompt,output))
         
     | 
| 53 | 
         
            +
                yield history
         
     | 
| 54 | 
         
            +
                    
         
     | 
| 55 | 
         
            +
            chat=[('user','bot'),('user','bot')]
         
     | 
| 56 | 
         
            +
             
     | 
| 57 | 
         
            +
            #get_screenshot(chat=[('user','bot'),('user','bot')])
         
     | 
| 58 | 
         
            +
            with gr.Blocks() as app:
         
     | 
| 59 | 
         
            +
                with gr.Row():
         
     | 
| 60 | 
         
            +
                    with gr.Column(scale=3):
         
     | 
| 61 | 
         
            +
                        with gr.Group():
         
     | 
| 62 | 
         
            +
                            chat_b = gr.Chatbot()
         
     | 
| 63 | 
         
            +
                            with gr.Row():
         
     | 
| 64 | 
         
            +
                                with gr.Column(scale=3):
         
     | 
| 65 | 
         
            +
                                    inp = gr.Textbox(label="Prompt")
         
     | 
| 66 | 
         
            +
                                    sys_inp = gr.Textbox(label="System Prompt (optional)")
         
     | 
| 67 | 
         
            +
                                    btn = gr.Button("Chat")
         
     | 
| 68 | 
         
            +
                                    
         
     | 
| 69 | 
         
            +
                                with gr.Column(scale=1):
         
     | 
| 70 | 
         
            +
                                    with gr.Group():
         
     | 
| 71 | 
         
            +
                                        stop_btn=gr.Button("Stop")
         
     | 
| 72 | 
         
            +
                                        clear_btn=gr.Button("Clear")
         
     | 
| 73 | 
         
            +
                    with gr.Column(scale=1):
         
     | 
| 74 | 
         
            +
                        with gr.Group():
         
     | 
| 75 | 
         
            +
                            with gr.Row():
         
     | 
| 76 | 
         
            +
                                im_height=gr.Number(label="Height",value=5000)
         
     | 
| 77 | 
         
            +
                                im_width=gr.Number(label="Width",value=500)
         
     | 
| 78 | 
         
            +
                            wait_time=gr.Number(label="Wait Time",value=3000)
         
     | 
| 79 | 
         
            +
                            theme=gr.Radio(label="Theme", choices=["light","dark"],value="light")
         
     | 
| 80 | 
         
            +
                            chatblock=gr.Dropdown(label="Chatblocks",choices=[c for c in range(1,40)],multiselect=True)
         
     | 
| 81 | 
         
            +
                            
         
     | 
| 82 | 
         
            +
                            im_btn=gr.Button("Screenshot")
         
     | 
| 83 | 
         
            +
                            img=gr.Image(type='filepath')
         
     | 
| 84 | 
         
            +
                btn.click(chat_inf,[sys_inp,inp,chat_b],chat_b)
         
     | 
| 85 | 
         
            +
                im_btn.click(get_screenshot,[chat_b,im_height,im_width,chatblock,theme,wait_time],img)
         
     | 
| 86 | 
         
            +
                #app.load(get_screenshot,inp,img)
         
     | 
| 87 | 
         
            +
            app.launch()
         
     | 
| 88 | 
         
            +
             
     | 
| 89 | 
         
            +
             
     | 
| 90 | 
         
            +
             
     | 
| 91 | 
         
            +
             
     | 
| 92 | 
         
            +
             
     | 
| 93 | 
         
            +
             
     | 
| 94 | 
         
            +
             
     | 
| 95 | 
         
            +
             
     | 
| 96 | 
         
            +
             
     | 
| 97 | 
         
            +
             
     | 
| 98 | 
         
            +
             
     | 
| 99 | 
         
            +
             
     |