File size: 1,861 Bytes
54a950c
3349358
 
 
 
 
54a950c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fb5a83
54a950c
 
9fb5a83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be9fba2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import subprocess

# 安裝缺少的依賴包
subprocess.run("pip install einops", shell=True)

import gradio as gr
import torch
from PIL import Image
from transformers import AutoProcessor, AutoModelForCausalLM

# Initialize Florence model
device = "cuda" if torch.cuda.is_available() else "cpu"
florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)

def generate_caption(image):
    if not isinstance(image, Image.Image):
        image = Image.fromarray(image)
    
    inputs = florence_processor(text="<MORE_DETAILED_CAPTION>", images=image, return_tensors="pt").to(device)
    generated_ids = florence_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 = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
    parsed_answer = florence_processor.post_process_generation(
        generated_text,
        task="<MORE_DETAILED_CAPTION>",
        image_size=(image.width, image.height)
    )
    prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
    return prompt

# Custom CSS for pink background and title styling
css = """
body {background-color: #FFC0CB;}
h1 {color: #800080; text-align: center; font-size: 32px; font-family: 'Comic Sans MS', cursive, sans-serif;}
"""

# Interface with pink background and a welcome message
io = gr.Interface(
    fn=generate_caption,
    inputs=[gr.Image(label="Input Image")],
    outputs=[gr.Textbox(label="Output Prompt", lines=2, show_copy_button=True)],
    title="歡迎來到我的魔法世界✨",
    css=css
)

io.launch(debug=True)