请把下面的curl命令转换为python代码,用python代码的gradio模块实现,两个文本输入框,一个是prompt和negativeprompt,同时把返回的图片信息在w…
- User
请把下面的curl命令转换为python代码,用python代码的gradio模块实现,两个文本输入框,一个是prompt和negative_prompt,同时把返回的图片信息在web页面显示出来,curl命令如下: curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/run/@cf/bytedance/stable-diffusion-xl-lightning
-X POST
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN"
-d '{ "prompt": prompt_word }' - Assistant
好的,我将把你提供的
curl命令转换为Python代码,并使用gradio模块创建一个具有两个文本输入框(prompt和negative_prompt)的简单Web界面,同时将返回的图片信息显示在网页上。以下是完整的实现代码:import gradio as gr import requests import json from PIL import Image from io import BytesIO # API details API_URL = "https://api.cloudflare.com/client/v4/accounts/YOUR_CLOUDFLARE_ACCOUNT_ID/ai/run/@cf/bytedance/stable-diffusion-xl-lightning" API_TOKEN = "YOUR_CLOUDFLARE_API_TOKEN" def generate_image(prompt, negative_prompt): headers = { "Authorization": f"Bearer {API_TOKEN}", "Content-Type": "application/json" } data = { "prompt": prompt, "negative_prompt": negative_prompt } response = requests.post(API_URL, headers=headers, data=json.dumps(data)) if response.status_code == 200: image_data = response.json().get("image") img = Image.open(BytesIO(image_data.encode('utf-8'))) return img else: return f"Error: {response.status_code}\n{response.text}" def generate_image_fn(prompt, negative_prompt): image = generate_image(prompt, negative_prompt) return image prompt_input = gr.inputs.Textbox(lines=2, placeholder="Enter prompt here...") negative_prompt_input = gr.inputs.Textbox(lines=2, placeholder="Enter negative prompt here...") gr.Interface( fn=generate_image_fn, inputs=[prompt_input, negative_prompt_input], outputs="image", title="AI Image Generator", description="Generate images using Stable Diffusion with given prompt and negative prompt." ).launch()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.