abhidgp1978 commited on
Commit
6dbd1cf
·
verified ·
1 Parent(s): ae4702e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -5
app.py CHANGED
@@ -59,7 +59,7 @@ def groq_api_chat(prompt, model="llama3-8b-8192", max_tokens=800):
59
  return data["choices"][0]["message"]["content"].strip()
60
 
61
  # -------------------------------
62
- # Helper: return updates to freeze/unfreeze all inputs
63
  # -------------------------------
64
  def updates_freeze(disable: bool):
65
  upd = gr.update(interactive=False) if disable else gr.update()
@@ -71,14 +71,10 @@ def updates_freeze(disable: bool):
71
  upd, # hashtags textbox
72
  upd] # launch_checkbox
73
 
74
- # -------------------------------
75
- # Shared IP enforcement
76
- # -------------------------------
77
  def enforce_limit(request: gr.Request):
78
  ip = request.client.host if request and request.client else "unknown"
79
  if ENABLE_IP_LIMIT:
80
  used = ip_usage.get(ip, 0)
81
- remaining = max(0, MAX_USES - used)
82
  if used >= MAX_USES:
83
  return ip, False, (
84
  f"⚠️ This IP has reached the maximum limit of {MAX_USES} uses. "
@@ -90,6 +86,27 @@ def enforce_limit(request: gr.Request):
90
  return ip, True, None, f"Remaining attempts: {remaining}/{MAX_USES}"
91
  return ip, True, None, "Unlimited attempts"
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  # -------------------------------
94
  # Main Agent (Generate)
95
  # -------------------------------
@@ -222,6 +239,7 @@ with gr.Blocks() as demo:
222
 
223
  launch_output = gr.Markdown()
224
 
 
225
  def enable_generate_button(product_name_val, *checkbox_values):
226
  product_entered = bool(product_name_val and product_name_val.strip())
227
  any_checked = any(checkbox_values)
@@ -321,4 +339,27 @@ with gr.Blocks() as demo:
321
  ],
322
  )
323
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324
  demo.launch()
 
59
  return data["choices"][0]["message"]["content"].strip()
60
 
61
  # -------------------------------
62
+ # Helpers
63
  # -------------------------------
64
  def updates_freeze(disable: bool):
65
  upd = gr.update(interactive=False) if disable else gr.update()
 
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:
77
  used = ip_usage.get(ip, 0)
 
78
  if used >= MAX_USES:
79
  return ip, False, (
80
  f"⚠️ This IP has reached the maximum limit of {MAX_USES} uses. "
 
86
  return ip, True, None, f"Remaining attempts: {remaining}/{MAX_USES}"
87
  return ip, True, None, "Unlimited attempts"
88
 
89
+ # -------------------------------
90
+ # Check IP status on load
91
+ # -------------------------------
92
+ def check_ip_on_load(request: gr.Request):
93
+ ip = request.client.host if request and request.client else "unknown"
94
+ used = ip_usage.get(ip, 0)
95
+ if ENABLE_IP_LIMIT and used >= MAX_USES:
96
+ msg = f"⚠️ This IP has reached the maximum limit of {MAX_USES} uses. All controls are disabled. Contact the admin."
97
+ return (
98
+ f"Remaining attempts: 0/{MAX_USES}",
99
+ gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
100
+ *updates_freeze(disable=True)
101
+ )
102
+ else:
103
+ remaining = MAX_USES - used if ENABLE_IP_LIMIT else "∞"
104
+ return (
105
+ f"Remaining attempts: {remaining}/{MAX_USES}" if ENABLE_IP_LIMIT else "Unlimited attempts",
106
+ gr.update(visible=False),
107
+ *updates_freeze(disable=False)
108
+ )
109
+
110
  # -------------------------------
111
  # Main Agent (Generate)
112
  # -------------------------------
 
239
 
240
  launch_output = gr.Markdown()
241
 
242
+ # Enable Generate dynamically
243
  def enable_generate_button(product_name_val, *checkbox_values):
244
  product_entered = bool(product_name_val and product_name_val.strip())
245
  any_checked = any(checkbox_values)
 
339
  ],
340
  )
341
 
342
+ # 🔴 Check IP immediately on load
343
+ demo.load(
344
+ fn=check_ip_on_load,
345
+ inputs=None,
346
+ outputs=[
347
+ counter_box,
348
+ limit_banner,
349
+ run_btn,
350
+ generate_hashtags_btn,
351
+ launch_btn,
352
+ product_name,
353
+ do_competitors,
354
+ do_message,
355
+ do_competitor_analysis,
356
+ do_launch_time,
357
+ do_seo,
358
+ do_budget_roi,
359
+ social_channels,
360
+ hashtags,
361
+ launch_checkbox,
362
+ ],
363
+ )
364
+
365
  demo.launch()