from openai import OpenAI from IPython.display import Image, display # 直接在程式碼中設置 API 金鑰(不推薦,僅供參考) openai.api_key = "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" response = openai.images.generate( model="dall-e-3", prompt="a white siamese cat", size="1024x1024", quality="standard", n=1, ) image_url = response.data[0].url # 显示图片 if image_url.startswith("http"): display(Image(url=image_url)) else: print("Error displaying image:", image_url) import openai from IPython.display import Image, display # 让用户输入 OpenAI API 密钥 api_key = input("Enter your OpenAI API Key: ") # 让用户输入提示 (prompt) prompt = input("Enter your prompt (e.g., 'a white siamese cat'): ") # 设置用户输入的 API 密钥 openai.api_key = api_key # 调用 OpenAI API 生成图像 try: response = openai.images.generate( model="dall-e-3", prompt=prompt, n=1, size="1024x1024" ) # 获取生成的图像 URL image_url = response.data[0].url # 显示图像 if image_url.startswith("http"): display(Image(url=image_url)) else: print("Error displaying image:", image_url) except Exception as e: print(f"Error generating image: {e}”)