| from huggingface_hub import InferenceClient | |
| from PIL import Image | |
| import io | |
| import streamlit as st | |
| # Streamlit UI setup | |
| st.set_page_config(page_title="Text-to-Image Generator", layout="centered") | |
| st.title("🖼️ Text to Image Generator") | |
| st.markdown("Enter a prompt and generate an image using the Flux model from Hugging Face.") | |
| # Input for the prompt | |
| prompt = st.text_input("Enter your prompt", "Astronaut riding a horse") | |
| # Generate button | |
| if st.button("Generate Image"): | |
| with st.spinner("Generating... please wait"): | |
| try: | |
| # Initialize the Inference Client | |
| client = InferenceClient( | |
| provider="fal-ai", | |
| api_key=st.secrets["TOGETHER_API_KEY"], # Store your key in .streamlit/secrets.toml | |
| ) | |
| # Generate image | |
| image = client.text_to_image( | |
| prompt, | |
| model="black-forest-labs/FLUX.1-dev", | |
| ) | |
| # Show image | |
| st.image(image, caption=f"Generated Image for: {prompt}", use_column_width=True) | |
| except Exception as e: | |
| st.error(f"Error: {str(e)}") | |