Spaces:
Running
Running
File size: 2,311 Bytes
1d17a15 8370647 12de40d 8370647 12de40d 1d17a15 12de40d 1d17a15 8370647 1d17a15 8370647 1d17a15 c2abdb7 7257002 8370647 d6428a4 1d17a15 8370647 d6428a4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
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):
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")
negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Describe what you do not wish to see.")
seed = gr.Number(label="Seed", value=0, minimum=0, maximum=4294967294, step=1)
generate_btn = gr.Button("Generate Image")
# api_key_info = gr.Text("API Key Application", style="bold") # Text display
# api_key_link = gr.Link("https://platform.stability.ai/account/keys", "Apply Here") # Hyperlink
with gr.Column(scale=1):
output = gr.Image()
generate_btn.click(fn=generate_image,
inputs=[api_key, prompt, aspect_ratio, model, negative_prompt, seed],
outputs=output)
demo.launch() |