sreejang commited on
Commit
8254cea
·
verified ·
1 Parent(s): b77f830

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -23
app.py CHANGED
@@ -1,35 +1,63 @@
1
- import os
2
  import gradio as gr
3
- from huggingface_hub import InferenceClient
 
 
 
4
 
5
- HF_TOKEN = os.environ.get("HF_TOKEN")
6
- client = InferenceClient(token=HF_TOKEN) if HF_TOKEN else None
7
-
8
- def generate_image(prompt, negative_prompt=""):
9
- if not client:
10
- return None, "Set HF_TOKEN in Secrets!"
11
  try:
12
- image = client.text_to_image(
13
- prompt=prompt,
14
- negative_prompt=negative_prompt,
15
- model="stabilityai/stable-diffusion-xl-base-1.0",
16
- width=1024,
17
- height=1024
18
- )
19
- return image, "Success!"
 
 
 
 
 
 
 
20
  except Exception as e:
21
- return None, str(e)
22
 
23
- with gr.Blocks() as demo:
24
- gr.Markdown("# Free Image Generator (Hugging Face)")
 
 
25
  with gr.Row():
26
  with gr.Column():
27
- prompt = gr.Textbox(label="Prompt", lines=3)
28
- neg = gr.Textbox(label="Negative Prompt", value="blurry, low quality")
29
- btn = gr.Button("Generate", variant="primary")
 
 
 
 
 
 
 
 
30
  status = gr.Textbox(label="Status")
 
31
  with gr.Column():
32
  img = gr.Image(label="Generated Image")
33
- btn.click(generate_image, inputs=[prompt, neg], outputs=[img, status])
 
 
 
 
 
 
 
 
 
 
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()