Daniyal041 commited on
Commit
8d64177
·
verified ·
1 Parent(s): e04c48a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -16
app.py CHANGED
@@ -1,24 +1,57 @@
1
  import gradio as gr
2
- from diffusers import StableDiffusionPipeline
3
- import torch
4
 
5
- # This will use CPU and download ~5GB model files
6
- pipe = StableDiffusionPipeline.from_pretrained(
7
- "CompVis/stable-diffusion-v1-4",
8
- torch_dtype=torch.float32,
9
- use_safetensors=True
10
- )
11
 
12
  def generate_image(prompt):
13
- return pipe(prompt).images[0]
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- with gr.Blocks() as demo:
16
- gr.Markdown("## 🐢 Local CPU Generator (SD 1.4)")
 
17
  with gr.Row():
18
- prompt = gr.Textbox(label="Prompt")
19
- btn = gr.Button("Generate (May take 2-5 mins)")
20
- output = gr.Image(label="Result")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- btn.click(fn=generate_image, inputs=prompt, outputs=output)
 
 
 
 
23
 
24
- demo.launch()
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import os
4
 
5
+ # Initialize the Inference Client
6
+ client = InferenceClient(token=os.getenv("HF_TOKEN"))
 
 
 
 
7
 
8
  def generate_image(prompt):
9
+ try:
10
+ # Generate image using Stable Diffusion 2.1 (free tier compatible)
11
+ image = client.text_to_image(
12
+ prompt,
13
+ model="stabilityai/stable-diffusion-2-1",
14
+ negative_prompt="blurry, low quality", # Optional quality improvement
15
+ guidance_scale=7.5, # Controls creativity vs prompt adherence
16
+ height=512, # Standard size
17
+ width=512
18
+ )
19
+ return image
20
+ except Exception as e:
21
+ raise gr.Error(f"Generation failed: {str(e)}. Please try again later.")
22
 
23
+ with gr.Blocks(title="Free AI Image Generator") as demo:
24
+ gr.Markdown("## 🖼️ Free AI Image Generator (Powered by Hugging Face)")
25
+
26
  with gr.Row():
27
+ with gr.Column():
28
+ prompt = gr.Textbox(
29
+ label="Describe your image",
30
+ placeholder="A astronaut riding a horse on Mars",
31
+ lines=2
32
+ )
33
+ generate_btn = gr.Button("Generate Image", variant="primary")
34
+
35
+ with gr.Column():
36
+ output_image = gr.Image(
37
+ label="Generated Image",
38
+ height=512,
39
+ width=512
40
+ )
41
+
42
+ # Additional controls
43
+ with gr.Accordion("Advanced Options", open=False):
44
+ negative_prompt = gr.Textbox(
45
+ label="What to exclude from image",
46
+ placeholder="blurry, distorted, low quality"
47
+ )
48
+ guidance = gr.Slider(3, 20, value=7.5, label="Creativity vs Accuracy")
49
+ steps = gr.Slider(10, 50, value=25, label="Generation Steps")
50
 
51
+ generate_btn.click(
52
+ fn=generate_image,
53
+ inputs=[prompt, negative_prompt, guidance, steps],
54
+ outputs=output_image
55
+ )
56
 
57
+ demo.launch(share=True) # share=True creates public link