import requests import os from dotenv import load_dotenv # Install the 'python-dotenv' package # Load environment variables from a .env file load_dotenv() API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5" API_TOKEN = os.getenv("HF_API_TOKEN") # Use os.getenv to retrieve the API token from environment variables if API_TOKEN is None: raise ValueError("HF_API_TOKEN environment variable not set") headers = {"Authorization": f"Bearer {API_TOKEN}"} def query(payload): response = requests.post(API_URL, headers=headers, json=payload) return response.content image_bytes = query({ "inputs": "cat is drinking milk ", }) import io from PIL import Image image = Image.open(io.BytesIO(image_bytes)) image.show() # If you want to save the generated image to a file, you can use the save method image.save("generated_image.png")