Update app.py
Browse files55 unmodified lines565758595960616226 unmodified lines899091929394959293943 unmodified lines989910010510610710110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419511119619719819930 unmodified lines23023123214814915023323423511 unmodified lines24724824916825025125225317117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720825425525625725825926026126226326426526621226726826927055 unmodified lines use_auth_token=HF_TOKEN)
# Hyper-SD LoRA ๋ก๋ (์ธ์ฆ ํฌํจ)# Hyper-SD LoRA ๋ก๋pipe.load_lora_weights( hf_hub_download( "ByteDance/Hyper-SD",26 unmodified lines image = Image.fromarray(image) image.save(filepath, "PNG", quality=100) if not os.path.exists(filepath): print(f"Warning: Failed to verify saved image at {filepath}") return None return filepath except Exception as e: print(f"Failed to save image: {str(e)}")3 unmodified lines print(f"Error in save_image: {str(e)}") return None
# Create Gradio interfacewith gr.Blocks(theme=gr.themes.Soft()) as demo: with gr.Row():# ์์ ํ๋กฌํํธ ์ ์examples = [ ["A 3D Star Wars Darth Vader helmet, highly detailed metallic finish"], ["A 3D Iron Man mask with glowing eyes and metallic red-gold finish"], ["A detailed 3D Pokemon Pikachu figure with glossy surface"], ["A 3D geometric abstract cube transforming into a sphere, metallic finish"], ["A 3D steampunk mechanical heart with brass and copper details"], ["A 3D crystal dragon with transparent iridescent scales"], ["A 3D futuristic hovering drone with neon light accents"], ["A 3D ancient Greek warrior helmet with ornate details"], ["A 3D robotic butterfly with mechanical wings and metallic finish"], ["A 3D floating magical crystal orb with internal energy swirls"]]
@spaces.GPUdef process_and_save_image(height=1024, width=1024, steps=8, scales=3.5, prompt="", seed=None): global pipe if seed is None: seed = torch.randint(0, 1000000, (1,)).item() # ํ๊ธ ๊ฐ์ง ๋ฐ ๋ฒ์ญ def contains_korean(text): return any(ord('๊ฐ') <= ord(c) <= ord('ํฃ') for c in text) # ํ๋กฌํํธ ์ ์ฒ๋ฆฌ if contains_korean(prompt): translated = translator(prompt)[0]['translation_text'] prompt = translated formatted_prompt = f"wbgmsst, 3D, {prompt} ,white background" with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"): try: generated_image = pipe( prompt=[formatted_prompt], generator=torch.Generator().manual_seed(int(seed)), num_inference_steps=int(steps), guidance_scale=float(scales), height=int(height), width=int(width), max_sequence_length=256 ).images[0] saved_path = save_image(generated_image) if saved_path is None: print("Warning: Failed to save generated image") return generated_image except Exception as e: print(f"Error in image generation: {str(e)}") return None
def get_random_seed(): return torch.randint(0, 1000000, (1,)).item()
# Gradio ์ธํฐํ์ด์คwith gr.Blocks( theme=gr.themes.Soft(), css=""" .container { background: linear-gradient(to bottom right, #1a1a1a, #4a4a4a); border-radius: 20px; padding: 20px; } .generate-btn { background: linear-gradient(45deg, #2196F3, #00BCD4); border: none; color: white; font-weight: bold; border-radius: 10px; } .output-image { border-radius: 15px; box-shadow: 0 8px 16px rgba(0,0,0,0.2); } .fixed-width { max-width: 1024px; margin: auto; } """) as demo: gr.HTML( """ <div style="text-align: center; max-width: 800px; margin: 0 auto; padding: 20px;"> <h1 style="font-size: 2.5rem; color: #2196F3;">3D Style Image Generator</h1> <p style="font-size: 1.2rem; color: #666;">Create amazing 3D-style images with AI</p> </div> """ ) with gr.Row(elem_classes="container"): with gr.Column(scale=3): prompt = gr.Textbox( label="Image Description", placeholder="Describe the image you want to create...", placeholder="Describe the 3D image you want to create...", lines=3 ) 30 unmodified lines value=3.5 ) def get_random_seed(): return torch.randint(0, 1000000, (1,)).item() seed = gr.Number( label="Seed (random by default, set for reproducibility)", value=get_random_seed(),11 unmodified lines output = gr.Image( label="Generated Image", elem_id="output-image", elem_classes=["output-image", "fixed-width"] elem_classes=["output-image", "fixed-width"], value="3d.webp" )
@spaces.GPU def process_and_save_image(height, width, steps, scales, prompt, seed): global pipe # ํ๊ธ ๊ฐ์ง ๋ฐ ๋ฒ์ญ def contains_korean(text): return any(ord('๊ฐ') <= ord(c) <= ord('ํฃ') for c in text) # ํ๋กฌํํธ ์ ์ฒ๋ฆฌ if contains_korean(prompt): # ํ๊ธ์ ์์ด๋ก ๋ฒ์ญ translated = translator(prompt)[0]['translation_text'] prompt = translated # ํ๋กฌํํธ ํ์ ๊ฐ์ formatted_prompt = f"wbgmsst, 3D, {prompt} ,white background" with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"): try: generated_image = pipe( prompt=[formatted_prompt], generator=torch.Generator().manual_seed(int(seed)), num_inference_steps=int(steps), guidance_scale=float(scales), height=int(height), width=int(width), max_sequence_length=256 ).images[0] saved_path = save_image(generated_image) if saved_path is None: print("Warning: Failed to save generated image") return generated_image except Exception as e: print(f"Error in image generation: {str(e)}") return None # Examples ์น์
gr.Examples( examples=examples, inputs=prompt, outputs=output, fn=process_and_save_image, cache_examples=True, examples_per_page=5 )
def update_seed(): return get_random_seed()
# Click event handlers inside gr.Blocks context # ์ด๋ฒคํธ ํธ๋ค๋ฌ generate_btn.click( process_and_save_image, inputs=[height, width, steps, scales, prompt, seed],app.py+107-52