Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from huggingface_hub import InferenceClient | |
| # π token | |
| HF_TOKEN = os.getenv("image") | |
| client = InferenceClient( | |
| model="stabilityai/stable-diffusion-xl-base-1.0", | |
| token=HF_TOKEN | |
| ) | |
| def generate_image(prompt, width, height): | |
| if not prompt.strip(): | |
| return None, "β οΈ Enter prompt" | |
| try: | |
| image = client.text_to_image( | |
| prompt, | |
| width=int(width), | |
| height=int(height) | |
| ) | |
| return image, "β Image generated" | |
| except Exception as e: | |
| return None, f"β Error: {str(e)}" | |
| # π¨ CLEAN MODERN UI | |
| with gr.Blocks( | |
| theme=gr.themes.Soft( | |
| primary_hue="blue", | |
| secondary_hue="sky", | |
| neutral_hue="gray" | |
| ) | |
| ) as app: | |
| # β MAIN TITLE (fixed visibility) | |
| gr.Markdown( | |
| """ | |
| # π¨ AI Text β Image Generator | |
| ### β¨ Generate high-quality AI images using Stable Diffusion | |
| """, | |
| elem_id="title" | |
| ) | |
| with gr.Row(): | |
| prompt = gr.Textbox( | |
| label="βοΈ Enter Prompt", | |
| placeholder="A futuristic city at night, cinematic lighting...", | |
| lines=3 | |
| ) | |
| with gr.Row(): | |
| width = gr.Slider(256, 1024, value=768, step=64, label="π Width") | |
| height = gr.Slider(256, 1024, value=768, step=64, label="π Height") | |
| with gr.Row(): | |
| btn = gr.Button("π Generate Image", variant="primary") | |
| output_img = gr.Image(label="πΌοΈ Result", type="pil") | |
| status = gr.Textbox(label="π’ Status") | |
| btn.click(generate_image, [prompt, width, height], [output_img, status]) | |
| app.launch() |