Update app.py
Browse files8 unmodified lines91011121314151617186 unmodified lines252627282930312 unmodified lines3435363738397 unmodified lines47484950515220 unmodified lines737475767778798081828384858687888990919293949510 unmodified lines10610710810911011173 unmodified lines1851861871881891901915 unmodified lines1971981992002012028 unmodified lines2112122132142152168 unmodified linesfrom diffusers import DiffusionPipelinefrom PIL import Image# Create directories if they don't existSAVE_DIR = "generated_images"if not os.path.exists(SAVE_DIR):os.makedirs(SAVE_DIR)device = "cuda" if torch.cuda.is_available() else "cpu"repo_id = "black-forest-labs/FLUX.1-dev"6 unmodified linesMAX_SEED = np.iinfo(np.int32).maxMAX_IMAGE_SIZE = 1024def save_generated_image(image):# Generate unique filename with timestamptimestamp = datetime.now().strftime("%Y%m%d_%H%M%S")unique_id = str(uuid.uuid4())[:8]2 unmodified lines# Save the imageimage.save(filepath)return filepathdef load_generated_images():7 unmodified linesimage_files.sort(key=lambda x: os.path.getctime(x), reverse=True)return image_files@spaces.GPU(duration=120)def inference(prompt: str,20 unmodified linesjoint_attention_kwargs={"scale": lora_scale},).images[0]# Save the generated imagesave_generated_image(image)# Return the image, seed, and updated galleryreturn image, seed, load_generated_images()def load_predefined_images():predefined_images = ["assets/cm1.webp","assets/cm2.webp","assets/cm3.webp","assets/cm4.webp","assets/cm5.webp","assets/cm6.webp",]return predefined_imagesexamples = ["Claude Monet's 1916 painting, Water Lilies, which is currently on display at the Metropolitan Museum of Art. The painting depicts a tranquil pond with water lilies floating on the surface, surrounded by lush green foliage and a variety of colorful flowers. The colors of the flowers range from bright pinks and purples to deep blues and greens, creating a peaceful and calming atmosphere. [trigger]","Claude Monet's 1869 masterpiece, The Magpie, showcasing a snow-covered rural landscape at dawn. A single black magpie perches on a wooden gate, contrasting against the pristine white snow. The scene captures the subtle interplay of light and shadow on the snow's surface, with delicate blue-gray tones in the shadows and warm golden hints where sunlight touches the snow-laden branches. [trigger]",10 unmodified lines"""with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:gr.HTML('<div class="title"> Claude Monet STUDIO </div>')gr.HTML('<div class="title">😄Image to Video Explore: <a href="https://huggingface.co/spaces/ginigen/theater" target="_blank">https://huggingface.co/spaces/ginigen/theater</a></div>')73 unmodified linescolumns=6,show_label=False,value=load_generated_images(),)# Add sample gallery section at the bottomgr.Markdown("### Claude Monet Style Examples")5 unmodified linesvalue=load_predefined_images())gr.on(triggers=[run_button.click, prompt.submit],fn=inference,8 unmodified lineslora_scale,],outputs=[result, seed, generated_gallery],)demo.queue()8 unmodified lines91011121314151617186 unmodified lines252627282930312 unmodified lines3435363738394041424344457 unmodified lines5354555657585960616263646566676869707172737475767778798020 unmodified lines10110210310410510610710810911011111210 unmodified lines12312412512612712812913073 unmodified lines2042052062072082092102115 unmodified lines2172182192202212222238 unmodified lines2322332342352362372382392402412422438 unmodified linesfrom diffusers import DiffusionPipelinefrom PIL import Image# Create permanent storage directorySAVE_DIR = "/content/generated_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-dev"6 unmodified linesMAX_SEED = np.iinfo(np.int32).maxMAX_IMAGE_SIZE = 1024def save_generated_image(image, prompt):# Generate unique filename with timestamptimestamp = datetime.now().strftime("%Y%m%d_%H%M%S")unique_id = str(uuid.uuid4())[:8]2 unmodified lines# Save the imageimage.save(filepath)# Save metadatametadata_file = os.path.join(SAVE_DIR, "metadata.txt")with open(metadata_file, "a", encoding="utf-8") as f:f.write(f"{filename}|{prompt}|{timestamp}\n")return filepathdef load_generated_images():7 unmodified linesimage_files.sort(key=lambda x: os.path.getctime(x), reverse=True)return image_filesclass ImageFlagging(gr.FlaggingCallback):def setup(self, components, flagging_dir: str):self.components = componentsself.flagging_dir = SAVE_DIRdef flag(self, flag_data, flag_option=None, flag_index=None, username=None) -> int:"""Save image and metadata permanently"""image, prompt = flag_datafilepath = save_generated_image(image, prompt)return 0def load_predefined_images():predefined_images = ["assets/cm1.webp","assets/cm2.webp","assets/cm3.webp","assets/cm4.webp","assets/cm5.webp","assets/cm6.webp",]return predefined_images@spaces.GPU(duration=120)def inference(prompt: str,20 unmodified linesjoint_attention_kwargs={"scale": lora_scale},).images[0]# Save the generated image with the promptsave_generated_image(image, prompt)# Return the image, seed, and updated galleryreturn image, seed, load_generated_images()examples = ["Claude Monet's 1916 painting, Water Lilies, which is currently on display at the Metropolitan Museum of Art. The painting depicts a tranquil pond with water lilies floating on the surface, surrounded by lush green foliage and a variety of colorful flowers. The colors of the flowers range from bright pinks and purples to deep blues and greens, creating a peaceful and calming atmosphere. [trigger]","Claude Monet's 1869 masterpiece, The Magpie, showcasing a snow-covered rural landscape at dawn. A single black magpie perches on a wooden gate, contrasting against the pristine white snow. The scene captures the subtle interplay of light and shadow on the snow's surface, with delicate blue-gray tones in the shadows and warm golden hints where sunlight touches the snow-laden branches. [trigger]",10 unmodified lines"""with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:flagging_callback = ImageFlagging()gr.HTML('<div class="title"> Claude Monet STUDIO </div>')gr.HTML('<div class="title">😄Image to Video Explore: <a href="https://huggingface.co/spaces/ginigen/theater" target="_blank">https://huggingface.co/spaces/ginigen/theater</a></div>')73 unmodified linescolumns=6,show_label=False,value=load_generated_images(),elem_id="generated_gallery").style(grid=6)# Add sample gallery section at the bottomgr.Markdown("### Claude Monet Style Examples")5 unmodified linesvalue=load_predefined_images())# Enable flagging for permanent storagegr.on(triggers=[run_button.click, prompt.submit],fn=inference,8 unmodified lineslora_scale,],outputs=[result, seed, generated_gallery],).then(fn=None,inputs=[result, prompt],outputs=None,_js="(res, prompt) => clearInterval(window.GalleryIntervalID)",flagging_callback=flagging_callback)demo.queue()
app.py+45-18