Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -71,6 +71,23 @@ def updates_freeze(disable: bool):
|
|
| 71 |
upd, # hashtags textbox
|
| 72 |
upd] # launch_checkbox
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
def enforce_limit(request: gr.Request):
|
| 75 |
ip = request.client.host if request and request.client else "unknown"
|
| 76 |
if ENABLE_IP_LIMIT:
|
|
@@ -127,6 +144,7 @@ def run_agent(request: gr.Request,
|
|
| 127 |
do_budget_roi):
|
| 128 |
|
| 129 |
ip, allowed, msg, counter = enforce_limit(request)
|
|
|
|
| 130 |
if not allowed:
|
| 131 |
return (
|
| 132 |
"⚠️ Limit reached. No more responses.",
|
|
@@ -134,7 +152,7 @@ def run_agent(request: gr.Request,
|
|
| 134 |
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 135 |
*updates_freeze(disable=True)
|
| 136 |
)
|
| 137 |
-
|
| 138 |
if not product_name or not product_name.strip():
|
| 139 |
return ("Please enter a valid product or campaign name.", counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 140 |
|
|
@@ -166,13 +184,14 @@ def run_agent(request: gr.Request,
|
|
| 166 |
except Exception as e:
|
| 167 |
response = f"Error calling API: {e}"
|
| 168 |
|
| 169 |
-
return (response, counter, gr.update(visible=False), *
|
| 170 |
|
| 171 |
# -------------------------------
|
| 172 |
# Hashtag Generator
|
| 173 |
# -------------------------------
|
| 174 |
def generate_hashtags(request: gr.Request, product_name_val):
|
| 175 |
ip, allowed, msg, counter = enforce_limit(request)
|
|
|
|
| 176 |
if not allowed:
|
| 177 |
return (
|
| 178 |
"⚠️ Limit reached. No more hashtags.",
|
|
@@ -182,7 +201,7 @@ def generate_hashtags(request: gr.Request, product_name_val):
|
|
| 182 |
)
|
| 183 |
|
| 184 |
if not product_name_val or not product_name_val.strip():
|
| 185 |
-
return ("Please enter a product or campaign name first.", counter, gr.update(visible=False), *
|
| 186 |
|
| 187 |
prompt = f"Generate relevant social media hashtags for the product or campaign named '{product_name_val}'. List hashtags separated by commas."
|
| 188 |
try:
|
|
@@ -190,13 +209,14 @@ def generate_hashtags(request: gr.Request, product_name_val):
|
|
| 190 |
except Exception as e:
|
| 191 |
tags = f"Error generating hashtags: {e}"
|
| 192 |
|
| 193 |
-
return (tags, counter, gr.update(visible=False), *
|
| 194 |
|
| 195 |
# -------------------------------
|
| 196 |
# Social Media Launch
|
| 197 |
# -------------------------------
|
| 198 |
def social_channel_launch(request: gr.Request, channel, hashtags, checkbox_value):
|
| 199 |
ip, allowed, msg, counter = enforce_limit(request)
|
|
|
|
| 200 |
if not allowed:
|
| 201 |
return (
|
| 202 |
"⚠️ Limit reached. Launch blocked.",
|
|
@@ -206,9 +226,9 @@ def social_channel_launch(request: gr.Request, channel, hashtags, checkbox_value
|
|
| 206 |
)
|
| 207 |
|
| 208 |
if checkbox_value:
|
| 209 |
-
return (f"✅ Launched campaign on {channel} with hashtags: {hashtags}", counter, gr.update(visible=False), *
|
| 210 |
else:
|
| 211 |
-
return ("", counter, gr.update(visible=False), *
|
| 212 |
|
| 213 |
# -------------------------------
|
| 214 |
# UI
|
|
@@ -280,15 +300,37 @@ with gr.Blocks() as demo:
|
|
| 280 |
do_budget_roi],
|
| 281 |
outputs=run_btn)
|
| 282 |
|
|
|
|
| 283 |
run_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
fn=run_agent,
|
| 285 |
-
inputs=[
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
| 292 |
outputs=[
|
| 293 |
output_box,
|
| 294 |
counter_box,
|
|
@@ -310,8 +352,26 @@ with gr.Blocks() as demo:
|
|
| 310 |
)
|
| 311 |
|
| 312 |
generate_hashtags_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
fn=generate_hashtags,
|
| 314 |
-
inputs=[product_name],
|
| 315 |
outputs=[
|
| 316 |
hashtags,
|
| 317 |
counter_box,
|
|
@@ -332,10 +392,27 @@ with gr.Blocks() as demo:
|
|
| 332 |
],
|
| 333 |
)
|
| 334 |
|
| 335 |
-
# 🟢 MODIFIED SECTION: Ensure all UI components are updated correctly after a launch
|
| 336 |
launch_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
fn=social_channel_launch,
|
| 338 |
-
inputs=[social_channels, hashtags, launch_checkbox],
|
| 339 |
outputs=[
|
| 340 |
launch_output,
|
| 341 |
counter_box,
|
|
|
|
| 71 |
upd, # hashtags textbox
|
| 72 |
upd] # launch_checkbox
|
| 73 |
|
| 74 |
+
def updates_unfreeze():
|
| 75 |
+
"""Unfreezes all relevant interactive components."""
|
| 76 |
+
return [
|
| 77 |
+
gr.update(interactive=True),
|
| 78 |
+
gr.update(interactive=True),
|
| 79 |
+
gr.update(interactive=True),
|
| 80 |
+
gr.update(interactive=True),
|
| 81 |
+
gr.update(interactive=True),
|
| 82 |
+
gr.update(interactive=True),
|
| 83 |
+
gr.update(interactive=True),
|
| 84 |
+
gr.update(interactive=True),
|
| 85 |
+
gr.update(interactive=True),
|
| 86 |
+
gr.update(interactive=True),
|
| 87 |
+
gr.update(interactive=True),
|
| 88 |
+
gr.update(interactive=True)
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
def enforce_limit(request: gr.Request):
|
| 92 |
ip = request.client.host if request and request.client else "unknown"
|
| 93 |
if ENABLE_IP_LIMIT:
|
|
|
|
| 144 |
do_budget_roi):
|
| 145 |
|
| 146 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 147 |
+
|
| 148 |
if not allowed:
|
| 149 |
return (
|
| 150 |
"⚠️ Limit reached. No more responses.",
|
|
|
|
| 152 |
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 153 |
*updates_freeze(disable=True)
|
| 154 |
)
|
| 155 |
+
|
| 156 |
if not product_name or not product_name.strip():
|
| 157 |
return ("Please enter a valid product or campaign name.", counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 158 |
|
|
|
|
| 184 |
except Exception as e:
|
| 185 |
response = f"Error calling API: {e}"
|
| 186 |
|
| 187 |
+
return (response, counter, gr.update(visible=False), *updates_unfreeze()) # 🟢 MODIFIED LINE
|
| 188 |
|
| 189 |
# -------------------------------
|
| 190 |
# Hashtag Generator
|
| 191 |
# -------------------------------
|
| 192 |
def generate_hashtags(request: gr.Request, product_name_val):
|
| 193 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 194 |
+
|
| 195 |
if not allowed:
|
| 196 |
return (
|
| 197 |
"⚠️ Limit reached. No more hashtags.",
|
|
|
|
| 201 |
)
|
| 202 |
|
| 203 |
if not product_name_val or not product_name_val.strip():
|
| 204 |
+
return ("Please enter a product or campaign name first.", counter, gr.update(visible=False), *updates_unfreeze()) # 🟢 MODIFIED LINE
|
| 205 |
|
| 206 |
prompt = f"Generate relevant social media hashtags for the product or campaign named '{product_name_val}'. List hashtags separated by commas."
|
| 207 |
try:
|
|
|
|
| 209 |
except Exception as e:
|
| 210 |
tags = f"Error generating hashtags: {e}"
|
| 211 |
|
| 212 |
+
return (tags, counter, gr.update(visible=False), *updates_unfreeze()) # 🟢 MODIFIED LINE
|
| 213 |
|
| 214 |
# -------------------------------
|
| 215 |
# Social Media Launch
|
| 216 |
# -------------------------------
|
| 217 |
def social_channel_launch(request: gr.Request, channel, hashtags, checkbox_value):
|
| 218 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 219 |
+
|
| 220 |
if not allowed:
|
| 221 |
return (
|
| 222 |
"⚠️ Limit reached. Launch blocked.",
|
|
|
|
| 226 |
)
|
| 227 |
|
| 228 |
if checkbox_value:
|
| 229 |
+
return (f"✅ Launched campaign on {channel} with hashtags: {hashtags}", counter, gr.update(visible=False), *updates_unfreeze()) # 🟢 MODIFIED LINE
|
| 230 |
else:
|
| 231 |
+
return ("", counter, gr.update(visible=False), *updates_unfreeze()) # 🟢 MODIFIED LINE
|
| 232 |
|
| 233 |
# -------------------------------
|
| 234 |
# UI
|
|
|
|
| 300 |
do_budget_roi],
|
| 301 |
outputs=run_btn)
|
| 302 |
|
| 303 |
+
# 🟢 MODIFIED SECTION: Add .then() to re-enable components
|
| 304 |
run_btn.click(
|
| 305 |
+
fn=lambda: updates_freeze(True), # Immediately disable all inputs
|
| 306 |
+
inputs=None,
|
| 307 |
+
outputs=[
|
| 308 |
+
run_btn,
|
| 309 |
+
generate_hashtags_btn,
|
| 310 |
+
launch_btn,
|
| 311 |
+
product_name,
|
| 312 |
+
do_competitors,
|
| 313 |
+
do_message,
|
| 314 |
+
do_competitor_analysis,
|
| 315 |
+
do_launch_time,
|
| 316 |
+
do_seo,
|
| 317 |
+
do_budget_roi,
|
| 318 |
+
social_channels,
|
| 319 |
+
hashtags,
|
| 320 |
+
launch_checkbox,
|
| 321 |
+
]
|
| 322 |
+
).then(
|
| 323 |
fn=run_agent,
|
| 324 |
+
inputs=[
|
| 325 |
+
gr.Request(),
|
| 326 |
+
product_name,
|
| 327 |
+
do_competitors,
|
| 328 |
+
do_message,
|
| 329 |
+
do_competitor_analysis,
|
| 330 |
+
do_launch_time,
|
| 331 |
+
do_seo,
|
| 332 |
+
do_budget_roi
|
| 333 |
+
],
|
| 334 |
outputs=[
|
| 335 |
output_box,
|
| 336 |
counter_box,
|
|
|
|
| 352 |
)
|
| 353 |
|
| 354 |
generate_hashtags_btn.click(
|
| 355 |
+
fn=lambda: updates_freeze(True), # Immediately disable all inputs
|
| 356 |
+
inputs=None,
|
| 357 |
+
outputs=[
|
| 358 |
+
run_btn,
|
| 359 |
+
generate_hashtags_btn,
|
| 360 |
+
launch_btn,
|
| 361 |
+
product_name,
|
| 362 |
+
do_competitors,
|
| 363 |
+
do_message,
|
| 364 |
+
do_competitor_analysis,
|
| 365 |
+
do_launch_time,
|
| 366 |
+
do_seo,
|
| 367 |
+
do_budget_roi,
|
| 368 |
+
social_channels,
|
| 369 |
+
hashtags,
|
| 370 |
+
launch_checkbox,
|
| 371 |
+
]
|
| 372 |
+
).then(
|
| 373 |
fn=generate_hashtags,
|
| 374 |
+
inputs=[gr.Request(), product_name],
|
| 375 |
outputs=[
|
| 376 |
hashtags,
|
| 377 |
counter_box,
|
|
|
|
| 392 |
],
|
| 393 |
)
|
| 394 |
|
|
|
|
| 395 |
launch_btn.click(
|
| 396 |
+
fn=lambda: updates_freeze(True), # Immediately disable all inputs
|
| 397 |
+
inputs=None,
|
| 398 |
+
outputs=[
|
| 399 |
+
run_btn,
|
| 400 |
+
generate_hashtags_btn,
|
| 401 |
+
launch_btn,
|
| 402 |
+
product_name,
|
| 403 |
+
do_competitors,
|
| 404 |
+
do_message,
|
| 405 |
+
do_competitor_analysis,
|
| 406 |
+
do_launch_time,
|
| 407 |
+
do_seo,
|
| 408 |
+
do_budget_roi,
|
| 409 |
+
social_channels,
|
| 410 |
+
hashtags,
|
| 411 |
+
launch_checkbox,
|
| 412 |
+
]
|
| 413 |
+
).then(
|
| 414 |
fn=social_channel_launch,
|
| 415 |
+
inputs=[gr.Request(), social_channels, hashtags, launch_checkbox],
|
| 416 |
outputs=[
|
| 417 |
launch_output,
|
| 418 |
counter_box,
|