Spaces:
Sleeping
Sleeping
Upload image_generation_app.py
Browse files- image_generation_app.py +33 -0
image_generation_app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
openai.api_key = "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"
|
| 7 |
+
|
| 8 |
+
def generate_image(prompt):
|
| 9 |
+
try:
|
| 10 |
+
response = openai.images.generate(
|
| 11 |
+
model="dall-e-3",
|
| 12 |
+
prompt=prompt,
|
| 13 |
+
n=1,
|
| 14 |
+
size="1024x1024"
|
| 15 |
+
)
|
| 16 |
+
return response.data[0].url
|
| 17 |
+
|
| 18 |
+
except openai.OpenAIError as e:
|
| 19 |
+
st.error(f"Error generating image: {e}")
|
| 20 |
+
return None
|
| 21 |
+
except Exception as e:
|
| 22 |
+
st.error(f"An unexpected error occurred: {e}")
|
| 23 |
+
return None
|
| 24 |
+
|
| 25 |
+
st.title(" Image Generator")
|
| 26 |
+
prompt = st.text_input("Enter a prompt:")
|
| 27 |
+
|
| 28 |
+
if st.button("Generate"):
|
| 29 |
+
if prompt:
|
| 30 |
+
with st.spinner("Generating image..."):
|
| 31 |
+
image_url = generate_image(prompt)
|
| 32 |
+
if image_url:
|
| 33 |
+
st.image(image_url)
|