Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from requests.structures import CaseInsensitiveDict
|
4 |
+
|
5 |
+
API_URL = "https://api.openai.com/v1/images/generations"
|
6 |
+
|
7 |
+
def generate_image(prompt, model, api_key):
|
8 |
+
headers = CaseInsensitiveDict()
|
9 |
+
headers["Content-Type"] = "application/json"
|
10 |
+
headers["Authorization"] = f"Bearer {api_key}"
|
11 |
+
|
12 |
+
data = """
|
13 |
+
{
|
14 |
+
"""
|
15 |
+
data += f'"model": "{model}",'
|
16 |
+
data += f'"prompt": "{prompt}",'
|
17 |
+
data += """
|
18 |
+
"num_images":1,
|
19 |
+
"size":"256x256",
|
20 |
+
"response_format":"url"
|
21 |
+
}
|
22 |
+
"""
|
23 |
+
|
24 |
+
resp = requests.post(API_URL, headers=headers, data=data)
|
25 |
+
|
26 |
+
if resp.status_code != 200:
|
27 |
+
raise ValueError("Failed to generate image")
|
28 |
+
|
29 |
+
response_text = resp.text
|
30 |
+
return response_text.strip()
|
31 |
+
|
32 |
+
st.title("Pixel Art Generator")
|
33 |
+
|
34 |
+
api_key = st.text_input("Enter your OpenAI API key:")
|
35 |
+
prompt = st.text_input("Enter your prompt (e.g. dog):")
|
36 |
+
model = "image-alpha-001"
|
37 |
+
|
38 |
+
if st.button("Generate Pixel Art"):
|
39 |
+
if api_key == "":
|
40 |
+
st.warning("Please enter your OpenAI API key")
|
41 |
+
elif prompt == "":
|
42 |
+
st.warning("Please enter a prompt")
|
43 |
+
else:
|
44 |
+
try:
|
45 |
+
image_url = generate_image(f"pixel art {prompt}", model, api_key)
|
46 |
+
st.image(image_url, width=256, caption="Generated Pixel Art")
|
47 |
+
except:
|
48 |
+
st.error("Failed to generate pixel art. Please try again later.")
|