import streamlit as st import requests import io from PIL import Image st.set_page_config(page_title="dreamforge · AI Image Generator", page_icon="🔮", layout="wide") st.markdown(""" """, unsafe_allow_html=True) MODELS = { "⚡ FLUX Schnell (fastest)": "black-forest-labs/FLUX.1-schnell", "🎨 SDXL Base (high quality)": "stabilityai/stable-diffusion-xl-base-1.0", "🖼 SD 1.5 (classic)": "runwayml/stable-diffusion-v1-5", "🌅 Openjourney (Midjourney style)": "prompthero/openjourney-v4", } STYLES = { "None": "", "Cinematic": ", cinematic lighting, film grain, anamorphic lens", "Anime": ", anime style, vibrant colors, studio ghibli", "Oil Painting": ", oil painting, textured brushstrokes, classical art", "Neon Noir": ", neon noir, rain-slicked streets, cyberpunk", "Soft Dream": ", soft focus, dreamy, pastel tones, ethereal", "Raw Photo": ", photorealistic, RAW photo, DSLR, 85mm lens", } RATIOS = { "1:1 (512×512)": (512, 512), "3:2 (768×512)": (768, 512), "2:3 (512×768)": (512, 768), "16:9 (896×512)": (896, 512), } HF_API = "https://api-inference.huggingface.co/models/" st.markdown("# 🔮 dreamforge") st.markdown('
AI image generation · Hugging Face Inference API
', unsafe_allow_html=True) st.markdown("---") col_left, col_right = st.columns([1, 1], gap="large") with col_left: hf_token = st.text_input("🔑 Hugging Face Token", type="password", placeholder="hf_xxxxxxxxxxxxxxxxxxxxxxxx", help="Get yours free at huggingface.co/settings/tokens") prompt = st.text_area("✏️ Prompt", placeholder="A misty mountain at golden hour, oil on canvas…", height=120) neg_prompt = st.text_area("🚫 Negative Prompt", value="blurry, low quality, watermark, text, ugly, deformed", height=68) model_label = st.selectbox("🤖 Model", list(MODELS.keys())) c1, c2 = st.columns(2) with c1: style_label = st.selectbox("🎨 Style", list(STYLES.keys())) with c2: ratio_label = st.selectbox("📐 Ratio", list(RATIOS.keys())) c3, c4 = st.columns(2) with c3: steps = st.slider("Steps", 1, 50, 4) with c4: guidance = st.slider("Guidance Scale", 1.0, 20.0, 7.5, 0.5) generate = st.button("✦ Generate Image", use_container_width=True) with col_right: st.markdown("### Output") if generate: if not hf_token.strip(): st.error("🔑 Please enter your Hugging Face API token.") elif not prompt.strip(): st.error("✏️ Please enter a prompt.") else: model_id = MODELS[model_label] full_prompt = prompt + STYLES[style_label] width, height = RATIOS[ratio_label] headers = {"Authorization": f"Bearer {hf_token.strip()}"} payload = {"inputs": full_prompt} if "FLUX" in model_label: payload["parameters"] = {"num_inference_steps": steps, "width": width, "height": height} else: payload["parameters"] = {"negative_prompt": neg_prompt, "num_inference_steps": steps, "guidance_scale": guidance, "width": width, "height": height} with st.spinner("Synthesizing pixels…"): try: resp = requests.post(HF_API + model_id, headers=headers, json=payload, timeout=120) if resp.status_code == 503: st.warning("⏳ Model is loading — wait 20 seconds and try again.") elif not resp.ok: try: detail = resp.json().get("error", resp.text) except: detail = resp.text st.error(f"API error {resp.status_code}: {detail}") else: image = Image.open(io.BytesIO(resp.content)) st.image(image, use_container_width=True) buf = io.BytesIO() image.save(buf, format="PNG") st.download_button("⬇ Download PNG", data=buf.getvalue(), file_name="dreamforge_output.png", mime="image/png", use_container_width=True) st.caption(f"**Prompt:** {full_prompt}") except requests.exceptions.Timeout: st.error("⏱ Request timed out. Try fewer steps or a faster model.") except Exception as e: st.error(f"Something went wrong: {e}") else: st.markdown('dreamforge · get your free HF token
', unsafe_allow_html=True)