appsnprojectsstpl-tech commited on
Commit
33a213e
·
1 Parent(s): 5d97cc0

Migrate to HF Inference API

Browse files
Files changed (2) hide show
  1. app.py +28 -89
  2. requirements.txt +2 -9
app.py CHANGED
@@ -1,55 +1,20 @@
1
- import torch
2
- import spaces
3
  import gradio as gr
4
- from diffusers import FluxPipeline, FluxImg2ImgPipeline
5
- import os
6
 
7
- print("Loading FLUX.1 Models (CPU)...")
8
- # Load the base FLUX model
9
- # We load the pipelines on CPU so ZeroGPU doesn't crash during startup.
10
- pipe_t2i = FluxPipeline.from_pretrained(
11
- "black-forest-labs/FLUX.1-schnell",
12
- torch_dtype=torch.bfloat16,
13
- token=os.environ.get("HF_TOKEN")
14
- )
15
-
16
- pipe_edit = FluxImg2ImgPipeline.from_pipe(pipe_t2i)
17
- print("Models loaded successfully!")
18
-
19
- @spaces.GPU
20
- def generate_or_edit(prompt, input_image, denoising_strength, num_inference_steps, seed, randomize_seed, progress=gr.Progress(track_tqdm=True)):
21
- # Move pipelines to GPU inside the ZeroGPU decorated function
22
- pipe_t2i.to("cuda")
23
- pipe_edit.to("cuda")
24
-
25
- if randomize_seed:
26
- seed = torch.randint(0, 2**32 - 1, (1,)).item()
27
- generator = torch.Generator("cuda").manual_seed(int(seed))
28
-
29
  if not prompt:
30
  raise gr.Error("Please enter a prompt!")
31
-
32
- if input_image is not None:
33
- # Edit mode (Img2Img)
34
- input_image = input_image.convert("RGB")
35
- image = pipe_edit(
36
- prompt=prompt,
37
- image=input_image,
38
- strength=denoising_strength,
39
- num_inference_steps=int(num_inference_steps),
40
- guidance_scale=0.0, # FLUX.1-schnell uses 0 guidance scale
41
- generator=generator,
42
- ).images[0]
43
- else:
44
- # Generate mode (T2I)
45
- image = pipe_t2i(
46
- prompt=prompt,
47
- num_inference_steps=int(num_inference_steps),
48
- guidance_scale=0.0, # FLUX.1-schnell uses 0 guidance scale
49
- generator=generator,
50
- ).images[0]
51
 
52
- return image, seed
 
 
 
 
 
 
 
53
 
54
  # UI
55
  custom_theme = gr.themes.Soft(
@@ -62,66 +27,40 @@ with gr.Blocks() as demo:
62
  gr.Markdown(
63
  """
64
  # ⚡ FLUX.1 Image Studio (Grok Quality)
65
- Generate state-of-the-art images from scratch, or edit existing ones using the FLUX.1-schnell model.
 
66
  """
67
  )
68
 
69
  with gr.Row():
70
  with gr.Column(scale=1):
 
 
 
 
 
 
71
  prompt = gr.Textbox(
72
  label="✨ Prompt",
73
  lines=3,
74
- placeholder="e.g. A cyberpunk cat...",
75
  autofocus=True
76
  )
77
- input_image = gr.Image(
78
- label="🖼️ Input Image (Optional - For editing)",
79
- type="pil"
80
- )
81
 
82
- with gr.Accordion("⚙️ Advanced Settings", open=False):
83
- denoising_strength = gr.Slider(
84
- minimum=0.0,
85
- maximum=1.0,
86
- value=0.5,
87
- step=0.05,
88
- label="Denoising Strength (Editing Only)",
89
- info="Lower = keeps more of original image. Higher = completely changes image to match prompt."
90
- )
91
- num_inference_steps = gr.Slider(
92
- minimum=1,
93
- maximum=12,
94
- value=4,
95
- step=1,
96
- label="Inference Steps",
97
- info="FLUX.1-schnell is optimized for 4 steps."
98
- )
99
-
100
- with gr.Row():
101
- randomize_seed = gr.Checkbox(label="🎲 Random Seed", value=True)
102
- seed = gr.Number(label="Seed", value=42, precision=0, visible=False)
103
-
104
- randomize_seed.change(
105
- lambda r: gr.Number(visible=not r),
106
- inputs=[randomize_seed],
107
- outputs=[seed]
108
- )
109
-
110
- generate_btn = gr.Button("🚀 Generate / Edit Image", variant="primary", size="lg")
111
 
112
  with gr.Column(scale=1):
113
  output_image = gr.Image(label="Result", type="pil", interactive=False)
114
- used_seed = gr.Number(label="Seed Used", interactive=False)
115
 
116
  generate_btn.click(
117
- fn=generate_or_edit,
118
- inputs=[prompt, input_image, denoising_strength, num_inference_steps, seed, randomize_seed],
119
- outputs=[output_image, used_seed]
120
  )
121
  prompt.submit(
122
- fn=generate_or_edit,
123
- inputs=[prompt, input_image, denoising_strength, num_inference_steps, seed, randomize_seed],
124
- outputs=[output_image, used_seed]
125
  )
126
 
127
  if __name__ == "__main__":
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
 
3
 
4
+ def generate_image(prompt, hf_token, progress=gr.Progress(track_tqdm=True)):
5
+ if not hf_token:
6
+ raise gr.Error("Please enter your Hugging Face API Token!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  if not prompt:
8
  raise gr.Error("Please enter a prompt!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ client = InferenceClient(token=hf_token.strip())
11
+
12
+ image = client.text_to_image(
13
+ prompt,
14
+ model="black-forest-labs/FLUX.1-schnell"
15
+ )
16
+
17
+ return image
18
 
19
  # UI
20
  custom_theme = gr.themes.Soft(
 
27
  gr.Markdown(
28
  """
29
  # ⚡ FLUX.1 Image Studio (Grok Quality)
30
+ Generate ultra-fast images from text using the real-time FLUX.1-schnell model via Hugging Face Serverless API.
31
+ *No local GPU Required! Generates in the cloud.*
32
  """
33
  )
34
 
35
  with gr.Row():
36
  with gr.Column(scale=1):
37
+ hf_token = gr.Textbox(
38
+ label="🔑 Hugging Face Access Token",
39
+ placeholder="hf_...",
40
+ type="password",
41
+ info="Paste your Hugging Face Token here"
42
+ )
43
  prompt = gr.Textbox(
44
  label="✨ Prompt",
45
  lines=3,
46
+ placeholder="e.g. A futuristic cyberpunk city at night...",
47
  autofocus=True
48
  )
 
 
 
 
49
 
50
+ generate_btn = gr.Button("🎨 Generate Image", variant="primary", size="lg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  with gr.Column(scale=1):
53
  output_image = gr.Image(label="Result", type="pil", interactive=False)
 
54
 
55
  generate_btn.click(
56
+ fn=generate_image,
57
+ inputs=[prompt, hf_token],
58
+ outputs=[output_image]
59
  )
60
  prompt.submit(
61
+ fn=generate_image,
62
+ inputs=[prompt, hf_token],
63
+ outputs=[output_image]
64
  )
65
 
66
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -1,9 +1,2 @@
1
- gradio
2
- git+https://github.com/huggingface/diffusers
3
- transformers
4
- kernels
5
- gradio[mcp]
6
- sentencepiece
7
- protobuf
8
- spaces
9
- accelerate
 
1
+ gradio==4.36.1
2
+ huggingface_hub