Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,35 +3,36 @@ import requests
|
|
3 |
import os
|
4 |
import base64
|
5 |
from PIL import Image
|
6 |
-
from io import BytesIO
|
7 |
|
8 |
# Set page title and layout
|
9 |
-
st.set_page_config(page_title="Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# API key from environment variable
|
12 |
API_KEY = os.environ.get("NEBIUS_API_KEY")
|
13 |
-
|
14 |
if not API_KEY:
|
15 |
st.error("API key not found. Please set the `NEBIUS_API_KEY` environment variable.")
|
16 |
|
17 |
-
# Function to call Nebius API
|
18 |
def generate_caption(image_base64, api_key):
|
19 |
api_url = "https://api.studio.nebius.ai/v1/chat/completions"
|
20 |
headers = {"Authorization": f"Bearer {api_key}"}
|
21 |
payload = {
|
22 |
"model": "Qwen/Qwen2-VL-7B-Instruct",
|
23 |
"messages": [
|
24 |
-
{
|
25 |
-
|
26 |
-
"
|
27 |
-
|
28 |
-
|
29 |
-
"role": "user",
|
30 |
-
"content": [
|
31 |
-
{"type": "text", "text": "Describe This Image In Detail under 100 words in this format: [image content/subject, description of action, state, and mood], [art form, style], [artist/photographer reference if needed], [additional settings such as camera and lens settings, lighting, colors, effects, texture, background, rendering]."},
|
32 |
-
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_base64}"}},
|
33 |
-
],
|
34 |
-
},
|
35 |
],
|
36 |
"temperature": 0.6,
|
37 |
}
|
@@ -49,58 +50,36 @@ def generate_caption(image_base64, api_key):
|
|
49 |
|
50 |
# File uploader for image
|
51 |
uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
|
|
|
|
52 |
|
53 |
if uploaded_image and API_KEY:
|
54 |
-
# Convert the image to base64
|
55 |
image = Image.open(uploaded_image)
|
56 |
-
buffered = BytesIO()
|
57 |
image.save(buffered, format="PNG")
|
58 |
image_base64 = base64.b64encode(buffered.getvalue()).decode()
|
59 |
-
|
60 |
-
# Show the uploaded image
|
61 |
st.image(image, caption="Uploaded Image", use_container_width=True)
|
62 |
-
|
63 |
-
|
64 |
-
if st.button("Generate Prompt", use_container_width=True):
|
65 |
st.write("Generating Prompt...")
|
66 |
result = generate_caption(image_base64, API_KEY)
|
67 |
-
|
68 |
-
# Extract and display the generated caption
|
69 |
if "error" in result:
|
70 |
st.error(f"Error: {result['error']}")
|
71 |
else:
|
72 |
try:
|
73 |
-
|
74 |
-
caption = (
|
75 |
-
result.get("choices", [{}])[0]
|
76 |
-
.get("message", {})
|
77 |
-
.get("content", "No caption generated.")
|
78 |
-
)
|
79 |
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
except Exception as e:
|
83 |
st.error(f"Error processing the response: {e}")
|
84 |
else:
|
85 |
-
|
86 |
-
st.warning("Please set the `NEBIUS_API_KEY` environment variable.")
|
87 |
-
else:
|
88 |
-
st.info("Please upload an image.")
|
89 |
-
|
90 |
-
# Remove the sidebar and center the content
|
91 |
-
st.markdown(
|
92 |
-
"""
|
93 |
-
<style>
|
94 |
-
.css-1d391kg {text-align: center;}
|
95 |
-
.css-1v3fvcr {justify-content: center; display: flex;}
|
96 |
-
.css-1y4ccs5 {margin: 0 auto;}
|
97 |
-
.css-1aumxhk {display: flex; justify-content: center;}
|
98 |
-
.css-1k6kn8p {justify-content: center; align-items: center; display: flex;}
|
99 |
-
.css-ffhzg6 {text-align: center;}
|
100 |
-
textarea {
|
101 |
-
color: white !important;
|
102 |
-
background-color: #262626 !important;
|
103 |
-
}
|
104 |
-
</style>
|
105 |
-
""", unsafe_allow_html=True
|
106 |
-
)
|
|
|
3 |
import os
|
4 |
import base64
|
5 |
from PIL import Image
|
6 |
+
from io import BytesIO
|
7 |
|
8 |
# Set page title and layout
|
9 |
+
st.set_page_config(page_title="AI Image Generator", layout="wide")
|
10 |
+
|
11 |
+
# Aspect Ratio Selection
|
12 |
+
aspect_ratios = {
|
13 |
+
"16:9 (1280x720)": (1280, 720),
|
14 |
+
"1:1 (1080x1080)": (1080, 1080),
|
15 |
+
"9:16 (720x1280)": (720, 1280),
|
16 |
+
"2:3 (800x1200)": (800, 1200),
|
17 |
+
}
|
18 |
|
19 |
# API key from environment variable
|
20 |
API_KEY = os.environ.get("NEBIUS_API_KEY")
|
|
|
21 |
if not API_KEY:
|
22 |
st.error("API key not found. Please set the `NEBIUS_API_KEY` environment variable.")
|
23 |
|
24 |
+
# Function to call Nebius API for prompt generation
|
25 |
def generate_caption(image_base64, api_key):
|
26 |
api_url = "https://api.studio.nebius.ai/v1/chat/completions"
|
27 |
headers = {"Authorization": f"Bearer {api_key}"}
|
28 |
payload = {
|
29 |
"model": "Qwen/Qwen2-VL-7B-Instruct",
|
30 |
"messages": [
|
31 |
+
{"role": "system", "content": "You are an image-to-text prompt converter for AI image generation."},
|
32 |
+
{"role": "user", "content": [
|
33 |
+
{"type": "text", "text": "Describe This Image In Detail under 100 words."},
|
34 |
+
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_base64}"}},
|
35 |
+
]},
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
],
|
37 |
"temperature": 0.6,
|
38 |
}
|
|
|
50 |
|
51 |
# File uploader for image
|
52 |
uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
53 |
+
selected_aspect = st.selectbox("Select Aspect Ratio", list(aspect_ratios.keys()))
|
54 |
+
width, height = aspect_ratios[selected_aspect]
|
55 |
|
56 |
if uploaded_image and API_KEY:
|
|
|
57 |
image = Image.open(uploaded_image)
|
58 |
+
buffered = BytesIO()
|
59 |
image.save(buffered, format="PNG")
|
60 |
image_base64 = base64.b64encode(buffered.getvalue()).decode()
|
61 |
+
|
|
|
62 |
st.image(image, caption="Uploaded Image", use_container_width=True)
|
63 |
+
|
64 |
+
if st.button("Generate Image", use_container_width=True):
|
|
|
65 |
st.write("Generating Prompt...")
|
66 |
result = generate_caption(image_base64, API_KEY)
|
67 |
+
|
|
|
68 |
if "error" in result:
|
69 |
st.error(f"Error: {result['error']}")
|
70 |
else:
|
71 |
try:
|
72 |
+
caption = result.get("choices", [{}])[0].get("message", {}).get("content", "No caption generated.")
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
# Construct the image generation URL
|
75 |
+
image_url = f"https://image.pollinations.ai/prompt/{requests.utils.quote(caption)}?width={width}&height={height}&nologo=true&model=flux&enhance=true&seed=119"
|
76 |
+
|
77 |
+
st.image(image_url, caption="Generated Image", use_column_width=True)
|
78 |
+
|
79 |
+
# Download button for the generated image
|
80 |
+
st.markdown(f"[Download Image]({image_url})", unsafe_allow_html=True)
|
81 |
+
|
82 |
except Exception as e:
|
83 |
st.error(f"Error processing the response: {e}")
|
84 |
else:
|
85 |
+
st.info("Please upload an image and select an aspect ratio.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|