Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -61,28 +61,31 @@ def groq_api_chat(prompt, model="llama3-8b-8192", max_tokens=800):
|
|
| 61 |
# -------------------------------
|
| 62 |
# Helpers
|
| 63 |
# -------------------------------
|
| 64 |
-
def
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
return [
|
| 67 |
-
gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False),
|
| 68 |
-
gr.update(interactive=False),
|
| 69 |
-
gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False),
|
| 70 |
-
gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False),
|
| 71 |
-
gr.update(interactive=False),
|
| 72 |
-
gr.update(interactive=False),
|
| 73 |
-
gr.update(interactive=False),
|
| 74 |
-
]
|
| 75 |
-
|
| 76 |
-
def get_unfrozen_components():
|
| 77 |
-
"""Returns a list of all interactive components for updates."""
|
| 78 |
-
return [
|
| 79 |
-
gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True),
|
| 80 |
gr.update(interactive=True),
|
| 81 |
-
gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True),
|
| 82 |
-
gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True),
|
| 83 |
gr.update(interactive=True),
|
| 84 |
gr.update(interactive=True),
|
| 85 |
gr.update(interactive=True),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
]
|
| 87 |
|
| 88 |
def enforce_limit(request: gr.Request):
|
|
@@ -115,14 +118,14 @@ def check_ip_on_load(request: gr.Request):
|
|
| 115 |
return (
|
| 116 |
f"Remaining attempts: 0/{MAX_USES}",
|
| 117 |
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 118 |
-
*
|
| 119 |
)
|
| 120 |
else:
|
| 121 |
remaining = MAX_USES - used if ENABLE_IP_LIMIT else "∞"
|
| 122 |
return (
|
| 123 |
f"Remaining attempts: {remaining}/{MAX_USES}" if ENABLE_IP_LIMIT else "Unlimited attempts",
|
| 124 |
gr.update(visible=False),
|
| 125 |
-
*
|
| 126 |
)
|
| 127 |
|
| 128 |
# -------------------------------
|
|
@@ -144,11 +147,11 @@ def run_agent(request: gr.Request,
|
|
| 144 |
"⚠️ Limit reached. No more responses.",
|
| 145 |
counter,
|
| 146 |
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 147 |
-
*
|
| 148 |
)
|
| 149 |
|
| 150 |
if not product_name or not product_name.strip():
|
| 151 |
-
return ("Please enter a valid product or campaign name.", counter, gr.update(visible=False), *
|
| 152 |
|
| 153 |
prompt = f"Create a detailed marketing plan for '{product_name}'.\nOnly include these sections:\n"
|
| 154 |
sections = []
|
|
@@ -166,7 +169,7 @@ def run_agent(request: gr.Request,
|
|
| 166 |
sections.append("Budget and ROI")
|
| 167 |
|
| 168 |
if not sections:
|
| 169 |
-
return ("Please select at least one option to generate the marketing plan.", counter, gr.update(visible=False), *
|
| 170 |
|
| 171 |
for s in sections:
|
| 172 |
prompt += f"- {s}\n"
|
|
@@ -178,7 +181,7 @@ def run_agent(request: gr.Request,
|
|
| 178 |
except Exception as e:
|
| 179 |
response = f"Error calling API: {e}"
|
| 180 |
|
| 181 |
-
return (response, counter, gr.update(visible=False), *
|
| 182 |
|
| 183 |
# -------------------------------
|
| 184 |
# Hashtag Generator
|
|
@@ -191,11 +194,11 @@ def generate_hashtags(request: gr.Request, product_name_val):
|
|
| 191 |
"⚠️ Limit reached. No more hashtags.",
|
| 192 |
counter,
|
| 193 |
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 194 |
-
*
|
| 195 |
)
|
| 196 |
|
| 197 |
if not product_name_val or not product_name_val.strip():
|
| 198 |
-
return ("Please enter a product or campaign name first.", counter, gr.update(visible=False), *
|
| 199 |
|
| 200 |
prompt = f"Generate relevant social media hashtags for the product or campaign named '{product_name_val}'. List hashtags separated by commas."
|
| 201 |
try:
|
|
@@ -203,7 +206,7 @@ def generate_hashtags(request: gr.Request, product_name_val):
|
|
| 203 |
except Exception as e:
|
| 204 |
tags = f"Error generating hashtags: {e}"
|
| 205 |
|
| 206 |
-
return (tags, counter, gr.update(visible=False), *
|
| 207 |
|
| 208 |
# -------------------------------
|
| 209 |
# Social Media Launch
|
|
@@ -216,13 +219,13 @@ def social_channel_launch(request: gr.Request, channel, hashtags, checkbox_value
|
|
| 216 |
"⚠️ Limit reached. Launch blocked.",
|
| 217 |
counter,
|
| 218 |
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 219 |
-
*
|
| 220 |
)
|
| 221 |
|
| 222 |
if checkbox_value:
|
| 223 |
-
return (f"✅ Launched campaign on {channel} with hashtags: {hashtags}", counter, gr.update(visible=False), *
|
| 224 |
else:
|
| 225 |
-
return ("", counter, gr.update(visible=False), *
|
| 226 |
|
| 227 |
# -------------------------------
|
| 228 |
# UI
|
|
@@ -297,7 +300,7 @@ with gr.Blocks() as demo:
|
|
| 297 |
run_btn.click(
|
| 298 |
fn=run_agent,
|
| 299 |
inputs=[
|
| 300 |
-
gr.Request(),
|
| 301 |
product_name,
|
| 302 |
do_competitors,
|
| 303 |
do_message,
|
|
@@ -372,7 +375,6 @@ with gr.Blocks() as demo:
|
|
| 372 |
],
|
| 373 |
)
|
| 374 |
|
| 375 |
-
# 🔴 Check IP immediately on load
|
| 376 |
demo.load(
|
| 377 |
fn=check_ip_on_load,
|
| 378 |
inputs=None,
|
|
|
|
| 61 |
# -------------------------------
|
| 62 |
# Helpers
|
| 63 |
# -------------------------------
|
| 64 |
+
def updates_freeze(disable: bool):
|
| 65 |
+
upd = gr.update(interactive=False) if disable else gr.update()
|
| 66 |
+
return [upd, upd, upd, # run_btn, generate_hashtags_btn, launch_btn
|
| 67 |
+
upd, # product_name
|
| 68 |
+
upd, upd, upd, # checkboxes row 1
|
| 69 |
+
upd, upd, upd, # checkboxes row 2
|
| 70 |
+
upd, # social_channels
|
| 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):
|
|
|
|
| 118 |
return (
|
| 119 |
f"Remaining attempts: 0/{MAX_USES}",
|
| 120 |
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 121 |
+
*updates_freeze(disable=True)
|
| 122 |
)
|
| 123 |
else:
|
| 124 |
remaining = MAX_USES - used if ENABLE_IP_LIMIT else "∞"
|
| 125 |
return (
|
| 126 |
f"Remaining attempts: {remaining}/{MAX_USES}" if ENABLE_IP_LIMIT else "Unlimited attempts",
|
| 127 |
gr.update(visible=False),
|
| 128 |
+
*updates_freeze(disable=False)
|
| 129 |
)
|
| 130 |
|
| 131 |
# -------------------------------
|
|
|
|
| 147 |
"⚠️ Limit reached. No more responses.",
|
| 148 |
counter,
|
| 149 |
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 150 |
+
*updates_freeze(disable=True)
|
| 151 |
)
|
| 152 |
|
| 153 |
if not product_name or not product_name.strip():
|
| 154 |
+
return ("Please enter a valid product or campaign name.", counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 155 |
|
| 156 |
prompt = f"Create a detailed marketing plan for '{product_name}'.\nOnly include these sections:\n"
|
| 157 |
sections = []
|
|
|
|
| 169 |
sections.append("Budget and ROI")
|
| 170 |
|
| 171 |
if not sections:
|
| 172 |
+
return ("Please select at least one option to generate the marketing plan.", counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 173 |
|
| 174 |
for s in sections:
|
| 175 |
prompt += f"- {s}\n"
|
|
|
|
| 181 |
except Exception as e:
|
| 182 |
response = f"Error calling API: {e}"
|
| 183 |
|
| 184 |
+
return (response, counter, gr.update(visible=False), *updates_unfreeze())
|
| 185 |
|
| 186 |
# -------------------------------
|
| 187 |
# Hashtag Generator
|
|
|
|
| 194 |
"⚠️ Limit reached. No more hashtags.",
|
| 195 |
counter,
|
| 196 |
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 197 |
+
*updates_freeze(disable=True)
|
| 198 |
)
|
| 199 |
|
| 200 |
if not product_name_val or not product_name_val.strip():
|
| 201 |
+
return ("Please enter a product or campaign name first.", counter, gr.update(visible=False), *updates_freeze(disable=False))
|
| 202 |
|
| 203 |
prompt = f"Generate relevant social media hashtags for the product or campaign named '{product_name_val}'. List hashtags separated by commas."
|
| 204 |
try:
|
|
|
|
| 206 |
except Exception as e:
|
| 207 |
tags = f"Error generating hashtags: {e}"
|
| 208 |
|
| 209 |
+
return (tags, counter, gr.update(visible=False), *updates_unfreeze())
|
| 210 |
|
| 211 |
# -------------------------------
|
| 212 |
# Social Media Launch
|
|
|
|
| 219 |
"⚠️ Limit reached. Launch blocked.",
|
| 220 |
counter,
|
| 221 |
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 222 |
+
*updates_freeze(disable=True)
|
| 223 |
)
|
| 224 |
|
| 225 |
if checkbox_value:
|
| 226 |
+
return (f"✅ Launched campaign on {channel} with hashtags: {hashtags}", counter, gr.update(visible=False), *updates_unfreeze())
|
| 227 |
else:
|
| 228 |
+
return ("", counter, gr.update(visible=False), *updates_unfreeze())
|
| 229 |
|
| 230 |
# -------------------------------
|
| 231 |
# UI
|
|
|
|
| 300 |
run_btn.click(
|
| 301 |
fn=run_agent,
|
| 302 |
inputs=[
|
| 303 |
+
gr.Request(), # Correctly pass the request object as a parameter
|
| 304 |
product_name,
|
| 305 |
do_competitors,
|
| 306 |
do_message,
|
|
|
|
| 375 |
],
|
| 376 |
)
|
| 377 |
|
|
|
|
| 378 |
demo.load(
|
| 379 |
fn=check_ip_on_load,
|
| 380 |
inputs=None,
|