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 SD3 Long Captioner

Florence-2 SD3 Long Captioner

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.

Output Text

''' return render_template_string(html) @app.route('/generate', methods=['POST']) def generate(): if 'image' not in request.files: return jsonify({"error": "No image provided"}), 400 image_file = request.files['image'] image = Image.open(image_file.stream) task_prompt = "" prompt = task_prompt + "Describe this image in great detail." # Ensure the image is in RGB mode if image.mode != "RGB": image = image.convert("RGB") inputs = processor(text=prompt, images=image, return_tensors="pt") generated_ids = model.generate( input_ids=inputs["input_ids"], pixel_values=inputs["pixel_values"], max_new_tokens=1024, 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)) caption = modify_caption(parsed_answer[""]) return jsonify({"caption": caption}) if __name__ == '__main__': app.run(debug=True, port=7860, host="0.0.0.0")