import gradio as gr import torch import requests import base64 from io import BytesIO from PIL import Image import os # --------------------------------------------------------- # CONFIG # --------------------------------------------------------- ENDPOINT_URL = "https://rmtgd24up4b49w1l.us-east-1.aws.endpoints.huggingface.cloud" HF_API_TOKEN = os.getenv("HF_API_TOKEN") def generate_image(prompt, negative_prompt, width, height, steps, seed, lora_denoising_steps): """ Sends inference request to the hosted Hugging Face endpoint. """ headers = { "Authorization": f"Bearer {HF_API_TOKEN}", "Content-Type": "application/json" } payload = { "inputs": { "prompt": prompt, "negative_prompt": negative_prompt, "width": width, "height": height, "steps": steps, "seed": seed, "lora_denoising_steps": lora_denoising_steps } } response = requests.post(ENDPOINT_URL, headers=headers, json=payload) if response.status_code != 200: raise RuntimeError(f"Request failed: {response.status_code} - {response.text}") # Depending on your handler, result may be image bytes or base64 try: result = response.json() if isinstance(result, dict) and "image" in result: image_data = base64.b64decode(result["image"]) elif isinstance(result, str): image_data = base64.b64decode(result) else: raise ValueError("Unexpected response format") except Exception: image_data = response.content # fallback if raw image returned image = Image.open(BytesIO(image_data)) return image EXAMPLES = [ 'Indian anime girl', "A blackboard that says 'AI research FRACTAL'", "Elaborate arches and pillars, teal, gold, polished surface.", "Black Converse sneakers sit on a rocky beach by the water.", '''A heartfelt anniversary card featuring a romantic illustration of a couple standing under a canopy of blooming cherry blossoms, with "Happy Anniversary, Indika Akka & Asela Ayya!" in elegant cursive script at the top. The scene is enhanced with delicate floral borders in soft pinks and whites, creating a serene and romantic atmosphere. The inside of the card provides ample space for a personal message, with the text "Wishing you both endless love, joy, and beautiful moments together." in a complementary, elegant font at the bottom, maintaining a cohesive and deeply emotional tone.''', "The image captures a vibrant outdoor scene where a series of colorful umbrellas are hung beneath white netting ropes. The background is a clear blue sky. These umbrellas appear to be made of paper or thin plastic and are arranged in a cascading manner, with each rope supporting multiple umbrellas. The ropes extend diagonally from the upper right corner to the lower left corner of the image, creating a dynamic visual flow. The umbrellas come in a variety of colors including orange, green, red, yellow, and white, adorned with intricate patterns such as dragonflies and flowers. The entire scene is illuminated by a white light strip running diagonally from the upper left corner to the lower right corner, adding a festive and magical atmosphere. Overall, the composition is very eye-catching, with the umbrellas and ropes forming an unforgettable display that draws the viewer's gaze upward.", '''A visually striking PPT slide characterized by a minimalistic background with soft color tones. The bold title reads ""Climate Change Initiatives"". A paragraph explains ""Global efforts to combat climate change focus on renewable energy, carbon reduction policies, and sustainable development practices."". A chart titled ""Carbon Emissions Reduction Targets"" with labels such as ""USA"", ""EU"", and ""China"". Icons including a leaf, a globe, and a wind turbine enhance the visual appeal. A footer note states ""UN Climate Report, 2024""''', '''Illustrate a futuristic library glowing with holographic books floating between glass shelves. The poster introduces ""Future of Knowledge Symposium."" In the middle, text invites ""Explore AI innovations, big data, and the next era of education."" At the bottom, dates and location appear: ""October 3-5 at Global Tech Center. Register now.""''', '''Introduce a cozy, pastel-themed bi-fold flyer for a bakery, decorated with hand-drawn illustrations and soft textures. Feature ""Sweet Moments Bakery"" on the cover, highlight daily offerings with ""Freshly Baked Daily"", and add a charming note ""Since 1998"''' ] with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.HTML( """
🔗 Model on Hugging Face | 🐦 Follow Fractal on Twitter
""" ) with gr.Row(): with gr.Column(scale=1): prompt = gr.Textbox( label="Prompt", placeholder="Describe the image you want to create...", lines=3, show_label=True ) negative_prompt = gr.Textbox( label="Negative Prompt", placeholder="Describe what you don't want in the image...", value=" ", lines=2, show_label=True ) with gr.Row(): width = gr.Slider(512, 1024, 1024, step=32, label="Width") height = gr.Slider(512, 1024, 1024, step=32, label="Height") with gr.Row(): steps = gr.Slider(10, 50, 50, step=1, label="Steps") lora_denoising_steps = gr.Slider(0, 50, 10, step=1, label="LoRA Denoising Steps (when to disable LoRA)") with gr.Row(): seed = gr.Number(value=-1, label="Seed (-1 for random)", precision=0) generate_btn = gr.Button("Generate Image", elem_classes="generate-btn") with gr.Column(scale=1): output_image = gr.Image( label="Generated Image", type="pil", show_download_button=True, elem_classes="image-container" ) generate_btn.click( fn=generate_image, inputs=[prompt, negative_prompt, width, height, steps, seed, lora_denoising_steps], outputs=[output_image] ) gr.Markdown("