Spaces:
Running on Zero
Running on Zero
Update
Browse files11 unmodified lines12131415161718191 unmodified line2122232425262728293031323334353637383940414243444546474849505152535455565758596011 unmodified lines727374757677781 unmodified line8081828384858687888917 unmodified lines1071081091101111121139 unmodified lines12312412512612712812934 unmodified lines16416516616716816917017117217317417517617717817918018118218318418518618737 unmodified lines2252262272282292302312322332342352362372382392405 unmodified lines24624724824925025125243 unmodified lines29629729829911 unmodified linesDESCRIPTION = "# [BLIP-2](https://github.com/salesforce/LAVIS/tree/main/projects/blip2)"if (SPACE_ID := os.getenv("SPACE_ID")) is not None:DESCRIPTION += f'\n<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. <a href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>'if not torch.cuda.is_available():DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"1 unmodified lineMODEL_ID_OPT_6_7B = "Salesforce/blip2-opt-6.7b"MODEL_ID_FLAN_T5_XXL = "Salesforce/blip2-flan-t5-xxl"if torch.cuda.is_available():model_dict = {# MODEL_ID_OPT_6_7B: {# 'processor':# AutoProcessor.from_pretrained(MODEL_ID_OPT_6_7B),# 'model':# Blip2ForConditionalGeneration.from_pretrained(MODEL_ID_OPT_6_7B,# device_map='auto',# load_in_8bit=True),# },MODEL_ID_FLAN_T5_XXL: {"processor": AutoProcessor.from_pretrained(MODEL_ID_FLAN_T5_XXL),"model": Blip2ForConditionalGeneration.from_pretrained(MODEL_ID_FLAN_T5_XXL, device_map="auto", load_in_8bit=True),}}else:model_dict = {}def generate_caption(model_id: str,image: PIL.Image.Image,decoding_method: str,temperature: float,length_penalty: float,repetition_penalty: float,) -> str:model_info = model_dict[model_id]processor = model_info["processor"]model = model_info["model"]inputs = processor(images=image, return_tensors="pt").to(device, torch.float16)generated_ids = model.generate(pixel_values=inputs.pixel_values,11 unmodified linesdef answer_question(model_id: str,image: PIL.Image.Image,text: str,decoding_method: str,1 unmodified linelength_penalty: float,repetition_penalty: float,) -> str:model_info = model_dict[model_id]processor = model_info["processor"]model = model_info["model"]inputs = processor(images=image, text=text, return_tensors="pt").to(device, torch.float16)generated_ids = model.generate(**inputs,17 unmodified linesdef chat(model_id: str,image: PIL.Image.Image,text: str,decoding_method: str,9 unmodified linesprompt = " ".join(history_qa)output = answer_question(model_id,image,prompt,decoding_method,34 unmodified lineswith gr.Blocks(css="style.css") as demo:gr.Markdown(DESCRIPTION)image = gr.Image(type="pil")with gr.Accordion(label="Advanced settings", open=False):with gr.Row():model_id_caption = gr.Dropdown(label="Model ID for image captioning",choices=[MODEL_ID_OPT_6_7B, MODEL_ID_FLAN_T5_XXL],value=MODEL_ID_FLAN_T5_XXL,interactive=False,visible=False,)model_id_chat = gr.Dropdown(label="Model ID for VQA",choices=[MODEL_ID_OPT_6_7B, MODEL_ID_FLAN_T5_XXL],value=MODEL_ID_FLAN_T5_XXL,interactive=False,visible=False,)sampling_method = gr.Radio(label="Text Decoding Method",choices=["Beam search", "Nucleus sampling"],37 unmodified linesgr.Examples(examples=examples,inputs=[image,vqa_input,],)caption_button.click(fn=generate_caption,inputs=[model_id_caption,image,sampling_method,temperature,5 unmodified lines)chat_inputs = [model_id_chat,image,vqa_input,sampling_method,43 unmodified linesqueue=False,)demo.queue(max_size=10).launch()11 unmodified lines1213141516171 unmodified line192021222324252627282930313233343536373839404111 unmodified lines5354555657581 unmodified line60616263646517 unmodified lines8384858687889 unmodified lines989910010110210334 unmodified lines13813914014114214314414514614714814915015137 unmodified lines1891901911921931941951961971981992005 unmodified lines20620720820921021143 unmodified lines25525625725825911 unmodified linesDESCRIPTION = "# [BLIP-2](https://github.com/salesforce/LAVIS/tree/main/projects/blip2)"if not torch.cuda.is_available():DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"1 unmodified lineMODEL_ID_OPT_6_7B = "Salesforce/blip2-opt-6.7b"MODEL_ID_FLAN_T5_XXL = "Salesforce/blip2-flan-t5-xxl"MODEL_ID = os.getenv("MODEL_ID", MODEL_ID_FLAN_T5_XXL)if torch.cuda.is_available():processor = AutoProcessor.from_pretrained(MODEL_ID)model = Blip2ForConditionalGeneration.from_pretrained(MODEL_ID, device_map="auto", load_in_8bit=True)else:processor = Nonemodel = Nonedef generate_caption(image: PIL.Image.Image,decoding_method: str,temperature: float,length_penalty: float,repetition_penalty: float,) -> str:inputs = processor(images=image, return_tensors="pt").to(device, torch.float16)generated_ids = model.generate(pixel_values=inputs.pixel_values,11 unmodified linesdef answer_question(image: PIL.Image.Image,text: str,decoding_method: str,1 unmodified linelength_penalty: float,repetition_penalty: float,) -> str:inputs = processor(images=image, text=text, return_tensors="pt").to(device, torch.float16)generated_ids = model.generate(**inputs,17 unmodified linesdef chat(image: PIL.Image.Image,text: str,decoding_method: str,9 unmodified linesprompt = " ".join(history_qa)output = answer_question(image,prompt,decoding_method,34 unmodified lineswith gr.Blocks(css="style.css") as demo:gr.Markdown(DESCRIPTION)gr.DuplicateButton(value="Duplicate Space for private use",elem_id="duplicate-button",visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",)image = gr.Image(type="pil")with gr.Accordion(label="Advanced settings", open=False):sampling_method = gr.Radio(label="Text Decoding Method",choices=["Beam search", "Nucleus sampling"],37 unmodified linesgr.Examples(examples=examples,inputs=[image, vqa_input],)caption_button.click(fn=generate_caption,inputs=[image,sampling_method,temperature,5 unmodified lines)chat_inputs = [image,vqa_input,sampling_method,43 unmodified linesqueue=False,)if __name__ == "__main__":demo.queue(max_size=10).launch()
123h1 {text-align: center;}12345678910h1 {text-align: center;}#duplicate-button {margin: auto;color: #fff;background: #1565c0;border-radius: 100vh;}
app.py+13-53
style.css+7