Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -159,46 +159,61 @@ def generate_negative_prompt(ad_data):
|
|
| 159 |
|
| 160 |
return ", ".join(base_negative)
|
| 161 |
|
| 162 |
-
def call_huggingface_api(prompt, negative_prompt, api_token):
|
| 163 |
-
"""Call Hugging Face Inference API for FLUX.1-schnell"""
|
| 164 |
-
|
| 165 |
-
|
|
|
|
| 166 |
headers = {
|
| 167 |
"Authorization": f"Bearer {api_token}",
|
| 168 |
"Content-Type": "application/json"
|
| 169 |
}
|
| 170 |
-
|
| 171 |
payload = {
|
| 172 |
"inputs": prompt,
|
| 173 |
"parameters": {
|
| 174 |
"negative_prompt": negative_prompt,
|
| 175 |
-
"num_inference_steps": 4,
|
| 176 |
-
"guidance_scale": 0.0,
|
| 177 |
-
"width":
|
| 178 |
-
"height":
|
| 179 |
}
|
| 180 |
}
|
| 181 |
-
|
| 182 |
try:
|
| 183 |
-
response = requests.post(API_URL, headers=headers, json=payload, timeout=
|
| 184 |
-
|
|
|
|
| 185 |
if response.status_code == 200:
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
elif response.status_code == 503:
|
| 189 |
-
return None, "Model is loading.
|
|
|
|
| 190 |
elif response.status_code == 401:
|
| 191 |
-
return None, "Invalid
|
|
|
|
| 192 |
elif response.status_code == 402:
|
| 193 |
-
return None, "
|
|
|
|
| 194 |
else:
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
return None, f"API Error ({response.status_code}): {error_msg}"
|
| 197 |
-
|
| 198 |
except requests.Timeout:
|
| 199 |
-
return None, "Request timed out.
|
|
|
|
| 200 |
except Exception as e:
|
| 201 |
-
return None, f"
|
|
|
|
| 202 |
|
| 203 |
def image_to_base64(image):
|
| 204 |
"""Convert PIL Image to base64 string"""
|
|
@@ -207,8 +222,8 @@ def image_to_base64(image):
|
|
| 207 |
return base64.b64encode(buffered.getvalue()).decode()
|
| 208 |
|
| 209 |
# Header
|
| 210 |
-
st.markdown('<h1 class="main-header">🎨
|
| 211 |
-
st.markdown('<p class="sub-header">Transform your concept into stunning advertisements
|
| 212 |
|
| 213 |
# Sidebar Configuration
|
| 214 |
with st.sidebar:
|
|
|
|
| 159 |
|
| 160 |
return ", ".join(base_negative)
|
| 161 |
|
| 162 |
+
def call_huggingface_api(prompt, negative_prompt, api_token, width=1024, height=1024):
|
| 163 |
+
"""Call Hugging Face Inference API (new Router endpoint) for FLUX.1-schnell"""
|
| 164 |
+
|
| 165 |
+
API_URL = "https://router.huggingface.co/hf-inference/models/black-forest-labs/FLUX.1-schnell"
|
| 166 |
+
|
| 167 |
headers = {
|
| 168 |
"Authorization": f"Bearer {api_token}",
|
| 169 |
"Content-Type": "application/json"
|
| 170 |
}
|
| 171 |
+
|
| 172 |
payload = {
|
| 173 |
"inputs": prompt,
|
| 174 |
"parameters": {
|
| 175 |
"negative_prompt": negative_prompt,
|
| 176 |
+
"num_inference_steps": 4,
|
| 177 |
+
"guidance_scale": 0.0,
|
| 178 |
+
"width": width,
|
| 179 |
+
"height": height
|
| 180 |
}
|
| 181 |
}
|
| 182 |
+
|
| 183 |
try:
|
| 184 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=120)
|
| 185 |
+
|
| 186 |
+
# Handle image bytes
|
| 187 |
if response.status_code == 200:
|
| 188 |
+
try:
|
| 189 |
+
image = Image.open(io.BytesIO(response.content))
|
| 190 |
+
return image, None
|
| 191 |
+
except Exception:
|
| 192 |
+
return None, "Received invalid image data."
|
| 193 |
+
|
| 194 |
elif response.status_code == 503:
|
| 195 |
+
return None, "Model is loading (503). Wait 20–40 sec and retry."
|
| 196 |
+
|
| 197 |
elif response.status_code == 401:
|
| 198 |
+
return None, "Invalid or missing HF token."
|
| 199 |
+
|
| 200 |
elif response.status_code == 402:
|
| 201 |
+
return None, "Free-tier quota exceeded."
|
| 202 |
+
|
| 203 |
else:
|
| 204 |
+
try:
|
| 205 |
+
error_msg = response.json().get("error", "Unknown error")
|
| 206 |
+
except:
|
| 207 |
+
error_msg = response.text
|
| 208 |
+
|
| 209 |
return None, f"API Error ({response.status_code}): {error_msg}"
|
| 210 |
+
|
| 211 |
except requests.Timeout:
|
| 212 |
+
return None, "Request timed out. HF router is busy. Try again."
|
| 213 |
+
|
| 214 |
except Exception as e:
|
| 215 |
+
return None, f"Unexpected error: {str(e)}"
|
| 216 |
+
|
| 217 |
|
| 218 |
def image_to_base64(image):
|
| 219 |
"""Convert PIL Image to base64 string"""
|
|
|
|
| 222 |
return base64.b64encode(buffered.getvalue()).decode()
|
| 223 |
|
| 224 |
# Header
|
| 225 |
+
st.markdown('<h1 class="main-header">🎨 Addmaker</h1>', unsafe_allow_html=True)
|
| 226 |
+
st.markdown('<p class="sub-header">Transform your concept into stunning advertisements </p>', unsafe_allow_html=True)
|
| 227 |
|
| 228 |
# Sidebar Configuration
|
| 229 |
with st.sidebar:
|