from flask import Flask, request, jsonify, render_template_string from transformers import AutoProcessor, AutoModelForCausalLM import subprocess import re from PIL import Image import io # Install the necessary packages subprocess.run('pip install flash-attn einops flask', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True) app = Flask(__name__) model = AutoModelForCausalLM.from_pretrained('gokaygokay/Florence-2-SD3-Captioner', trust_remote_code=True).eval() processor = AutoProcessor.from_pretrained('gokaygokay/Florence-2-SD3-Captioner', trust_remote_code=True) 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 @app.route('/') def index(): html = '''
Florence-2 Base fine-tuned on Long SD3 Prompt and Image pairs. Check the Hugging Face link for datasets that are used for fine-tuning.