import os from transformers import AutoProcessor,AutoModelForCausalLM import copy from PIL import Image,ImageDraw,ImageFont import io,spaces,matplotlib.pyplot as plt,matplotlib.patches as patches,random,numpy as np from unittest.mock import patch from transformers import AutoModelForCausalLM,AutoProcessor from transformers.dynamic_module_utils import get_imports def fixed_get_imports(filename:str|os.PathLike)->list[str]: if not str(filename).endswith('/modeling_florence2.py'):return get_imports(filename) imports=get_imports(filename) if'flash_attn'in imports:imports.remove('flash_attn') return imports @spaces.GPU def get_device_type(): import torch if torch.cuda.is_available():return'cuda' elif torch.backends.mps.is_available()and torch.backends.mps.is_built():return'mps' else:return'cpu' model_id = 'MiaoshouAI/Florence-2-base-PromptGen-v2.0' import subprocess device = get_device_type() if (device == "cuda"): subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True) model = AutoModelForCausalLM.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v2.0", trust_remote_code=True) processor = AutoProcessor.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v2.0", trust_remote_code=True) model.to(device) else: with patch("transformers.dynamic_module_utils.get_imports", fixed_get_imports): model = AutoModelForCausalLM.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v2.0", trust_remote_code=True) processor = AutoProcessor.from_pretrained("MiaoshouAI/Florence-2-base-PromptGen-v2.0", trust_remote_code=True) model.to(device) colormap=['blue','orange','green','purple','brown','pink','gray','olive','cyan','red','lime','indigo','violet','aqua','magenta','coral','gold','tan','skyblue'] def fig_to_pil(fig):buf=io.BytesIO();fig.savefig(buf,format='png');buf.seek(0);return Image.open(buf) @spaces.GPU def run_example(task_prompt,image,text_input=None): if text_input is None:prompt=task_prompt else:prompt=task_prompt+text_input inputs=processor(text=prompt,images=image,return_tensors='pt').to(device);generated_ids=model.generate(input_ids=inputs['input_ids'],pixel_values=inputs['pixel_values'],max_new_tokens=1024,early_stopping=False,do_sample=False,num_beams=3);generated_text=processor.batch_decode(generated_ids,skip_special_tokens=False)[0];parsed_answer=processor.post_process_generation(generated_text,task=task_prompt,image_size=(image.width,image.height));return parsed_answer def plot_bbox(image,data): fig,ax=plt.subplots();ax.imshow(image) for(bbox,label)in zip(data['bboxes'],data['labels']):x1,y1,x2,y2=bbox;rect=patches.Rectangle((x1,y1),x2-x1,y2-y1,linewidth=1,edgecolor='r',facecolor='none');ax.add_patch(rect);plt.text(x1,y1,label,color='white',fontsize=8,bbox=dict(facecolor='red',alpha=.5)) ax.axis('off');return fig def draw_polygons(image,prediction,fill_mask=False): draw=ImageDraw.Draw(image);scale=1 for(polygons,label)in zip(prediction['polygons'],prediction['labels']): color=random.choice(colormap);fill_color=random.choice(colormap)if fill_mask else None for _polygon in polygons: _polygon=np.array(_polygon).reshape(-1,2) if len(_polygon)<3:print('Invalid polygon:',_polygon);continue _polygon=(_polygon*scale).reshape(-1).tolist() if fill_mask:draw.polygon(_polygon,outline=color,fill=fill_color) else:draw.polygon(_polygon,outline=color) draw.text((_polygon[0]+8,_polygon[1]+2),label,fill=color) return image def draw_ocr_bboxes(image,prediction): scale=1;draw=ImageDraw.Draw(image);bboxes,labels=prediction['quad_boxes'],prediction['labels'] for(box,label)in zip(bboxes,labels):color=random.choice(colormap);new_box=(np.array(box)*scale).tolist();draw.polygon(new_box,width=3,outline=color);draw.text((new_box[0]+8,new_box[1]+2),'{}'.format(label),align='right',fill=color) return image def convert_to_od_format(data):bboxes=data.get('bboxes',[]);labels=data.get('bboxes_labels',[]);od_results={'bboxes':bboxes,'labels':labels};return od_results def process_image(image,task_prompt,text_input=None): if isinstance(image,str):image=Image.open(image) else:image=Image.fromarray(image) if task_prompt=='Caption':task_prompt='