Spaces:
Build error
Build error
| # 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) | |