Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Get token from environment variable (Hugging Face Spaces will provide this)
|
| 8 |
+
HF_API_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
+
|
| 10 |
+
# Best working model
|
| 11 |
+
MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 12 |
+
API_URL = f"https://api-inference.huggingface.co/models/{MODEL}"
|
| 13 |
+
|
| 14 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 15 |
+
|
| 16 |
+
def generate_image(prompt):
|
| 17 |
+
"""Generate image using Hugging Face API"""
|
| 18 |
+
if not prompt:
|
| 19 |
+
return None, "Please enter a prompt!"
|
| 20 |
+
|
| 21 |
+
if not HF_API_TOKEN:
|
| 22 |
+
return None, "β Error: HF_TOKEN not found. Please set it in Space settings."
|
| 23 |
+
|
| 24 |
+
payload = {"inputs": prompt}
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 28 |
+
|
| 29 |
+
if response.status_code == 200:
|
| 30 |
+
image = Image.open(BytesIO(response.content))
|
| 31 |
+
return image, "β
Image generated successfully!"
|
| 32 |
+
else:
|
| 33 |
+
return None, f"β Error {response.status_code}: {response.text}"
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return None, f"β Error: {str(e)}"
|
| 36 |
+
|
| 37 |
+
# Create Gradio Interface
|
| 38 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 39 |
+
gr.Markdown(
|
| 40 |
+
"""
|
| 41 |
+
# π¨ AI Image Generator
|
| 42 |
+
### Powered by Stable Diffusion XL
|
| 43 |
+
Enter a text prompt to generate an image!
|
| 44 |
+
"""
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
with gr.Column(scale=1):
|
| 49 |
+
prompt_input = gr.Textbox(
|
| 50 |
+
label="Enter your prompt",
|
| 51 |
+
placeholder="e.g., A futuristic city at sunset, Robot holding a red skateboard...",
|
| 52 |
+
lines=3
|
| 53 |
+
)
|
| 54 |
+
generate_btn = gr.Button("π¨ Generate Image", variant="primary", size="lg")
|
| 55 |
+
status_text = gr.Textbox(label="Status", interactive=False)
|
| 56 |
+
|
| 57 |
+
gr.Markdown("### π‘ Example Prompts:")
|
| 58 |
+
gr.Examples(
|
| 59 |
+
examples=[
|
| 60 |
+
["A cute cat wearing sunglasses on a beach"],
|
| 61 |
+
["A futuristic city at sunset with flying cars"],
|
| 62 |
+
["Robot holding a red skateboard"],
|
| 63 |
+
["A magical forest with glowing mushrooms"],
|
| 64 |
+
["An astronaut riding a horse on Mars"],
|
| 65 |
+
],
|
| 66 |
+
inputs=prompt_input
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
with gr.Column(scale=1):
|
| 70 |
+
image_output = gr.Image(label="Generated Image", type="pil")
|
| 71 |
+
|
| 72 |
+
# Connect the button
|
| 73 |
+
generate_btn.click(
|
| 74 |
+
fn=generate_image,
|
| 75 |
+
inputs=prompt_input,
|
| 76 |
+
outputs=[image_output, status_text]
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Also allow Enter key to generate
|
| 80 |
+
prompt_input.submit(
|
| 81 |
+
fn=generate_image,
|
| 82 |
+
inputs=prompt_input,
|
| 83 |
+
outputs=[image_output, status_text]
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Launch the app
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
print("π Starting AI Image Generator...")
|
| 89 |
+
print("π± Opening in your browser...")
|
| 90 |
+
demo.launch(share=False)
|