dawood/Chat_With_Blip2-test
0
1import requests2from PIL import Image3import gradio as gr4from transformers import AutoProcessor, Blip2ForConditionalGeneration5import torch6 7 8css = """9#column_container {10 position: relative;11 height: 800px;12 max-width: 700px;13 display: flex;14 flex-direction: column;15 background-color: lightgray;16 border: 1px solid gray;17 border-radius: 5px;18 padding: 10px;19 box-shadow: 2px 2px 5px gray;20 margin-left: auto; 21 margin-right: auto;22}23#input_prompt {24 position: fixed;25 bottom: 0;26 max-width: 680px;27}28#chatbot-component {29 overflow: auto;30}31"""32 33processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")34model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16) 35 36device = "cuda" if torch.cuda.is_available() else "cpu"37model.to(device)38 39def upload_button_config():40 return gr.update(visible=False)41 42def update_textbox_config(text_in):43 return gr.update(visible=True)44 45#takes input and generates the Response46def predict(btn_upload, counter,image_hid, input, history):47 48 if counter == 0:49 image_in = Image.open(btn_upload)50 #Resizing the image51 basewidth = 51252 wpercent = (basewidth/float(image_in.size[0]))53 hsize = int((float(image_in.size[1])*float(wpercent)))54 image_in = image_in.resize((basewidth,hsize)) #, Image.Resampling.LANCZOS)55 # Save the image to the file-like object56 #seed = random.randint(0, 1000000)57 img_name = "uploaded_image.png" #f"./edited_image_{seed}.png"58 image_in.save(img_name)59 #add state60 history = history or []61 response = '<img src="/file=' + img_name + '">'62 history.append((input, response))63 counter += 164 return history, history, img_name, counter, image_in65 66 #process the prompt67 print(f"prompt is :{input}") 68 #Getting prompt in the format - Question: Is this photo unusual? Answer:69 prompt = f"Question: {input} Answer: "70 inputs = processor(image_hid, text=prompt, return_tensors="pt").to(device, torch.float16)71 72 #generate the response73 generated_ids = model.generate(**inputs, max_new_tokens=10)74 generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()75 print(f"generated_text is : {generated_text}")76 77 #add state78 history = history or []79 response = generated_text 80 history.append((input, response))81 counter += 182 return history, history, "uploaded_image.png", counter, image_hid83 84#Blocks Layout - leaving this here for moment - "#chatbot-component .overflow-y-auto{height:800px}"85with gr.Blocks(css="#chatbot-component {height: 900px}") as demo: 86 with gr.Row():87 with gr.Column(scale=1):88 #with gr.Accordion("See details"):89 gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">90 <div91 style="92 display: inline-flex;93 align-items: center;94 gap: 0.8rem;95 font-size: 1.75rem;96 "97 >98 <h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">99 Bringing Visual Conversations to Life with BLIP2100 </h1>101 </div>102 <p style="margin-bottom: 10px; font-size: 94%">103 Blip2 is functioning as an <b>instructed zero-shot image-to-text generation</b> model using OPT-2.7B in this Space. 104 It shows a wide range of capabilities including visual conversation, visual knowledge reasoning, visual commensense reasoning, storytelling, 105 personalized image-to-text generation etc.<br>106 BLIP-2 by <a href="https://huggingface.co/Salesforce" target="_blank">Salesforce</a> is now available in🤗Transformers! 107 This model was contributed by <a href="https://twitter.com/NielsRogge" target="_blank">nielsr</a>. 108 The BLIP-2 model was proposed in <a href="https://arxiv.org/abs/2301.12597" target="_blank">BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models</a> 109 by Junnan Li, Dongxu Li, Silvio Savarese, Steven Hoi.<br><br>110 </p></div>""")111 gr.HTML("""<a href="https://huggingface.co/spaces/ysharma/InstructPix2Pix_Chatbot?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate Space with GPU Upgrade for fast Inference & no queue<br>""")112 113 with gr.Column(elem_id = "column_container", scale=2):114 #text_in = gr.Textbox(value='', placeholder="Type your questions here and press enter", elem_id = "input_prompt", visible=False, label='Great! Now you can ask questions to get more information about the image')115 btn_upload = gr.UploadButton("Upload image!", file_types=["image"], file_count="single", elem_id="upload_button")116 chatbot = gr.Chatbot(elem_id = 'chatbot-component', label='Converse with Images')117 text_in = gr.Textbox(value='', placeholder="Type your questions here and press enter", elem_id = "input_prompt", visible=False, label='Great! Now you can ask questions to get more information about the image')118 state_in = gr.State()119 counter_out = gr.Number(visible=False, value=0, precision=0)120 text_out = gr.Textbox(visible=False) #getting image name out121 image_hid = gr.Image(visible=False) #, type='pil')122 123 #Using Event Listeners124 btn_upload.upload(predict, [btn_upload, counter_out, image_hid, text_in, state_in], [chatbot, state_in, text_out, counter_out, image_hid])125 btn_upload.upload(fn = update_textbox_config, inputs=text_in, outputs = text_in)126 127 text_in.submit(predict, [btn_upload, counter_out, image_hid, text_in, state_in], [chatbot, state_in, text_out, counter_out, image_hid])128 129 chatbot.change(fn = upload_button_config, outputs=btn_upload) #, scroll_to_output = True)130 131demo.queue(concurrency_count=10)132demo.launch(debug=True) #, width="80%", height=2000)