Spaces:
Running on Zero
Running on Zero
Update
Browse files11 unmodified lines12131415161516171 unmodified line19202122232426272829303132333435363738394041252627432829303132473334353637385455565739404111 unmodified lines535455755657581 unmodified line6061628384858663646517 unmodified lines8384851108687889 unmodified lines989910012610110210334 unmodified lines13813914014114214314414514614714817017117217317417517617717817918018118218318414915015137 unmodified lines1891901912282292302311921931941951961972371981992005 unmodified lines20620720824920921021143 unmodified lines25525625725829925911 unmodified lines
DESCRIPTION = "# [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 line
MODEL_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(): 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 ), } } processor = AutoProcessor.from_pretrained(MODEL_ID) model = Blip2ForConditionalGeneration.from_pretrained(MODEL_ID, device_map="auto", load_in_8bit=True)else: model_dict = {} processor = None model = None
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 lines
def answer_question( model_id: str, image: PIL.Image.Image, text: str, decoding_method: str,1 unmodified line 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, text=text, return_tensors="pt").to(device, torch.float16) generated_ids = model.generate( **inputs,17 unmodified lines
def chat( model_id: str, image: PIL.Image.Image, text: str, decoding_method: str,9 unmodified lines prompt = " ".join(history_qa)
output = answer_question( model_id, image, prompt, decoding_method,34 unmodified lines
with 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): 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 lines
gr.Examples( examples=examples, inputs=[ image, vqa_input, ], 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 lines queue=False, )
if __name__ == "__main__":demo.queue(max_size=10).launch() demo.queue(max_size=10).launch()12345678910h1 { text-align: center;}
#duplicate-button { margin: auto; color: #fff; background: #1565c0; border-radius: 100vh;}app.py+13-53
style.css+7