Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,2 +1,91 @@
|
|
| 1 |
# app.py
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 7 |
+
|
| 8 |
+
# 1. ์ฅ์น ์ค์
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
|
| 11 |
+
# 2. Florence ๋ชจ๋ธ ๋ฐ ํ๋ก์ธ์ ๋ก๋
|
| 12 |
+
florence_model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
"microsoft/Florence-2-base",
|
| 14 |
+
trust_remote_code=True
|
| 15 |
+
).to(device).eval()
|
| 16 |
+
florence_processor = AutoProcessor.from_pretrained(
|
| 17 |
+
"microsoft/Florence-2-base",
|
| 18 |
+
trust_remote_code=True
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# 3. ์ด๋ฏธ์ง ์ค๋ช
์์ฑ ํจ์
|
| 22 |
+
def generate_caption(image):
|
| 23 |
+
if not isinstance(image, Image.Image):
|
| 24 |
+
image = Image.fromarray(image)
|
| 25 |
+
|
| 26 |
+
inputs = florence_processor(
|
| 27 |
+
text="<MORE_DETAILED_CAPTION>",
|
| 28 |
+
images=image,
|
| 29 |
+
return_tensors="pt"
|
| 30 |
+
).to(device)
|
| 31 |
+
generated_ids = florence_model.generate(
|
| 32 |
+
input_ids=inputs["input_ids"],
|
| 33 |
+
pixel_values=inputs["pixel_values"],
|
| 34 |
+
max_new_tokens=1024,
|
| 35 |
+
early_stopping=False,
|
| 36 |
+
do_sample=False,
|
| 37 |
+
num_beams=3,
|
| 38 |
+
)
|
| 39 |
+
generated_text = florence_processor.batch_decode(
|
| 40 |
+
generated_ids,
|
| 41 |
+
skip_special_tokens=False
|
| 42 |
+
)[0]
|
| 43 |
+
parsed_answer = florence_processor.post_process_generation(
|
| 44 |
+
generated_text,
|
| 45 |
+
task="<MORE_DETAILED_CAPTION>",
|
| 46 |
+
image_size=(image.width, image.height)
|
| 47 |
+
)
|
| 48 |
+
prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
|
| 49 |
+
print("Generation completed!:", prompt)
|
| 50 |
+
return prompt
|
| 51 |
+
|
| 52 |
+
# 4. Gradio ์ธํฐํ์ด์ค (์บ๋ฆฌ์ปค์ณ ๋ฒํผ ํฌํจ)
|
| 53 |
+
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange") as demo:
|
| 54 |
+
gr.Markdown("## ๐ผ๏ธ ์ด๋ฏธ์ง ์ค๋ช
์์ฑ๊ธฐ")
|
| 55 |
+
gr.Markdown(
|
| 56 |
+
"โ ํ์ฌ CPU ๋ชจ๋๋ก ์คํ ์ค์ด๋ฏ๋ก ์๋๊ฐ ๋๋ฆด ์ ์์ต๋๋ค. ์ํด ๋ถํ๋๋ฆฝ๋๋ค."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column():
|
| 61 |
+
image_input = gr.Image(label="Input Image", type="pil")
|
| 62 |
+
with gr.Column():
|
| 63 |
+
caption_output = gr.Textbox(
|
| 64 |
+
label="Output Prompt",
|
| 65 |
+
lines=6, # ์ด์ ๋ณด๋ค 2๋ฐฐ ๋์ด
|
| 66 |
+
show_copy_button=True
|
| 67 |
+
)
|
| 68 |
+
# ์ค๋ฅธ์ชฝ ํ๋จ '์บ๋ฆฌ์ปค์ณ ๋ง๋ค๊ธฐ' ๋ฒํผ
|
| 69 |
+
gr.HTML("""
|
| 70 |
+
<div style="margin-top:10px; text-align:center;">
|
| 71 |
+
<a href="https://huggingface.co/spaces/VIDraft/stable-diffusion-3.5-large-turboX" target="_blank">
|
| 72 |
+
<button style="
|
| 73 |
+
padding:10px 20px;
|
| 74 |
+
background-color:#ff9900;
|
| 75 |
+
color:white;
|
| 76 |
+
border:none;
|
| 77 |
+
border-radius:10px;
|
| 78 |
+
font-size:16px;
|
| 79 |
+
box-shadow:2px 2px 8px rgba(0,0,0,0.3);
|
| 80 |
+
cursor:pointer;
|
| 81 |
+
">
|
| 82 |
+
๐จ ์บ๋ฆฌ์ปค์ณ ๋ง๋ค๊ธฐ
|
| 83 |
+
</button>
|
| 84 |
+
</a>
|
| 85 |
+
</div>
|
| 86 |
+
""")
|
| 87 |
+
|
| 88 |
+
image_input.upload(fn=generate_caption, inputs=image_input, outputs=caption_output)
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
demo.launch(debug=True)
|