Maryam-34 commited on
Commit
85efd98
·
verified ·
1 Parent(s): da1b198

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -10,13 +10,14 @@ load_dotenv()
10
 
11
  # Get API key from environment variable
12
  # Set your API key in Hugging Face Space Settings > Variables and Secrets
13
- # Name it: API_KEY
14
  API_KEY = os.getenv("API_KEY")
15
- API_URL = os.getenv("API_URL", "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0")
 
 
16
 
17
  def generate_image(prompt, negative_prompt="", width=1024, height=1024, guidance_scale=7.5, num_inference_steps=50):
18
  """
19
- Generate image using Hugging Face Inference API or similar API
20
  """
21
  if not API_KEY:
22
  return None, "Error: API key not configured. Please set the API_KEY in your Space secrets."
@@ -29,10 +30,10 @@ def generate_image(prompt, negative_prompt="", width=1024, height=1024, guidance
29
  "Content-Type": "application/json"
30
  }
31
 
 
32
  payload = {
33
  "inputs": prompt,
34
  "parameters": {
35
- "negative_prompt": negative_prompt,
36
  "width": width,
37
  "height": height,
38
  "guidance_scale": guidance_scale,
@@ -40,6 +41,10 @@ def generate_image(prompt, negative_prompt="", width=1024, height=1024, guidance
40
  }
41
  }
42
 
 
 
 
 
43
  try:
44
  response = requests.post(API_URL, headers=headers, json=payload, timeout=300)
45
 
@@ -79,14 +84,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
79
  with gr.Row():
80
  width_slider = gr.Slider(
81
  minimum=512,
82
- maximum=2048,
83
  step=64,
84
  value=1024,
85
  label="Width"
86
  )
87
  height_slider = gr.Slider(
88
  minimum=512,
89
- maximum=2048,
90
  step=64,
91
  value=1024,
92
  label="Height"
@@ -101,11 +106,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
101
  )
102
 
103
  steps_slider = gr.Slider(
104
- minimum=20,
105
- maximum=100,
106
  step=1,
107
- value=50,
108
- label="Inference Steps"
109
  )
110
 
111
  generate_btn = gr.Button("✨ Generate Image", variant="primary")
@@ -119,6 +124,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
119
  - Be specific and descriptive in your prompts
120
  - Include style keywords: "digital art", "photorealistic", "oil painting"
121
  - Mention lighting: "cinematic lighting", "golden hour", "studio lighting"
 
122
  """)
123
 
124
  # Event handlers
@@ -138,9 +144,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
138
  # Examples
139
  gr.Examples(
140
  examples=[
141
- ["A futuristic city at night with neon lights and flying cars, digital art", "", 1024, 1024, 7.5, 50],
142
- ["Portrait of a wise wizard with long beard, magical atmosphere, oil painting", "blurry, ugly", 1024, 1024, 8, 50],
143
- ["Cute robot reading a book in a cozy library, pixar style", "", 1024, 1024, 7, 50],
144
  ],
145
  inputs=[prompt_input, negative_prompt_input, width_slider, height_slider, guidance_slider, steps_slider],
146
  label="Example Prompts"
 
10
 
11
  # Get API key from environment variable
12
  # Set your API key in Hugging Face Space Settings > Variables and Secrets
 
13
  API_KEY = os.getenv("API_KEY")
14
+
15
+ # NEW CORRECT ENDPOINT (updated from api-inference.huggingface.co)
16
+ API_URL = os.getenv("API_URL", "https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell")
17
 
18
  def generate_image(prompt, negative_prompt="", width=1024, height=1024, guidance_scale=7.5, num_inference_steps=50):
19
  """
20
+ Generate image using Hugging Face Inference API (Router endpoint)
21
  """
22
  if not API_KEY:
23
  return None, "Error: API key not configured. Please set the API_KEY in your Space secrets."
 
30
  "Content-Type": "application/json"
31
  }
32
 
33
+ # FLUX.1-schnell uses different parameters - adjust payload accordingly
34
  payload = {
35
  "inputs": prompt,
36
  "parameters": {
 
37
  "width": width,
38
  "height": height,
39
  "guidance_scale": guidance_scale,
 
41
  }
42
  }
43
 
44
+ # Only add negative_prompt if provided and model supports it
45
+ if negative_prompt:
46
+ payload["parameters"]["negative_prompt"] = negative_prompt
47
+
48
  try:
49
  response = requests.post(API_URL, headers=headers, json=payload, timeout=300)
50
 
 
84
  with gr.Row():
85
  width_slider = gr.Slider(
86
  minimum=512,
87
+ maximum=1024,
88
  step=64,
89
  value=1024,
90
  label="Width"
91
  )
92
  height_slider = gr.Slider(
93
  minimum=512,
94
+ maximum=1024,
95
  step=64,
96
  value=1024,
97
  label="Height"
 
106
  )
107
 
108
  steps_slider = gr.Slider(
109
+ minimum=1,
110
+ maximum=50,
111
  step=1,
112
+ value=4,
113
+ label="Inference Steps (FLUX works well with 4-20)"
114
  )
115
 
116
  generate_btn = gr.Button("✨ Generate Image", variant="primary")
 
124
  - Be specific and descriptive in your prompts
125
  - Include style keywords: "digital art", "photorealistic", "oil painting"
126
  - Mention lighting: "cinematic lighting", "golden hour", "studio lighting"
127
+ - FLUX models work great with just 4-8 inference steps!
128
  """)
129
 
130
  # Event handlers
 
144
  # Examples
145
  gr.Examples(
146
  examples=[
147
+ ["A futuristic city at night with neon lights and flying cars, digital art", "", 1024, 1024, 7.5, 4],
148
+ ["Portrait of a wise wizard with long beard, magical atmosphere, oil painting", "blurry, ugly", 1024, 1024, 8, 4],
149
+ ["Cute robot reading a book in a cozy library, pixar style", "", 1024, 1024, 7, 4],
150
  ],
151
  inputs=[prompt_input, negative_prompt_input, width_slider, height_slider, guidance_slider, steps_slider],
152
  label="Example Prompts"