import gradio as gr from transformers import AutoProcessor, AutoModelForCausalLM import re from PIL import Image import subprocess subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True) model = AutoModelForCausalLM.from_pretrained('vumichien/Florence-2-FT-Caption', trust_remote_code=True).to("cpu").eval() processor = AutoProcessor.from_pretrained('vumichien/Florence-2-FT-Caption', trust_remote_code=True) TITLE = "# [Florence-2 Captioner](https://huggingface.co/vumichien/Florence-2-FT-Caption)" def modify_caption(caption: str) -> str: """ Removes specific prefixes from captions if present, otherwise returns the original caption. Args: caption (str): A string containing a caption. Returns: str: The caption with the prefix removed if it was present, or the original caption. """ # Define the prefixes to remove prefix_substrings = [ ('captured from ', ''), ('captured at ', '') ] # Create a regex pattern to match any of the prefixes pattern = '|'.join([re.escape(opening) for opening, _ in prefix_substrings]) replacers = {opening.lower(): replacer for opening, replacer in prefix_substrings} # Function to replace matched prefix with its corresponding replacement def replace_fn(match): return replacers[match.group(0).lower()] # Apply the regex to the caption modified_caption = re.sub(pattern, replace_fn, caption, count=1, flags=re.IGNORECASE) # If the caption was modified, return the modified version; otherwise, return the original return modified_caption if modified_caption != caption else caption #@spaces.GPU def run_example(image): image = Image.fromarray(image) task_prompt = "