rajkr commited on
Commit
26fbae5
·
verified ·
1 Parent(s): 19ac4ad

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +28 -27
app.py CHANGED
@@ -5,14 +5,24 @@ import random
5
 
6
  HF_TOKEN = os.environ.get("HF_TOKEN")
7
 
8
- client_hf = InferenceClient(provider="hf-inference", token=HF_TOKEN)
9
- client_fal = InferenceClient(provider="fal-ai", api_key=HF_TOKEN)
10
 
11
  MODELS = {
12
- "SDXL Base": {"model_id": "stabilityai/stable-diffusion-xl-base-1.0", "client": client_fal},
13
- "FLUX.1-schnell": {"model_id": "black-forest-labs/FLUX.1-schnell", "client": client_fal},
14
- "FLUX.1-dev": {"model_id": "black-forest-labs/FLUX.1-dev", "client": client_fal},
15
- "SD v1.5 (Free)": {"model_id": "stable-diffusion-v1-5/stable-diffusion-v1-5", "client": client_hf},
 
 
 
 
 
 
 
 
 
 
 
16
  }
17
 
18
  DEFAULT_MODEL = "SDXL Base"
@@ -40,41 +50,32 @@ def generate_image(prompt, model_name=DEFAULT_MODEL, style="None", negative_prom
40
  seed = random.randint(0, 2**32 - 1)
41
  model_info = MODELS.get(model_name, MODELS[DEFAULT_MODEL])
42
  model_id = model_info["model_id"]
43
- client = model_info["client"]
 
 
 
44
 
45
  try:
46
  image = client.text_to_image(
47
  prompt=full_prompt, model=model_id,
48
  negative_prompt=negative_prompt if negative_prompt.strip() else None,
49
- guidance_scale=guidance_scale, num_inference_steps=num_inference_steps,
50
  width=width, height=height, seed=seed,
51
  )
52
- return image, f"Seed: {seed} | Model: {model_name}"
53
  except Exception as e:
54
- error_msg = str(e)
55
- if client != client_hf:
56
- try:
57
- image = client_hf.text_to_image(
58
- prompt=full_prompt, model="stable-diffusion-v1-5/stable-diffusion-v1-5",
59
- negative_prompt=negative_prompt if negative_prompt.strip() else None,
60
- guidance_scale=guidance_scale, num_inference_steps=num_inference_steps,
61
- width=min(width, 512), height=min(height, 512), seed=seed,
62
- )
63
- return image, f"Fallback SD v1.5 | Seed: {seed}" + f" | Size: {min(width, 512)}x{min(height, 512)}"
64
- except Exception as e2:
65
- raise gr.Error(f"Primary: {error_msg} | Fallback: {str(e2)}")
66
- raise gr.Error(f"Generation failed: {error_msg}")
67
 
68
 
69
  EXAMPLES = [
70
  ["A majestic dragon flying over a crystal lake at sunset, epic fantasy art", "SDXL Base", "Fantasy", "", 7.5, 30, -1, 1024, 1024],
71
  ["A cute robot exploring a colorful garden", "SDXL Base", "Anime", "", 7.5, 30, -1, 1024, 1024],
72
- ["An astronaut riding a horse on Mars", "FLUX.1-schnell", "Photorealistic", "", 7.5, 30, -1, 1024, 1024],
73
  ["A cozy coffee shop interior with rain outside", "SDXL Base", "Digital Art", "", 7.5, 30, -1, 1024, 1024],
74
  ["A futuristic city skyline at night", "SDXL Base", "3D Render", "", 7.5, 30, -1, 1024, 1024],
75
  ["Beautiful mountain landscape with a river, golden hour", "SDXL Base", "Oil Painting", "", 7.5, 30, -1, 1024, 1024],
76
- ["A magical forest with glowing mushrooms", "FLUX.1-schnell", "Fantasy", "", 7.5, 30, -1, 1024, 1024],
77
- ["Portrait of a wise old wizard", "FLUX.1-dev", "Digital Art", "", 7.5, 30, -1, 1024, 1024],
78
  ]
79
 
80
 
@@ -85,7 +86,7 @@ with gr.Blocks(
85
  theme=gr.themes.Soft(),
86
  css=".generate-btn { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important; }"
87
  ) as demo:
88
- gr.Markdown("# 🎨 Text-to-Image Generator\n\nGenerate images from text using SDXL, FLUX, and Stable Diffusion.")
89
 
90
  with gr.Row():
91
  with gr.Column(scale=3):
@@ -140,7 +141,7 @@ with gr.Blocks(
140
  outputs=[output_image, info_text],
141
  )
142
 
143
- gr.Markdown("---\n**Models:** SDXL (Stability AI) | FLUX (Black Forest Labs) | SD v1.5")
144
 
145
  if __name__ == "__main__":
146
  demo.launch()
 
5
 
6
  HF_TOKEN = os.environ.get("HF_TOKEN")
7
 
8
+ client = InferenceClient(provider="fal-ai", api_key=HF_TOKEN)
 
9
 
10
  MODELS = {
11
+ "SDXL Base": {
12
+ "model_id": "stabilityai/stable-diffusion-xl-base-1.0",
13
+ "max_steps": 50,
14
+ "recommended_steps": 30,
15
+ },
16
+ "FLUX.1-schnell (4 steps)": {
17
+ "model_id": "black-forest-labs/FLUX.1-schnell",
18
+ "max_steps": 4,
19
+ "recommended_steps": 4,
20
+ },
21
+ "FLUX.1-dev (28 steps)": {
22
+ "model_id": "black-forest-labs/FLUX.1-dev",
23
+ "max_steps": 28,
24
+ "recommended_steps": 28,
25
+ },
26
  }
27
 
28
  DEFAULT_MODEL = "SDXL Base"
 
50
  seed = random.randint(0, 2**32 - 1)
51
  model_info = MODELS.get(model_name, MODELS[DEFAULT_MODEL])
52
  model_id = model_info["model_id"]
53
+ max_steps = model_info["max_steps"]
54
+
55
+ # Clamp inference steps to model's max supported
56
+ steps = min(num_inference_steps, max_steps)
57
 
58
  try:
59
  image = client.text_to_image(
60
  prompt=full_prompt, model=model_id,
61
  negative_prompt=negative_prompt if negative_prompt.strip() else None,
62
+ guidance_scale=guidance_scale, num_inference_steps=steps,
63
  width=width, height=height, seed=seed,
64
  )
65
+ return image, f"Seed: {seed} | Model: {model_name} | Steps: {steps}"
66
  except Exception as e:
67
+ raise gr.Error(f"Generation failed: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
 
70
  EXAMPLES = [
71
  ["A majestic dragon flying over a crystal lake at sunset, epic fantasy art", "SDXL Base", "Fantasy", "", 7.5, 30, -1, 1024, 1024],
72
  ["A cute robot exploring a colorful garden", "SDXL Base", "Anime", "", 7.5, 30, -1, 1024, 1024],
73
+ ["An astronaut riding a horse on Mars", "FLUX.1-schnell (4 steps)", "Photorealistic", "", 7.5, 4, -1, 1024, 1024],
74
  ["A cozy coffee shop interior with rain outside", "SDXL Base", "Digital Art", "", 7.5, 30, -1, 1024, 1024],
75
  ["A futuristic city skyline at night", "SDXL Base", "3D Render", "", 7.5, 30, -1, 1024, 1024],
76
  ["Beautiful mountain landscape with a river, golden hour", "SDXL Base", "Oil Painting", "", 7.5, 30, -1, 1024, 1024],
77
+ ["A magical forest with glowing mushrooms", "FLUX.1-schnell (4 steps)", "Fantasy", "", 7.5, 4, -1, 1024, 1024],
78
+ ["Portrait of a wise old wizard", "FLUX.1-dev (28 steps)", "Digital Art", "", 7.5, 28, -1, 1024, 1024],
79
  ]
80
 
81
 
 
86
  theme=gr.themes.Soft(),
87
  css=".generate-btn { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important; }"
88
  ) as demo:
89
+ gr.Markdown("# 🎨 Text-to-Image Generator\n\nGenerate images from text using SDXL and FLUX via HuggingFace Inference API.")
90
 
91
  with gr.Row():
92
  with gr.Column(scale=3):
 
141
  outputs=[output_image, info_text],
142
  )
143
 
144
+ gr.Markdown("---\n**Models:** SDXL (Stability AI) | FLUX.1-schnell/dev (Black Forest Labs) via fal-ai")
145
 
146
  if __name__ == "__main__":
147
  demo.launch()