Text-to-Image / app.py
nomanmanzoor's picture
Update app.py
aa5947f verified
# app.py
import streamlit as st
import requests
import os
import base64
st.set_page_config(page_title="Text to Image Generator", layout="wide")
st.markdown("""
<style>
.stApp {
background-color: #f5f7fa;
}
.title {
font-size: 3em;
font-weight: 700;
text-align: center;
color: #2c3e50;
margin-bottom: 0.5em;
}
.footer {
text-align: center;
margin-top: 3em;
font-size: 0.9em;
color: gray;
}
</style>
""", unsafe_allow_html=True)
st.markdown('<div class="title">πŸ–ΌοΈ AI Text to Image Generator</div>', unsafe_allow_html=True)
API_URL = "https://api-inference.huggingface.co/models/prompthero/openjourney"
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN', '')}"}
# Sidebar Controls
with st.sidebar:
st.header("πŸ”§ Controls")
category = st.selectbox("Category", ["Fantasy", "Cyberpunk", "Anime", "Landscape", "Portrait", "Architecture"])
language = st.selectbox("Language", ["English", "Urdu", "Spanish", "French", "Chinese"])
color_mode = st.radio("Color Mode", ["Color", "Black & White"])
seed_toggle = st.checkbox("Use Random Seed", value=True)
prompt = st.text_area("Enter your prompt", placeholder="e.g., A futuristic city at sunset in cyberpunk style...")
generate_button = st.button("Generate Image")
@st.cache_data(show_spinner=False)
def query(payload):
try:
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
return response.content
except Exception as e:
return None
if generate_button and prompt:
with st.spinner("Generating image, please wait..."):
prompt_full = f"{prompt}, {category} style, Language: {language}, Mode: {color_mode}"
data = query({"inputs": prompt_full})
if data is None:
st.error("❌ Failed to connect to the model. Please check your token or internet.")
elif b"error" in data:
st.error("🚫 API returned an error. Try again later.")
else:
st.image(data, use_column_width=True)
st.markdown('<div class="footer">πŸš€ Built by @NumanManzo41980 | Follow for more AI projects.</div>', unsafe_allow_html=True)