Update app.py
Browse files123456789101112131415161718192021222324252627282930313233223 unmodified lines257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299import randomimport osimport uuidfrom datetime import datetimeimport gradio as grimport numpy as npimport torchfrom diffusers import DiffusionPipelinefrom PIL import Imageimport warnings# ๊ฒฝ๊ณ ๋ฉ์์ง ์จ๊ธฐ๊ธฐwarnings.filterwarnings('ignore', category=UserWarning)# ์ ์ฅ ๋๋ ํ ๋ฆฌ ์์ฑSAVE_DIR = "saved_images"if not os.path.exists(SAVE_DIR):os.makedirs(SAVE_DIR, exist_ok=True)# ์ฅ์น ์ค์ device = "cuda" if torch.cuda.is_available() else "cpu"# ๋ชจ๋ธ ๋ก๋repo_id = "black-forest-labs/FLUX.1-schnell"pipeline = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float32)pipeline = pipeline.to(device)MAX_SEED = np.iinfo(np.int32).maxMAX_IMAGE_SIZE = 2048# Enhanced examples with more detailed prompts and specific stylingEXAMPLES = [{223 unmodified linesexcept Exception as e:raise gr.Error(f"๋ค์ด์ด๊ทธ๋จ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")# Gradio ์ธํฐํ์ด์ค ์์ฑdemo = gr.Interface(fn=generate_diagram,inputs=[gr.Textbox(label="๋ค์ด์ด๊ทธ๋จ ํ๋กฌํํธ",placeholder="๋ค์ด์ด๊ทธ๋จ ๊ตฌ์กฐ๋ฅผ ์ ๋ ฅํ์ธ์...",lines=10),gr.Slider(label="๋๋น",minimum=512,maximum=MAX_IMAGE_SIZE,step=128,value=1024),gr.Slider(label="๋์ด",minimum=512,maximum=MAX_IMAGE_SIZE,step=128,value=1024)],outputs=gr.Image(label="์์ฑ๋ ๋ค์ด์ด๊ทธ๋จ"),title="๐จ FLUX ๋ค์ด์ด๊ทธ๋จ ์์ฑ๊ธฐ",description="FLUX AI๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฆ๋ค์ด ์๊ทธ๋ฆผ ์คํ์ผ์ ๋ค์ด์ด๊ทธ๋จ์ ์์ฑํฉ๋๋ค",article="""### ๋ ๋์ ๊ฒฐ๊ณผ๋ฅผ ์ํ ํ- ๋ช ํํ ๊ณ์ธต ๊ตฌ์กฐ ์ฌ์ฉ- ๋๊ดํธ ์์ ์์ด์ฝ ์ค๋ช ํฌํจ- ๊ฐ๊ฒฐํ๊ณ ์๋ฏธ ์๋ ํ ์คํธ ์ฌ์ฉ- ์ผ๊ด๋ ํ์ ์ ์ง""",examples=GRADIO_EXAMPLES,cache_examples=True)# ์ฑ ์คํif __name__ == "__main__":123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475223 unmodified lines299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383import gradio as grimport numpy as npimport randomimport torchfrom diffusers import DiffusionPipelineimport warningsimport osfrom datetime import datetimeimport uuid# ๊ฒฝ๊ณ ๋ฉ์์ง ์จ๊ธฐ๊ธฐwarnings.filterwarnings('ignore', category=UserWarning)# ์ ์ฅ ๋๋ ํ ๋ฆฌ ์์ฑSAVE_DIR = "saved_images"if not os.path.exists(SAVE_DIR):os.makedirs(SAVE_DIR, exist_ok=True)# ์ฅ์น ๋ฐ dtype ์ค์ dtype = torch.float32 if torch.cuda.is_available() else torch.float32device = "cuda" if torch.cuda.is_available() else "cpu"# ๋ชจ๋ธ ๋ก๋pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell",torch_dtype=dtype,device_map="auto",use_safetensors=True).to(device)# ๋ฉ๋ชจ๋ฆฌ ์ต์ ํpipe.enable_attention_slicing()if device == "cpu":pipe.enable_sequential_cpu_offload()MAX_SEED = np.iinfo(np.int32).maxMAX_IMAGE_SIZE = 2048def generate_diagram(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4):"""FLUX AI๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ด์ด๊ทธ๋จ ์์ฑ"""try:if randomize_seed:seed = random.randint(0, MAX_SEED)generator = torch.Generator(device=device).manual_seed(seed)with torch.no_grad():image = pipe(prompt=prompt,width=width,height=height,num_inference_steps=num_inference_steps,generator=generator,guidance_scale=0.0).images[0]# ์ด๋ฏธ์ง ์ ์ฅtimestamp = datetime.now().strftime("%Y%m%d_%H%M%S")unique_id = str(uuid.uuid4())[:8]filename = f"diagram_{timestamp}_{unique_id}.png"save_path = os.path.join(SAVE_DIR, filename)image.save(save_path)return image, seedexcept Exception as e:raise gr.Error(f"๋ค์ด์ด๊ทธ๋จ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")finally:if torch.cuda.is_available():torch.cuda.empty_cache()# Enhanced examples with more detailed prompts and specific stylingEXAMPLES = [{223 unmodified linesexcept Exception as e:raise gr.Error(f"๋ค์ด์ด๊ทธ๋จ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")# CSS ์คํ์ผcss="""#col-container {margin: 0 auto;max-width: 520px;}"""# Gradio ์ธํฐํ์ด์ค ์์ฑwith gr.Blocks(css=css) as demo:with gr.Column(elem_id="col-container"):gr.Markdown("""# FLUX ๋ค์ด์ด๊ทธ๋จ ์์ฑ๊ธฐFLUX AI๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฆ๋ค์ด ์๊ทธ๋ฆผ ์คํ์ผ์ ๋ค์ด์ด๊ทธ๋จ์ ์์ฑํฉ๋๋ค""")with gr.Row():prompt = gr.Text(label="ํ๋กฌํํธ",show_label=False,max_lines=1,placeholder="๋ค์ด์ด๊ทธ๋จ ๊ตฌ์กฐ๋ฅผ ์ ๋ ฅํ์ธ์...",container=False,)run_button = gr.Button("์์ฑ", scale=0)result = gr.Image(label="์์ฑ๋ ๋ค์ด์ด๊ทธ๋จ", show_label=False)with gr.Accordion("๊ณ ๊ธ ์ค์ ", open=False):seed = gr.Slider(label="์๋",minimum=0,maximum=MAX_SEED,step=1,value=0,)randomize_seed = gr.Checkbox(label="๋๋ค ์๋", value=True)with gr.Row():width = gr.Slider(label="๋๋น",minimum=256,maximum=MAX_IMAGE_SIZE,step=32,value=1024,)height = gr.Slider(label="๋์ด",minimum=256,maximum=MAX_IMAGE_SIZE,step=32,value=1024,)num_inference_steps = gr.Slider(label="์ถ๋ก ๋จ๊ณ ์",minimum=1,maximum=50,step=1,value=4,)# ์์ ์ถ๊ฐgr.Examples(examples=EXAMPLES, # ์ด์ ์ ์ ์๋ ์์ ๋ค ์ฌ์ฉfn=generate_diagram,inputs=[prompt],outputs=[result, seed],cache_examples=True)# ์ด๋ฒคํธ ํธ๋ค๋ฌgr.on(triggers=[run_button.click, prompt.submit],fn=generate_diagram,inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],outputs=[result, seed])# ์ฑ ์คํif __name__ == "__main__":
app.py+129-45