import requests import gradio as gr from PIL import Image import io # 修改 generate_image 函数以处理图像生成并保存为临时文件 def generate_image(api_key, prompt, aspect_ratio, model, negative_prompt, seed): if not api_key or not api_key.strip(): raise gr.Error("Please enter API key") response = requests.post( "https://api.stability.ai/v2beta/stable-image/generate/sd3", headers={ "authorization": f"Bearer {api_key}", "accept": "image/*" }, files={"none": ''}, data={ "prompt": prompt, "aspect_ratio": aspect_ratio, "model": model, "negative_prompt": negative_prompt, "seed": seed, "output_format": "jpeg", }, ) if response.status_code == 200: # 将 bytes 数据转换为 PIL Image 对象 image_bytes = io.BytesIO(response.content) image = Image.open(image_bytes) # 保存为临时文件 temp_path = "./temp_image.jpeg" image.save(temp_path) return temp_path else: raise Exception(str(response.json())) # 构建 Gradio 界面 with gr.Blocks() as demo: with gr.Row(): with gr.Column(scale=1): api_key = gr.Textbox(label="API Key") prompt = gr.Textbox(label="Prompt", placeholder="What you wish to see in the output image.") aspect_ratio = gr.Dropdown(label="Aspect Ratio", choices=["16:9", "1:1", "21:9", "2:3", "3:2", "4:5", "5:4", "9:16", "9:21"], value="1:1") model = gr.Dropdown(label="Model", choices=["sd3", "sd3-turbo"], value="sd3") seed = gr.Number(label="Seed (Enter 0 for random seed)", value=0, minimum=0, maximum=4294967294, step=1) negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Describe what you do not wish to see.") generate_btn = gr.Button("Generate Image") image = gr.Image(label="Out image", type="pil", height=0.001) api_key_text = gr.HTML("https://platform.stability.ai/account/keys") with gr.Column(scale=1): output = gr.Image() examples = [ ["""A rabbit in a police uniform, holding a turnish-shaped gun, points at a wall. The word "Sheriff" was painted in large letters on the wall. 32k ultra HD photos, high detail, rendered in Pixar's signature style, vibrant colors, laugh filled atmosphere, Pixar style, visually stunning and emotionally appealing""", "1:1", "sd3", "(worst quality, low quality, normal quality, lowres, low details, oversaturated, undersaturated, overexposed, underexposed, grayscale, bw, bad photo, bad photography, bad art:1.4), (blur, blurry, grainy), morbid, ugly, asymmetrical, mutated malformed, mutilated, poorly lit, bad shadow, draft, cropped, out of frame, cut off, censored, jpeg artifacts, out of focus, glitch, duplicate,","ex_images/1.jpeg"], ["""There is a transparent and exquisite luxury bag on the grass, which contains various expensive and exquisite flowers. The flowers form a cute word "LOVE" """, "1:1", "sd3", "(worst quality, low quality, normal quality, lowres, low details, oversaturated, undersaturated, overexposed, underexposed, grayscale, bw, bad photo, bad photography, bad art:1.4), (blur, blurry, grainy), morbid, ugly, asymmetrical, mutated malformed, mutilated, poorly lit, bad shadow, draft, cropped, out of frame, cut off, censored, jpeg artifacts, out of focus, glitch, duplicate,","ex_images/2.jpeg"], ["""A striking and surreal vector design featuring a crying skull adorned with gold teeth, rainbows, clouds, glitter, and psychedelic colors. The skull's eyes are filled with tears, and its mouth is open in a howl, while the text "I'm fine" is written across its forehead. The overall design is dark and morbid with a touch of humor and a dash of unexpected color and glitter, creating a unique and unforgettable visual experience.""", "1:1", "sd3", "(worst quality, low quality, normal quality, lowres, low details, oversaturated, undersaturated, overexposed, underexposed, grayscale, bw, bad photo, bad photography, bad art:1.4), (blur, blurry, grainy), morbid, ugly, asymmetrical, mutated malformed, mutilated, poorly lit, bad shadow, draft, cropped, out of frame, cut off, censored, jpeg artifacts, out of focus, glitch, duplicate,","ex_images/3.jpeg"], ["""A captivating watercolor illustration presents a mesmerizing cat-like creature entirely composed of green cannabis leaves. The first-person perspective draws the viewer into its haunting world, immersing them in a supernatural realm filled with dark fantasy elements. The creature's glowing eyes emit an eerie light, inviting exploration into the unknown. The delicate coloring and side view of the subject showcase its delicate beauty and enigmatic allure. The artistic influences of dark fantasy, the painting style of Quentin Blake, and the creative genius of Damien Hirst harmoniously converge to create this extraordinary masterpiece., photo, illustration""", "1:1", "sd3", "(worst quality, low quality, normal quality, lowres, low details, oversaturated, undersaturated, overexposed, underexposed, grayscale, bw, bad photo, bad photography, bad art:1.4), (blur, blurry, grainy), morbid, ugly, asymmetrical, mutated malformed, mutilated, poorly lit, bad shadow, draft, cropped, out of frame, cut off, censored, jpeg artifacts, out of focus, glitch, duplicate,","ex_images/4.jpeg"], ["""A powerful visual metaphor featuring a sequence of four bananas, each transitioning seamlessly from fresh yellow to dark brown. The journey begins with a vibrant, slightly green banana on the left, moving through a yellowing banana with a hint of brown, a banana with a predominantly brownish-yellow hue, and finally to a dark brown, overripe banana on the right. The message beneath them, written in large, bold text, reads 'Make the most of the present moment; time waits for no one.' The dark grey background emphasizes the vibrant colors of the bananas and the stark contrast between them, creating a striking and thought-provoking visual statement., typography""", "1:1", "sd3", "(worst quality, low quality, normal quality, lowres, low details, oversaturated, undersaturated, overexposed, underexposed, grayscale, bw, bad photo, bad photography, bad art:1.4), (blur, blurry, grainy), morbid, ugly, asymmetrical, mutated malformed, mutilated, poorly lit, bad shadow, draft, cropped, out of frame, cut off, censored, jpeg artifacts, out of focus, glitch, duplicate,","ex_images/5.jpeg"], ["""A striking duality illustration featuring SpongeBob SquarePants in two contrasting expressions. On the left, a fierce SpongeBob sports a darker fur coat, red eyes, and a menacing grin, surrounded by a fiery orange and black background with sparks flying. On the right, a cheerful SpongeBob appears in a bright yellow fur coat, blue eyes, and a contagious smile, against a serene blue background that contrasts sharply with the left side. A lightning bolt connects these two opposing natures, emphasizing the duality of SpongeBob's character.""", "1:1", "sd3", "(worst quality, low quality, normal quality, lowres, low details, oversaturated, undersaturated, overexposed, underexposed, grayscale, bw, bad photo, bad photography, bad art:1.4), (blur, blurry, grainy), morbid, ugly, asymmetrical, mutated malformed, mutilated, poorly lit, bad shadow, draft, cropped, out of frame, cut off, censored, jpeg artifacts, out of focus, glitch, duplicate,","ex_images/6.jpeg"], ] # 添加示例到界面 gr.Examples(examples, inputs=[prompt, aspect_ratio, model, negative_prompt,image], outputs=output) generate_btn.click(fn=generate_image, inputs=[api_key, prompt, aspect_ratio, model, negative_prompt, seed], outputs=output) demo.launch()