added embeddings
Browse files2 unmodified lines3456789101112138 unmodified lines222324252627282930313233342 unmodified lines373839404142432 unmodified linesfrom transformers import AutoProcessor, LlavaForConditionalGenerationfrom transformers import BitsAndBytesConfigquantization_config = BitsAndBytesConfig(load_in_4bit=True,bnb_4bit_compute_dtype=torch.float16)model_id = "llava-hf/llava-1.5-7b-hf"processor = AutoProcessor.from_pretrained(model_id)8 unmodified linesprompt = f'USER: <image>\n{prompt}\nASSISTANT:'inputs = processor([prompt], images=[image], padding=True, return_tensors="pt").to(model.device)for k, v in inputs.items():print(k, v.shape)print(inputs)output = model.generate(**inputs, max_new_tokens=100)generated_text = processor.batch_decode(output, skip_special_tokens=True)for text in generated_text:return text.split("ASSISTANT:")[-1]demo = gr.Interface(2 unmodified linesgr.Image(label='Select an image to analyze', type='pil'),gr.Textbox(label='Enter Prompt')],outputs=gr.Textbox(label='Maurice says:'))if __name__ == "__main__":2 unmodified lines345678910111213141516178 unmodified lines262728293031323334353637382 unmodified lines414243444546472 unmodified linesfrom transformers import AutoProcessor, LlavaForConditionalGenerationfrom transformers import BitsAndBytesConfigfrom sentence_transformers import SentenceTransformer, utilquantization_config = BitsAndBytesConfig(load_in_4bit=True,bnb_4bit_compute_dtype=torch.float16)embedder = SentenceTransformer('all-mpnet-base-v2')model_id = "llava-hf/llava-1.5-7b-hf"processor = AutoProcessor.from_pretrained(model_id)8 unmodified linesprompt = f'USER: <image>\n{prompt}\nASSISTANT:'inputs = processor([prompt], images=[image], padding=True, return_tensors="pt").to(model.device)output = model.generate(**inputs, max_new_tokens=500)generated_text = processor.batch_decode(output, skip_special_tokens=True)text = generated_text.pop()text_output = text.split("ASSISTANT:")[-1]text_embeddings = embedder.encode(text_output)return text_output, dict(text_embeddings=text_embeddings)demo = gr.Interface(2 unmodified linesgr.Image(label='Select an image to analyze', type='pil'),gr.Textbox(label='Enter Prompt')],outputs=[gr.Textbox(label='Maurice says:'), gr.JSON(label='Embedded text')])if __name__ == "__main__":
app.py+11-7