Spaces:
Runtime error
Runtime error
| import sys, types | |
| sys.modules['flash_attn'] = types.ModuleType('flash_attn') | |
| import transformers.dynamic_module_utils as dmu | |
| dmu.check_imports = lambda *args, **kwargs: [] | |
| import gradio as gr | |
| import torch | |
| import random | |
| from PIL import Image | |
| from transformers import AutoProcessor, AutoModelForCausalLM | |
| from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler | |
| REVISION = "ceaf371f01ef66192264811b390bccad475a4f02" | |
| # Florence-2 ๋ก๋ | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', revision = REVISION, trust_remote_code=True).to(device).eval() | |
| florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', revision = REVISION, trust_remote_code=True) | |
| # Stable Diffusion TurboX ๋ก๋ | |
| model_repo = "tensorart/stable-diffusion-3.5-large-TurboX" | |
| pipe = DiffusionPipeline.from_pretrained( | |
| model_repo, | |
| torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32 | |
| ) | |
| pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(model_repo, subfolder="scheduler", shift=5) | |
| pipe = pipe.to(device) | |
| MAX_SEED = 2**31 - 1 | |
| def pseudo_translate_to_korean_style(en_prompt: str) -> str: | |
| # ๋ฒ์ญ ์์ด ์คํ์ผ ์ ์ฉ | |
| return f"์ด ์ฅ๋ฉด์ {en_prompt} ์ฅ๋ฉด์ ๋๋ค. ๋ฐ๊ณ ๊ท์ฌ์ด ์นดํฐ ์คํ์ผ๋ก ๊ทธ๋ ค์ฃผ์ธ์. ๋์งํธ ์ผ๋ฌ์คํธ ๋๋์ผ๋ก ๋ฌ์ฌํด ์ฃผ์ธ์." | |
| def generate_prompt(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=512, | |
| 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_en = parsed_answer["<MORE_DETAILED_CAPTION>"] | |
| # ๋ฒ์ญ๊ธฐ ์์ด ์คํ์ผ ์ ์ฉ | |
| cartoon_prompt = pseudo_translate_to_korean_style(prompt_en) | |
| return cartoon_prompt | |
| def generate_image(prompt, seed=42, randomize_seed=False): | |
| """ํ ์คํธ ํ๋กฌํํธ โ ์ด๋ฏธ์ง ์์ฑ""" | |
| if randomize_seed: | |
| seed = random.randint(0, MAX_SEED) | |
| generator = torch.Generator().manual_seed(seed) | |
| image = pipe( | |
| prompt=prompt, | |
| negative_prompt="์๊ณก๋ ์, ํ๋ฆผ, ์ด์ํ ์ผ๊ตด", | |
| guidance_scale=1.5, | |
| num_inference_steps=8, | |
| width=768, | |
| height=768, | |
| generator=generator | |
| ).images[0] | |
| return image, seed | |
| # Gradio UI ๊ตฌ์ฑ | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# ๐ผ ์ด๋ฏธ์ง โ ์ค๋ช ์์ฑ โ ์นดํฐ ์ด๋ฏธ์ง ์๋ ์์ฑ๊ธฐ") | |
| gr.Markdown("**๐ ์ฌ์ฉ๋ฒ ์๋ด (ํ๊ตญ์ด)**\n" | |
| "- ์ผ์ชฝ์ ์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํ์ธ์.\n" | |
| "- AI๊ฐ ์์ด ์ค๋ช ์ ๋ง๋ค๊ณ , ๋ด๋ถ์์ ํ๊ตญ์ด ์คํ์ผ ํ๋กฌํํธ๋ก ์ฌ๊ตฌ์ฑํฉ๋๋ค.\n" | |
| "- ์ค๋ฅธ์ชฝ์ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_img = gr.Image(label="๐จ ์๋ณธ ์ด๋ฏธ์ง ์ ๋ก๋") | |
| run_button = gr.Button("โจ ์์ฑ ์์") | |
| with gr.Column(): | |
| prompt_out = gr.Textbox(label="๐ ์คํ์ผ ์ ์ฉ๋ ํ๋กฌํํธ", lines=3, show_copy_button=True) | |
| output_img = gr.Image(label="๐ ์์ฑ๋ ์ด๋ฏธ์ง") | |
| def full_process(img): | |
| prompt = generate_prompt(img) | |
| image, seed = generate_image(prompt, randomize_seed=True) | |
| return prompt, image | |
| run_button.click(fn=full_process, inputs=[input_img], outputs=[prompt_out, output_img]) | |
| demo.launch() | |