Advertisement
Guest User

open ai code

a guest
Jan 8th, 2023
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1.  
  2.  
  3.  
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <title>OpenAI Image Generation</title>
  8. <style>
  9. /* Add some styling to the image */
  10. #generated-image {
  11. width: 256px; /* Set the width of the image */
  12. border: 1px solid black; /* Add a border around the image */
  13. border-radius: 5px; /* Round the corners of the border */
  14. margin-top: 10px; /* Add some space above the image */
  15. }
  16. </style>
  17. </head>
  18. <body>
  19.  
  20. <!-- Add a text field for the user to enter a prompt -->
  21. <input type="text" id="prompt" placeholder="Enter a prompt" style="width: 500px; height: 50px; font-size: 20px;">
  22.  
  23. <!-- Add a button to trigger the image generation -->
  24. <button id="generate-button" style="font-size: 20px;">Generate Image</button>
  25.  
  26. <!-- Add an image element to display the generated image -->
  27. <img id="generated-image" src="" alt="Generated Image">
  28. <img id="generated-image" src="" alt="Generated Image">
  29.  
  30. <script>
  31. // Add a click event listener to the generate button
  32. document.getElementById('generate-button').addEventListener('click', function() {
  33. // Get the user-entered prompt from the text field
  34. const prompt = document.getElementById('prompt').value;
  35.  
  36. // Your OpenAI API key
  37. const apiKey = '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';
  38.  
  39. // The API endpoint for image generation
  40. const apiUrl = 'https://api.openai.com/v1/images/generations';
  41.  
  42. // The data to send to the API
  43. const data = {
  44. 'prompt': prompt,
  45. 'model': 'image-alpha-001',
  46. 'num_images': 2,
  47. 'size': '1024x1024',
  48. 'response_format': 'url'
  49. };
  50.  
  51. // Send a POST request to the API
  52. fetch(apiUrl, {
  53. method: 'POST',
  54. headers: {
  55. 'Content-Type': 'application/json',
  56. 'Authorization': `Bearer ${apiKey}`
  57. },
  58. body: JSON.stringify(data)
  59. })
  60. .then(response => response.json())
  61. .then(json => {
  62. // Get the URL of the generated image from the API response
  63. const imageUrl = json.data[0].url;
  64.  
  65. // Set the src attribute of the image element to the generated image URL
  66. document.getElementById('generated-image').src = imageUrl;
  67. });
  68. });
  69. </script>
  70.  
  71. </body>
  72. </html>
  73.  
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement