superbsaeed commited on
Commit
a17e33e
·
verified ·
1 Parent(s): e398f42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -54
app.py CHANGED
@@ -1,65 +1,37 @@
1
- import os
2
  import gradio as gr
3
- import torch
4
- import spaces
5
- from diffusers import FluxPipeline
6
- from huggingface_hub import login
7
 
8
- # --- AUTHENTICATION ---
9
- token = os.getenv("HF_TOKEN")
10
- if token:
11
- login(token=token)
12
 
13
- # --- MODEL LOADING ---
14
- model_id = "black-forest-labs/FLUX.1-schnell"
15
 
16
- # Load without .to("cuda") here. Let ZeroGPU handle it.
17
- pipe = FluxPipeline.from_pretrained(
18
- model_id,
19
- torch_dtype=torch.bfloat16,
20
- token=token
21
- )
 
 
 
22
 
23
- @spaces.GPU(duration=60)
24
- def generate_image(prompt):
25
- if not prompt:
26
- return None
27
 
28
- try:
29
- # ZeroGPU specific: ensure the pipe is on the right device
30
- pipe.to("cuda")
 
 
31
 
32
- generator = torch.Generator("cuda").manual_seed(42)
33
-
34
- image = pipe(
35
- prompt=prompt,
36
- width=1024,
37
- height=1024,
38
- num_inference_steps=4,
39
- generator=generator,
40
- guidance_scale=0.0
41
- ).images[0]
42
-
43
- return image
44
- except Exception as e:
45
- # This will show the actual error message in the Gradio UI
46
- raise gr.Error(f"Generation failed: {str(e)}")
47
 
48
- # --- MINIMALIST UI ---
49
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
50
- gr.Markdown("# FLUX.1 Minimal")
51
-
52
- with gr.Group():
53
- prompt = gr.Textbox(
54
- placeholder="Describe your image...",
55
- show_label=False,
56
- container=False
57
- )
58
- btn = gr.Button("Generate", variant="primary")
59
-
60
- output_img = gr.Image(show_label=False, interactive=False)
61
-
62
- btn.click(fn=generate_image, inputs=[prompt], outputs=output_img)
63
 
64
  if __name__ == "__main__":
65
  demo.launch(share=True)
 
 
1
  import gradio as gr
2
+ from optimum.intel import OVStableDiffusionPipeline
 
 
 
3
 
4
+ # 1. Load the OpenVINO optimized model
5
+ # This model is specifically 'squeezed' to run on CPUs
6
+ model_id = "OpenVINO/stable-diffusion-v1-5-int8-ov"
 
7
 
8
+ print("Loading model to CPU... (This may take a minute)")
9
+ pipe = OVStableDiffusionPipeline.from_pretrained(model_id, compile=True)
10
 
11
+ # 2. Define the Generation Function
12
+ def generate_image(prompt, steps):
13
+ # On CPU, we keep steps low (1-4) for speed
14
+ image = pipe(
15
+ prompt=prompt,
16
+ num_inference_steps=steps,
17
+ guidance_scale=7.5
18
+ ).images[0]
19
+ return image
20
 
21
+ # 3. Gradio UI
22
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
23
+ gr.Markdown("# CPU-Fast Image Generator")
24
+ gr.Markdown("Using OpenVINO to run Stable Diffusion on a free CPU tier.")
25
 
26
+ with gr.Row():
27
+ with gr.Column():
28
+ prompt = gr.Textbox(label="Enter Prompt", value="A cozy cabin in the woods, digital art")
29
+ steps = gr.Slider(1, 10, value=4, step=1, label="Steps (Lower is faster)")
30
+ btn = gr.Button("Generate", variant="primary")
31
 
32
+ output_img = gr.Image(label="Result")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ btn.click(fn=generate_image, inputs=[prompt, steps], outputs=output_img)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  if __name__ == "__main__":
37
  demo.launch(share=True)