test03 / app.py
vivaceailab's picture
Update app.py
070ac5d verified
# app.py
import gradio as gr
import torch
from PIL import Image
from transformers import AutoProcessor, AutoModelForCausalLM
# 1. ์žฅ์น˜ ์„ค์ •
device = "cuda" if torch.cuda.is_available() else "cpu"
# 2. Florence ๋ชจ๋ธ ๋ฐ ํ”„๋กœ์„ธ์„œ ๋กœ๋“œ
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
)
# 3. ์ด๋ฏธ์ง€ ์„ค๋ช… ์ƒ์„ฑ ํ•จ์ˆ˜
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>"]
print("Generation completed!:", prompt)
return prompt
# 4. Gradio ์ธํ„ฐํŽ˜์ด์Šค (์บ๋ฆฌ์ปค์ณ ๋ฒ„ํŠผ ํฌํ•จ)
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange") as demo:
gr.Markdown("## ๐Ÿ–ผ๏ธ ์ด๋ฏธ์ง€ ์„ค๋ช… ์ƒ์„ฑ๊ธฐ")
gr.Markdown(
"โš  ํ˜„์žฌ CPU ๋ชจ๋“œ๋กœ ์‹คํ–‰ ์ค‘์ด๋ฏ€๋กœ ์†๋„๊ฐ€ ๋А๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์–‘ํ•ด ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค."
)
with gr.Row():
with gr.Column():
image_input = gr.Image(label="Input Image", type="pil")
with gr.Column():
caption_output = gr.Textbox(
label="Output Prompt",
lines=6, # ์ด์ „๋ณด๋‹ค 2๋ฐฐ ๋†’์ด
show_copy_button=True
)
# ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ '์บ๋ฆฌ์ปค์ณ ๋งŒ๋“ค๊ธฐ' ๋ฒ„ํŠผ
gr.HTML("""
<div style="margin-top:10px; text-align:center;">
<a href="https://huggingface.co/spaces/VIDraft/stable-diffusion-3.5-large-turboX" target="_blank">
<button style="
padding:10px 20px;
background-color:#ff9900;
color:white;
border:none;
border-radius:10px;
font-size:16px;
box-shadow:2px 2px 8px rgba(0,0,0,0.3);
cursor:pointer;
">
๐ŸŽจ ์บ๋ฆฌ์ปค์ณ ๋งŒ๋“ค๊ธฐ
</button>
</a>
</div>
""")
image_input.upload(fn=generate_caption, inputs=image_input, outputs=caption_output)
if __name__ == "__main__":
demo.launch(debug=True)