File size: 1,873 Bytes
8360c81
47a73c7
8360c81
 
 
855c0dd
428e205
855c0dd
428e205
 
 
 
 
855c0dd
8360c81
 
 
428e205
 
47a73c7
428e205
 
8360c81
47a73c7
ba138bc
8360c81
428e205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from diffusers import AutoPipelineForText2Image
import torch
from PIL import Image
import io
import os
import requests

# --- CONFIG ---
USE_GROQ = False  # Set to True when Groq image API is available
GROQ_API_URL = "https://your-groq-image-api.com/generate"  # Placeholder

# Force CPU
os.environ["CUDA_VISIBLE_DEVICES"] = ""

@st.cache_resource
def load_model():
    if USE_GROQ:
        return None  # Skip local model
    pipe = AutoPipelineForText2Image.from_pretrained(
        "stabilityai/sd-turbo",
        torch_dtype=torch.float32
    )
    pipe.to("cpu")
    return pipe

def generate_image_local(prompt, guidance_scale):
    pipe = load_model()
    result = pipe(prompt, guidance_scale=guidance_scale, num_inference_steps=20)
    return result.images[0]

def generate_image_from_groq(prompt):
    response = requests.post(GROQ_API_URL, json={"prompt": prompt})
    if response.status_code == 200:
        image_bytes = io.BytesIO(response.content)
        return Image.open(image_bytes)
    else:
        raise Exception(f"GROQ API failed: {response.text}")

# UI
st.title("🧠 AI Image Generator (Fast with API / Groq-ready)")

prompt = st.text_input("Prompt:", "A glowing alien forest with floating orbs, concept art, 8K")
guidance = st.slider("Guidance scale", 1.0, 10.0, 3.0)

if st.button("Generate"):
    with st.spinner("Generating..."):
        try:
            if USE_GROQ:
                image = generate_image_from_groq(prompt)
            else:
                image = generate_image_local(prompt, guidance)

            st.image(image, caption="Generated Image", use_column_width=True)
            buf = io.BytesIO()
            image.save(buf, format="PNG")
            st.download_button("Download Image", buf.getvalue(), "generated.png", "image/png")
        except Exception as e:
            st.error(f"Error: {e}")