File size: 3,724 Bytes
8ffbf51
c73a6b4
 
 
6e2e0c5
f92fe30
7c56b7b
2e0de3d
f92fe30
 
 
 
 
 
 
 
 
c73a6b4
c32572d
 
 
 
c73a6b4
f92fe30
c73a6b4
 
 
 
986b8bd
c73a6b4
f92fe30
 
45f5c71
f92fe30
 
a93e14b
e17b907
c73a6b4
7c56b7b
99dbe33
 
 
 
 
 
 
 
 
 
6e2e0c5
c73a6b4
 
f92fe30
 
a93e14b
c32572d
c73a6b4
f92fe30
8ffbf51
c73a6b4
f92fe30
2e0de3d
f92fe30
 
8aa1ea9
fc4d401
f92fe30
fc4d401
 
 
99dbe33
f92fe30
ecfb0cd
f92fe30
 
 
45f5c71
f92fe30
 
8aa1ea9
 
 
 
 
 
 
 
f92fe30
99dbe33
 
c73a6b4
f92fe30
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import streamlit as st
import requests
import os
import base64
from PIL import Image
from io import BytesIO

# Set page title and layout
st.set_page_config(page_title="AI Image Generator", layout="wide")

# Aspect Ratio Selection
aspect_ratios = {
    "16:9 (1280x720)": (1280, 720),
    "1:1 (1080x1080)": (1080, 1080),
    "9:16 (720x1280)": (720, 1280),
    "2:3 (800x1200)": (800, 1200),
}

# API key from environment variable
API_KEY = os.environ.get("NEBIUS_API_KEY")
if not API_KEY:
    st.error("API key not found. Please set the `NEBIUS_API_KEY` environment variable.")

# Function to call Nebius API for prompt generation
def generate_caption(image_base64, api_key):
    api_url = "https://api.studio.nebius.ai/v1/chat/completions"
    headers = {"Authorization": f"Bearer {api_key}"}
    payload = {
        "model": "mistralai/Mistral-Small-3.1-24B-Instruct-2503",
        "messages": [
            {"role": "system", "content": "You are an image-to-text prompt converter for AI image generation."},
            {"role": "user", "content": [
                {"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]."},
                {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_base64}"}},
            ]},
        ],
        "temperature": 0.6,
    }

    try:
        response = requests.post(api_url, json=payload, headers=headers)
        if response.status_code == 200:
            return response.json()
        else:
            st.error(f"API Error: {response.status_code}, {response.text}")
            return {"error": response.text}
    except Exception as e:
        st.error(f"An exception occurred: {e}")
        return {"error": str(e)}

# File uploader for image
uploaded_image = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
selected_aspect = st.selectbox("Select Aspect Ratio", list(aspect_ratios.keys()))
width, height = aspect_ratios[selected_aspect]

if uploaded_image and API_KEY:
    image = Image.open(uploaded_image)
    buffered = BytesIO()
    image.save(buffered, format="PNG")
    image_base64 = base64.b64encode(buffered.getvalue()).decode()
    
    st.image(image, caption="Uploaded Image", use_container_width=True)
    
    if st.button("Generate Image", use_container_width=True):
        st.write("Wait AI Is Generating Image...")
        result = generate_caption(image_base64, API_KEY)
        
        if "error" in result:
            st.error(f"Error: {result['error']}")
        else:
            try:
                caption = result.get("choices", [{}])[0].get("message", {}).get("content", "No caption generated.")
                
                # Construct the image generation URL
                image_url = f"https://image.pollinations.ai/prompt/{requests.utils.quote(caption)}?width={width}&height={height}&nologo=true&model=flux&enhance=true&seed=119"
                
                st.image(image_url, caption="Generated Image", use_container_width=True)
                
                # Download button for the generated image
                st.download_button(
    label="Download Image",
    data=requests.get(image_url).content,  # Fetch the image data
    file_name="generated_image.png",
    mime="image/png",
    use_container_width=True
)

            
            except Exception as e:
                st.error(f"Error processing the response: {e}")
else:
    st.info("Please upload an image and select an aspect ratio.")