Spaces:
Sleeping
Sleeping
File size: 1,543 Bytes
2a21347 24bb010 2a21347 d9d4282 2a21347 24bb010 2a21347 24bb010 2a21347 24bb010 d9d4282 2a21347 24bb010 2a21347 d9d4282 24bb010 d9d4282 24bb010 2a21347 24bb010 |
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 |
import openai
import streamlit as st
from PIL import Image
import requests
from io import BytesIO
# Function to generate image from OpenAI API
def generate_image_from_text(text):
try:
response = openai.Image.create(
prompt=text, # The text prompt for the image
n=1, # Number of images to generate
size="1024x1024" # Size of the image
)
# Extracting the image URL from the response
image_url = response['data'][0]['url']
return image_url
except Exception as e:
st.error(f"Error generating image: {e}")
return None
# Streamlit UI for Image Generation
if __name__ == "__main__":
st.title("Test OpenAI Image Generation API")
# Load OpenAI API key securely
openai.api_key = st.secrets["OPENAI_API_KEY"]
user_input = st.text_input("Enter text for image generation:")
if st.button("Generate Image"):
if user_input:
image_url = generate_image_from_text(user_input)
if image_url:
# Fetch the image from the URL and display it
response = requests.get(image_url)
if response.status_code == 200:
img = Image.open(BytesIO(response.content))
st.image(img, caption="Generated Image")
else:
st.error("Failed to fetch image.")
else:
st.error("Image generation failed.")
else:
st.error("Please enter text to generate an image.")
|