Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,63 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
if not client:
|
| 10 |
-
return None, "Set HF_TOKEN in Secrets!"
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
-
return None, str(e)
|
| 22 |
|
| 23 |
-
with gr.Blocks() as demo:
|
| 24 |
-
gr.Markdown("# Free Image Generator
|
|
|
|
|
|
|
| 25 |
with gr.Row():
|
| 26 |
with gr.Column():
|
| 27 |
-
prompt = gr.Textbox(
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
status = gr.Textbox(label="Status")
|
|
|
|
| 31 |
with gr.Column():
|
| 32 |
img = gr.Image(label="Generated Image")
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import urllib.parse
|
| 6 |
|
| 7 |
+
def generate_image(prompt, width=1024, height=1024, seed=0):
|
| 8 |
+
if not prompt:
|
| 9 |
+
return None, "Enter a prompt"
|
| 10 |
+
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
+
# Encode prompt for URL
|
| 13 |
+
encoded_prompt = urllib.parse.quote(prompt)
|
| 14 |
+
|
| 15 |
+
# Pollinations.ai endpoint (completely free)
|
| 16 |
+
url = f"https://image.pollinations.ai/prompt/{encoded_prompt}?width={width}&height={height}&nologo=true"
|
| 17 |
+
|
| 18 |
+
if seed > 0:
|
| 19 |
+
url += f"&seed={seed}"
|
| 20 |
+
|
| 21 |
+
# Download image
|
| 22 |
+
response = requests.get(url, timeout=30)
|
| 23 |
+
image = Image.open(BytesIO(response.content))
|
| 24 |
+
|
| 25 |
+
return image, "✅ Success! (Powered by Pollinations.ai - Free)"
|
| 26 |
+
|
| 27 |
except Exception as e:
|
| 28 |
+
return None, f"Error: {str(e)}"
|
| 29 |
|
| 30 |
+
with gr.Blocks(title="Free Image Generator (No API Key)") as demo:
|
| 31 |
+
gr.Markdown("# 🎨 Free Image Generator")
|
| 32 |
+
gr.Markdown("**No API key required!** Powered by Pollinations.ai")
|
| 33 |
+
|
| 34 |
with gr.Row():
|
| 35 |
with gr.Column():
|
| 36 |
+
prompt = gr.Textbox(
|
| 37 |
+
label="Prompt",
|
| 38 |
+
lines=3,
|
| 39 |
+
placeholder="A beautiful sunset over mountains..."
|
| 40 |
+
)
|
| 41 |
+
with gr.Row():
|
| 42 |
+
width = gr.Slider(512, 1024, 1024, step=64, label="Width")
|
| 43 |
+
height = gr.Slider(512, 1024, 1024, step=64, label="Height")
|
| 44 |
+
seed = gr.Number(0, label="Seed (0 for random)", precision=0)
|
| 45 |
+
|
| 46 |
+
btn = gr.Button("🎨 Generate (Free)", variant="primary")
|
| 47 |
status = gr.Textbox(label="Status")
|
| 48 |
+
|
| 49 |
with gr.Column():
|
| 50 |
img = gr.Image(label="Generated Image")
|
| 51 |
+
|
| 52 |
+
examples = gr.Examples(
|
| 53 |
+
examples=[
|
| 54 |
+
["A futuristic city, cyberpunk, neon lights"],
|
| 55 |
+
["Portrait of a cat wearing crown, oil painting"],
|
| 56 |
+
["Space nebula, vibrant colors, stars"],
|
| 57 |
+
],
|
| 58 |
+
inputs=prompt
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
btn.click(generate_image, inputs=[prompt, width, height, seed], outputs=[img, status])
|
| 62 |
|
| 63 |
demo.launch()
|