CoolFace
Apppublic

RiccardoDandrea/Text2Image

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
0likes
streamlit_app.py58 linesDownload Raw Back to src
1import os2os.environ["HF_HOME"] = "/tmp/huggingface"  # Cache-Verzeichnis für Modelle3 4import streamlit as st5import torch6from diffusers import StableDiffusionPipeline7 8st.set_page_config(page_title="Text 2 Image Generator", page_icon="🎨", layout="centered")9 10st.title("🎨 Text → Image Generator (optimiert für CPU/GPU)")11 12MODEL_OPTIONS = {13    "segmind/tiny-sd": "Tiny-SD (leicht, schnell, CPU-freundlich)",14    "dreamlike-art/dreamlike-photoreal-2.0": "Dreamlike Photoreal 2.0 (fotorealistisch)",15    "Lykon/dreamshaper-7": "Dreamshaper-7 (künstlerisch)",16}17 18model_choice = st.selectbox(19    "Wähle ein Modell:",20    options=list(MODEL_OPTIONS.keys()),21    format_func=lambda x: MODEL_OPTIONS[x],22)23 24prompt = st.text_area("Prompt eingeben:", placeholder="z. B. A futuristic cityscape at sunset, ultra detailed")25negative_prompt = st.text_input("Optional: Negative Prompt")26 27# ------------------------28# Pipeline laden (gecached)29# ------------------------30@st.cache_resource31def load_pipeline(model_name: str):32    # Float32 wenn nur CPU, sonst Float16 für CUDA33    dtype = torch.float16 if torch.cuda.is_available() else torch.float3234    pipe = StableDiffusionPipeline.from_pretrained(model_name, torch_dtype=dtype)35    device = "cuda" if torch.cuda.is_available() else "cpu"36    return pipe.to(device)37 38# ------------------------39# Bild generieren40# ------------------------41if st.button("✨ Bild generieren"):42    if not prompt.strip():43        st.warning("Bitte gib zuerst einen Prompt ein!")44    else:45        with st.spinner(f"Generiere Bild mit {model_choice} ... (kann auf CPU länger dauern)"):46            pipe = load_pipeline(model_choice)47 48            image = pipe(49                prompt,50                negative_prompt=negative_prompt or None,51                guidance_scale=7.0,52                num_inference_steps=12,   # 🚀 schneller machen!53                height=384,               # 🚀 kleiner für CPU54                width=38455            ).images[0]56 57            st.image(image, caption=f"🖼️ Ergebnis ({model_choice})", use_container_width=True)58