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
|
| 5 |
+
import io
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Get API key from environment variable
|
| 12 |
+
# Set your API key in Hugging Face Space Settings > Variables and Secrets
|
| 13 |
+
# Name it: API_KEY
|
| 14 |
+
API_KEY = os.getenv("API_KEY")
|
| 15 |
+
API_URL = os.getenv("API_URL", "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0")
|
| 16 |
+
|
| 17 |
+
def generate_image(prompt, negative_prompt="", width=1024, height=1024, guidance_scale=7.5, num_inference_steps=50):
|
| 18 |
+
"""
|
| 19 |
+
Generate image using Hugging Face Inference API or similar API
|
| 20 |
+
"""
|
| 21 |
+
if not API_KEY:
|
| 22 |
+
return None, "Error: API key not configured. Please set the API_KEY in your Space secrets."
|
| 23 |
+
|
| 24 |
+
if not prompt:
|
| 25 |
+
return None, "Error: Please enter a prompt."
|
| 26 |
+
|
| 27 |
+
headers = {
|
| 28 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 29 |
+
"Content-Type": "application/json"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
payload = {
|
| 33 |
+
"inputs": prompt,
|
| 34 |
+
"parameters": {
|
| 35 |
+
"negative_prompt": negative_prompt,
|
| 36 |
+
"width": width,
|
| 37 |
+
"height": height,
|
| 38 |
+
"guidance_scale": guidance_scale,
|
| 39 |
+
"num_inference_steps": num_inference_steps,
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=300)
|
| 45 |
+
|
| 46 |
+
if response.status_code != 200:
|
| 47 |
+
return None, f"API Error: {response.status_code} - {response.text}"
|
| 48 |
+
|
| 49 |
+
# Handle image response
|
| 50 |
+
image_bytes = response.content
|
| 51 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 52 |
+
return image, "Success!"
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return None, f"Error: {str(e)}"
|
| 56 |
+
|
| 57 |
+
# Create Gradio interface
|
| 58 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 59 |
+
gr.Markdown("""
|
| 60 |
+
# 🎨 AI Image Generator
|
| 61 |
+
|
| 62 |
+
Generate stunning images from text prompts using AI. Enter your prompt below and click Generate.
|
| 63 |
+
""")
|
| 64 |
+
|
| 65 |
+
with gr.Row():
|
| 66 |
+
with gr.Column(scale=1):
|
| 67 |
+
prompt_input = gr.Textbox(
|
| 68 |
+
label="Prompt",
|
| 69 |
+
placeholder="A serene landscape with mountains and a lake at sunset...",
|
| 70 |
+
lines=3
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
negative_prompt_input = gr.Textbox(
|
| 74 |
+
label="Negative Prompt (what to avoid)",
|
| 75 |
+
placeholder="blurry, low quality, distorted...",
|
| 76 |
+
lines=2
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
with gr.Row():
|
| 80 |
+
width_slider = gr.Slider(
|
| 81 |
+
minimum=512,
|
| 82 |
+
maximum=2048,
|
| 83 |
+
step=64,
|
| 84 |
+
value=1024,
|
| 85 |
+
label="Width"
|
| 86 |
+
)
|
| 87 |
+
height_slider = gr.Slider(
|
| 88 |
+
minimum=512,
|
| 89 |
+
maximum=2048,
|
| 90 |
+
step=64,
|
| 91 |
+
value=1024,
|
| 92 |
+
label="Height"
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
guidance_slider = gr.Slider(
|
| 96 |
+
minimum=1,
|
| 97 |
+
maximum=20,
|
| 98 |
+
step=0.5,
|
| 99 |
+
value=7.5,
|
| 100 |
+
label="Guidance Scale"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
steps_slider = gr.Slider(
|
| 104 |
+
minimum=20,
|
| 105 |
+
maximum=100,
|
| 106 |
+
step=1,
|
| 107 |
+
value=50,
|
| 108 |
+
label="Inference Steps"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
generate_btn = gr.Button("✨ Generate Image", variant="primary")
|
| 112 |
+
status_text = gr.Textbox(label="Status", interactive=False)
|
| 113 |
+
|
| 114 |
+
with gr.Column(scale=1):
|
| 115 |
+
output_image = gr.Image(label="Generated Image", type="pil")
|
| 116 |
+
|
| 117 |
+
gr.Markdown("""
|
| 118 |
+
### Tips for better results:
|
| 119 |
+
- Be specific and descriptive in your prompts
|
| 120 |
+
- Include style keywords: "digital art", "photorealistic", "oil painting"
|
| 121 |
+
- Mention lighting: "cinematic lighting", "golden hour", "studio lighting"
|
| 122 |
+
""")
|
| 123 |
+
|
| 124 |
+
# Event handlers
|
| 125 |
+
generate_btn.click(
|
| 126 |
+
fn=generate_image,
|
| 127 |
+
inputs=[
|
| 128 |
+
prompt_input,
|
| 129 |
+
negative_prompt_input,
|
| 130 |
+
width_slider,
|
| 131 |
+
height_slider,
|
| 132 |
+
guidance_slider,
|
| 133 |
+
steps_slider
|
| 134 |
+
],
|
| 135 |
+
outputs=[output_image, status_text]
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# Examples
|
| 139 |
+
gr.Examples(
|
| 140 |
+
examples=[
|
| 141 |
+
["A futuristic city at night with neon lights and flying cars, digital art", "", 1024, 1024, 7.5, 50],
|
| 142 |
+
["Portrait of a wise wizard with long beard, magical atmosphere, oil painting", "blurry, ugly", 1024, 1024, 8, 50],
|
| 143 |
+
["Cute robot reading a book in a cozy library, pixar style", "", 1024, 1024, 7, 50],
|
| 144 |
+
],
|
| 145 |
+
inputs=[prompt_input, negative_prompt_input, width_slider, height_slider, guidance_slider, steps_slider],
|
| 146 |
+
label="Example Prompts"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
if __name__ == "__main__":
|
| 150 |
+
demo.launch()
|