Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -80,8 +80,10 @@ def enforce_limit(request: gr.Request):
|
|
| 80 |
used = ip_usage.get(ip, 0)
|
| 81 |
remaining = max(0, MAX_USES - used)
|
| 82 |
if used >= MAX_USES:
|
| 83 |
-
return ip, False, (
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
ip_usage[ip] = used + 1
|
| 86 |
save_usage(ip_usage)
|
| 87 |
remaining = MAX_USES - ip_usage[ip]
|
|
@@ -102,10 +104,12 @@ def run_agent(request: gr.Request,
|
|
| 102 |
|
| 103 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 104 |
if not allowed:
|
| 105 |
-
return (msg, counter,
|
|
|
|
|
|
|
| 106 |
|
| 107 |
if not product_name or not product_name.strip():
|
| 108 |
-
return ("Please enter a valid product or campaign name.", counter, *updates_freeze(disable=False))
|
| 109 |
|
| 110 |
prompt = f"Create a detailed marketing plan for '{product_name}'.\nOnly include these sections:\n"
|
| 111 |
sections = []
|
|
@@ -123,7 +127,7 @@ def run_agent(request: gr.Request,
|
|
| 123 |
sections.append("Budget and ROI")
|
| 124 |
|
| 125 |
if not sections:
|
| 126 |
-
return ("Please select at least one option to generate the marketing plan.", counter, *updates_freeze(disable=False))
|
| 127 |
|
| 128 |
for s in sections:
|
| 129 |
prompt += f"- {s}\n"
|
|
@@ -135,7 +139,7 @@ def run_agent(request: gr.Request,
|
|
| 135 |
except Exception as e:
|
| 136 |
response = f"Error calling API: {e}"
|
| 137 |
|
| 138 |
-
return (response, counter, *updates_freeze(disable=False))
|
| 139 |
|
| 140 |
# -------------------------------
|
| 141 |
# Hashtag Generator
|
|
@@ -143,10 +147,12 @@ def run_agent(request: gr.Request,
|
|
| 143 |
def generate_hashtags(request: gr.Request, product_name_val):
|
| 144 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 145 |
if not allowed:
|
| 146 |
-
return (msg, counter,
|
|
|
|
|
|
|
| 147 |
|
| 148 |
if not product_name_val or not product_name_val.strip():
|
| 149 |
-
return ("Please enter a product or campaign name first.", counter, *updates_freeze(disable=False))
|
| 150 |
|
| 151 |
prompt = f"Generate relevant social media hashtags for the product or campaign named '{product_name_val}'. List hashtags separated by commas."
|
| 152 |
try:
|
|
@@ -154,7 +160,7 @@ def generate_hashtags(request: gr.Request, product_name_val):
|
|
| 154 |
except Exception as e:
|
| 155 |
tags = f"Error generating hashtags: {e}"
|
| 156 |
|
| 157 |
-
return (tags, counter, *updates_freeze(disable=False))
|
| 158 |
|
| 159 |
# -------------------------------
|
| 160 |
# Social Media Launch
|
|
@@ -162,17 +168,22 @@ def generate_hashtags(request: gr.Request, product_name_val):
|
|
| 162 |
def social_channel_launch(request: gr.Request, channel, hashtags, checkbox_value):
|
| 163 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 164 |
if not allowed:
|
| 165 |
-
return (msg, counter,
|
|
|
|
|
|
|
| 166 |
|
| 167 |
if checkbox_value:
|
| 168 |
-
return (f"✅ Launched campaign on {channel} with hashtags: {hashtags}", counter, *updates_freeze(disable=False))
|
| 169 |
else:
|
| 170 |
-
return ("", counter, *updates_freeze(disable=False))
|
| 171 |
|
| 172 |
# -------------------------------
|
| 173 |
# UI
|
| 174 |
# -------------------------------
|
| 175 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
| 176 |
product_name = gr.Textbox(label="Product or Campaign Name", placeholder="Enter product or campaign name here...")
|
| 177 |
|
| 178 |
with gr.Row():
|
|
@@ -247,6 +258,7 @@ with gr.Blocks() as demo:
|
|
| 247 |
outputs=[
|
| 248 |
output_box,
|
| 249 |
counter_box,
|
|
|
|
| 250 |
run_btn,
|
| 251 |
generate_hashtags_btn,
|
| 252 |
launch_btn,
|
|
@@ -269,6 +281,7 @@ with gr.Blocks() as demo:
|
|
| 269 |
outputs=[
|
| 270 |
hashtags,
|
| 271 |
counter_box,
|
|
|
|
| 272 |
run_btn,
|
| 273 |
generate_hashtags_btn,
|
| 274 |
launch_btn,
|
|
@@ -291,6 +304,7 @@ with gr.Blocks() as demo:
|
|
| 291 |
outputs=[
|
| 292 |
launch_output,
|
| 293 |
counter_box,
|
|
|
|
| 294 |
run_btn,
|
| 295 |
generate_hashtags_btn,
|
| 296 |
launch_btn,
|
|
|
|
| 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. "
|
| 85 |
+
"All controls are disabled. Contact the admin."
|
| 86 |
+
), f"Remaining attempts: 0/{MAX_USES}"
|
| 87 |
ip_usage[ip] = used + 1
|
| 88 |
save_usage(ip_usage)
|
| 89 |
remaining = MAX_USES - ip_usage[ip]
|
|
|
|
| 104 |
|
| 105 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 106 |
if not allowed:
|
| 107 |
+
return (msg, counter,
|
| 108 |
+
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 109 |
+
*updates_freeze(disable=True))
|
| 110 |
|
| 111 |
if not product_name or not product_name.strip():
|
| 112 |
+
return ("Please enter a valid product or campaign name.", counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 113 |
|
| 114 |
prompt = f"Create a detailed marketing plan for '{product_name}'.\nOnly include these sections:\n"
|
| 115 |
sections = []
|
|
|
|
| 127 |
sections.append("Budget and ROI")
|
| 128 |
|
| 129 |
if not sections:
|
| 130 |
+
return ("Please select at least one option to generate the marketing plan.", counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 131 |
|
| 132 |
for s in sections:
|
| 133 |
prompt += f"- {s}\n"
|
|
|
|
| 139 |
except Exception as e:
|
| 140 |
response = f"Error calling API: {e}"
|
| 141 |
|
| 142 |
+
return (response, counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 143 |
|
| 144 |
# -------------------------------
|
| 145 |
# Hashtag Generator
|
|
|
|
| 147 |
def generate_hashtags(request: gr.Request, product_name_val):
|
| 148 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 149 |
if not allowed:
|
| 150 |
+
return (msg, counter,
|
| 151 |
+
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 152 |
+
*updates_freeze(disable=True))
|
| 153 |
|
| 154 |
if not product_name_val or not product_name_val.strip():
|
| 155 |
+
return ("Please enter a product or campaign name first.", counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 156 |
|
| 157 |
prompt = f"Generate relevant social media hashtags for the product or campaign named '{product_name_val}'. List hashtags separated by commas."
|
| 158 |
try:
|
|
|
|
| 160 |
except Exception as e:
|
| 161 |
tags = f"Error generating hashtags: {e}"
|
| 162 |
|
| 163 |
+
return (tags, counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 164 |
|
| 165 |
# -------------------------------
|
| 166 |
# Social Media Launch
|
|
|
|
| 168 |
def social_channel_launch(request: gr.Request, channel, hashtags, checkbox_value):
|
| 169 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 170 |
if not allowed:
|
| 171 |
+
return (msg, counter,
|
| 172 |
+
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 173 |
+
*updates_freeze(disable=True))
|
| 174 |
|
| 175 |
if checkbox_value:
|
| 176 |
+
return (f"✅ Launched campaign on {channel} with hashtags: {hashtags}", counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 177 |
else:
|
| 178 |
+
return ("", counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 179 |
|
| 180 |
# -------------------------------
|
| 181 |
# UI
|
| 182 |
# -------------------------------
|
| 183 |
with gr.Blocks() as demo:
|
| 184 |
+
# Persistent warning banner
|
| 185 |
+
limit_banner = gr.Markdown("", visible=False)
|
| 186 |
+
|
| 187 |
product_name = gr.Textbox(label="Product or Campaign Name", placeholder="Enter product or campaign name here...")
|
| 188 |
|
| 189 |
with gr.Row():
|
|
|
|
| 258 |
outputs=[
|
| 259 |
output_box,
|
| 260 |
counter_box,
|
| 261 |
+
limit_banner,
|
| 262 |
run_btn,
|
| 263 |
generate_hashtags_btn,
|
| 264 |
launch_btn,
|
|
|
|
| 281 |
outputs=[
|
| 282 |
hashtags,
|
| 283 |
counter_box,
|
| 284 |
+
limit_banner,
|
| 285 |
run_btn,
|
| 286 |
generate_hashtags_btn,
|
| 287 |
launch_btn,
|
|
|
|
| 304 |
outputs=[
|
| 305 |
launch_output,
|
| 306 |
counter_box,
|
| 307 |
+
limit_banner,
|
| 308 |
run_btn,
|
| 309 |
generate_hashtags_btn,
|
| 310 |
launch_btn,
|