Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,46 +2,51 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 8 |
|
| 9 |
-
# ===============================
|
| 10 |
-
# ๐ง Usage Limit Configuration
|
| 11 |
-
# ===============================
|
| 12 |
-
ENABLE_IP_LIMIT = True # ๐ Comment/Set False to disable IP limiting
|
| 13 |
-
MAX_USES = 3 # ๐ Maximum allowed actions per IP
|
| 14 |
-
USAGE_FILE = "ip_usage.json" # ๐ Persistent storage file
|
| 15 |
-
# ===============================
|
| 16 |
-
|
| 17 |
-
if not GROQ_API_KEY:
|
| 18 |
-
raise ValueError("GROQ_API_KEY not found. Please add it in the Space Secrets.")
|
| 19 |
|
| 20 |
# -------------------------------
|
| 21 |
-
#
|
| 22 |
# -------------------------------
|
| 23 |
def load_usage():
|
| 24 |
-
if
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return json.load(f)
|
| 28 |
-
except Exception:
|
| 29 |
-
return {}
|
| 30 |
return {}
|
| 31 |
|
| 32 |
def save_usage(data):
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
ip_usage = load_usage()
|
| 40 |
|
| 41 |
# -------------------------------
|
| 42 |
-
# API
|
| 43 |
# -------------------------------
|
| 44 |
-
def groq_api_chat(prompt, model="llama3-8b-8192", max_tokens=
|
| 45 |
headers = {
|
| 46 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 47 |
"Content-Type": "application/json"
|
|
@@ -50,333 +55,81 @@ def groq_api_chat(prompt, model="llama3-8b-8192", max_tokens=800):
|
|
| 50 |
"model": model,
|
| 51 |
"messages": [{"role": "user", "content": prompt}],
|
| 52 |
"max_tokens": max_tokens,
|
| 53 |
-
"temperature": 0.6,
|
| 54 |
-
"top_p": 0.9,
|
| 55 |
}
|
| 56 |
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
|
| 57 |
response.raise_for_status()
|
| 58 |
-
|
| 59 |
-
return data["choices"][0]["message"]["content"].strip()
|
| 60 |
|
| 61 |
-
# -------------------------------
|
| 62 |
-
# Helpers
|
| 63 |
-
# -------------------------------
|
| 64 |
-
def get_component_updates(interactive_state: bool):
|
| 65 |
-
"""Returns a tuple of gr.update objects for all interactive components."""
|
| 66 |
-
upd = gr.update(interactive=interactive_state)
|
| 67 |
-
return (
|
| 68 |
-
upd, upd, upd, # run_btn, generate_hashtags_btn, launch_btn
|
| 69 |
-
upd, # product_name
|
| 70 |
-
upd, upd, upd, upd, upd, upd, # 6 checkboxes
|
| 71 |
-
upd, # social_channels
|
| 72 |
-
upd, # hashtags textbox
|
| 73 |
-
upd # launch_checkbox
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
def enforce_limit(request: gr.Request):
|
| 77 |
-
ip = request.client.host if request and request.client else "unknown"
|
| 78 |
-
if ENABLE_IP_LIMIT:
|
| 79 |
-
used = ip_usage.get(ip, 0)
|
| 80 |
-
|
| 81 |
-
if used >= MAX_USES:
|
| 82 |
-
return ip, False, (
|
| 83 |
-
f"โ ๏ธ This IP has reached the maximum limit of {MAX_USES} uses. "
|
| 84 |
-
"All controls are disabled. Contact the admin."
|
| 85 |
-
), f"Remaining attempts: 0/{MAX_USES}"
|
| 86 |
-
|
| 87 |
-
used += 1
|
| 88 |
-
ip_usage[ip] = used
|
| 89 |
-
save_usage(ip_usage)
|
| 90 |
-
|
| 91 |
-
remaining = MAX_USES - used
|
| 92 |
-
return ip, True, None, f"Remaining attempts: {remaining}/{MAX_USES}"
|
| 93 |
-
|
| 94 |
-
return ip, True, None, "Unlimited attempts"
|
| 95 |
|
| 96 |
# -------------------------------
|
| 97 |
-
#
|
| 98 |
# -------------------------------
|
| 99 |
-
def
|
| 100 |
-
|
| 101 |
-
used = ip_usage.get(ip, 0)
|
| 102 |
-
if ENABLE_IP_LIMIT and used >= MAX_USES:
|
| 103 |
-
msg = f"โ ๏ธ This IP has reached the maximum limit of {MAX_USES} uses. All controls are disabled. Contact the admin."
|
| 104 |
-
return (
|
| 105 |
-
f"Remaining attempts: 0/{MAX_USES}",
|
| 106 |
-
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 107 |
-
*get_component_updates(False)
|
| 108 |
-
)
|
| 109 |
-
else:
|
| 110 |
-
remaining = MAX_USES - used if ENABLE_IP_LIMIT else "โ"
|
| 111 |
-
return (
|
| 112 |
-
f"Remaining attempts: {remaining}/{MAX_USES}" if ENABLE_IP_LIMIT else "Unlimited attempts",
|
| 113 |
-
gr.update(visible=False),
|
| 114 |
-
*get_component_updates(True)
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
# -------------------------------
|
| 118 |
-
# Main Agent (Generate)
|
| 119 |
-
# -------------------------------
|
| 120 |
-
def run_agent(request: gr.Request,
|
| 121 |
-
product_name,
|
| 122 |
-
do_competitors,
|
| 123 |
-
do_message,
|
| 124 |
-
do_competitor_analysis,
|
| 125 |
-
do_launch_time,
|
| 126 |
-
do_seo,
|
| 127 |
-
do_budget_roi):
|
| 128 |
-
|
| 129 |
-
ip, allowed, msg, counter = enforce_limit(request)
|
| 130 |
-
|
| 131 |
if not allowed:
|
| 132 |
-
return (
|
| 133 |
-
"โ ๏ธ Limit reached. No more responses.",
|
| 134 |
-
counter,
|
| 135 |
-
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 136 |
-
*get_component_updates(False)
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
if not product_name or not product_name.strip():
|
| 140 |
-
return ("Please enter a valid product or campaign name.", counter, gr.update(visible=False), *get_component_updates(True))
|
| 141 |
-
|
| 142 |
-
prompt = f"Create a detailed marketing plan for '{product_name}'.\nOnly include these sections:\n"
|
| 143 |
-
sections = []
|
| 144 |
-
if do_competitors:
|
| 145 |
-
sections.append("Competitor Names")
|
| 146 |
-
if do_competitor_analysis:
|
| 147 |
-
sections.append("Competitor Analysis Insights")
|
| 148 |
-
if do_message:
|
| 149 |
-
sections.append("Campaign Messages")
|
| 150 |
-
if do_launch_time:
|
| 151 |
-
sections.append("Best Launch Time")
|
| 152 |
-
if do_seo:
|
| 153 |
-
sections.append("SEO Keywords")
|
| 154 |
-
if do_budget_roi:
|
| 155 |
-
sections.append("Budget and ROI")
|
| 156 |
-
|
| 157 |
-
if not sections:
|
| 158 |
-
return ("Please select at least one option to generate the marketing plan.", counter, gr.update(visible=False), *get_component_updates(True))
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
|
| 163 |
-
prompt += "\nRespond with each section clearly headed as shown above."
|
| 164 |
-
|
| 165 |
-
try:
|
| 166 |
-
response = groq_api_chat(prompt, max_tokens=800)
|
| 167 |
-
except Exception as e:
|
| 168 |
-
response = f"Error calling API: {e}"
|
| 169 |
-
|
| 170 |
-
return (response, counter, gr.update(visible=False), *get_component_updates(True))
|
| 171 |
-
|
| 172 |
-
# -------------------------------
|
| 173 |
-
# Hashtag Generator
|
| 174 |
-
# -------------------------------
|
| 175 |
-
def generate_hashtags(request: gr.Request, product_name_val):
|
| 176 |
-
ip, allowed, msg, counter = enforce_limit(request)
|
| 177 |
|
|
|
|
|
|
|
| 178 |
if not allowed:
|
| 179 |
-
return (
|
| 180 |
-
"โ ๏ธ Limit reached. No more hashtags.",
|
| 181 |
-
counter,
|
| 182 |
-
gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
|
| 183 |
-
*get_component_updates(False)
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
-
if not product_name_val or not product_name_val.strip():
|
| 187 |
-
return ("Please enter a product or campaign name first.", counter, gr.update(visible=False), *get_component_updates(True))
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
tags = groq_api_chat(prompt)
|
| 192 |
-
except Exception as e:
|
| 193 |
-
tags = f"Error generating hashtags: {e}"
|
| 194 |
|
| 195 |
-
return (tags, counter, gr.update(visible=False), *get_component_updates(True))
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
# -------------------------------
|
| 200 |
-
def social_channel_launch(request: gr.Request, channel, hashtags, checkbox_value):
|
| 201 |
-
ip, allowed, msg, counter = enforce_limit(request)
|
| 202 |
-
|
| 203 |
if not allowed:
|
| 204 |
-
return (
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
|
| 211 |
-
if checkbox_value:
|
| 212 |
-
return (f"โ
Launched campaign on {channel} with hashtags: {hashtags}", counter, gr.update(visible=False), *get_component_updates(True))
|
| 213 |
-
else:
|
| 214 |
-
return ("", counter, gr.update(visible=False), *get_component_updates(True))
|
| 215 |
|
| 216 |
# -------------------------------
|
| 217 |
# UI
|
| 218 |
# -------------------------------
|
| 219 |
with gr.Blocks() as demo:
|
| 220 |
-
|
| 221 |
-
limit_banner = gr.Markdown("", visible=False)
|
| 222 |
-
|
| 223 |
-
product_name = gr.Textbox(label="Product or Campaign Name", placeholder="Enter product or campaign name here...")
|
| 224 |
-
|
| 225 |
-
with gr.Row():
|
| 226 |
-
goals_vision = gr.Checkbox(label="Goals & Vision (disabled for now)", value=False, interactive=False)
|
| 227 |
-
target_audience = gr.Checkbox(label="Target Audience (disabled for now)", value=False, interactive=False)
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
do_message = gr.Checkbox(label="Campaign Messages & Social Media", value=False)
|
| 232 |
-
with gr.Row():
|
| 233 |
-
do_competitor_analysis = gr.Checkbox(label="Competitor Analysis Insights", value=False)
|
| 234 |
-
do_launch_time = gr.Checkbox(label="Best Campaign Launch Time", value=False)
|
| 235 |
-
with gr.Row():
|
| 236 |
-
do_seo = gr.Checkbox(label="SEO Keywords", value=False)
|
| 237 |
-
do_budget_roi = gr.Checkbox(label="Campaign Budget Estimation & ROI", value=False)
|
| 238 |
|
| 239 |
-
|
| 240 |
-
counter_box = gr.Markdown("Remaining attempts: โ")
|
| 241 |
-
|
| 242 |
-
run_btn = gr.Button("๐ Generate", interactive=False)
|
| 243 |
output_box = gr.Markdown()
|
| 244 |
|
|
|
|
|
|
|
|
|
|
| 245 |
social_channels = gr.Dropdown(
|
| 246 |
-
|
| 247 |
-
choices=["Twitter", "Facebook", "Instagram", "LinkedIn"],
|
| 248 |
-
value="Twitter",
|
| 249 |
-
interactive=True,
|
| 250 |
-
multiselect=False
|
| 251 |
)
|
| 252 |
-
|
| 253 |
-
hashtags = gr.Textbox(label="Generated Hashtags", interactive=False)
|
| 254 |
-
generate_hashtags_btn = gr.Button("Generate Hashtags")
|
| 255 |
-
|
| 256 |
-
launch_checkbox = gr.Checkbox(label="Confirm Launch Campaign", value=False)
|
| 257 |
launch_btn = gr.Button("๐ Launch Campaign")
|
| 258 |
-
|
| 259 |
launch_output = gr.Markdown()
|
| 260 |
|
| 261 |
-
#
|
| 262 |
-
def enable_generate_button(product_name_val, *checkbox_values):
|
| 263 |
-
product_entered = bool(product_name_val and product_name_val.strip())
|
| 264 |
-
any_checked = any(checkbox_values)
|
| 265 |
-
return gr.update(interactive=product_entered and any_checked)
|
| 266 |
-
|
| 267 |
-
inputs_for_enable = [product_name,
|
| 268 |
-
do_competitors,
|
| 269 |
-
do_message,
|
| 270 |
-
do_competitor_analysis,
|
| 271 |
-
do_launch_time,
|
| 272 |
-
do_seo,
|
| 273 |
-
do_budget_roi]
|
| 274 |
-
|
| 275 |
-
for comp in inputs_for_enable:
|
| 276 |
-
comp.change(fn=enable_generate_button,
|
| 277 |
-
inputs=[product_name,
|
| 278 |
-
do_competitors,
|
| 279 |
-
do_message,
|
| 280 |
-
do_competitor_analysis,
|
| 281 |
-
do_launch_time,
|
| 282 |
-
do_seo,
|
| 283 |
-
do_budget_roi],
|
| 284 |
-
outputs=run_btn)
|
| 285 |
-
|
| 286 |
-
# NEW Chained Callbacks to prevent race condition
|
| 287 |
-
# ---------------------------------------------
|
| 288 |
run_btn.click(
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
run_btn, generate_hashtags_btn, launch_btn, product_name,
|
| 293 |
-
do_competitors, do_message, do_competitor_analysis, do_launch_time, do_seo, do_budget_roi,
|
| 294 |
-
social_channels, hashtags, launch_checkbox
|
| 295 |
-
]
|
| 296 |
-
).then(
|
| 297 |
-
fn=run_agent,
|
| 298 |
-
inputs=[
|
| 299 |
-
product_name,
|
| 300 |
-
do_competitors,
|
| 301 |
-
do_message,
|
| 302 |
-
do_competitor_analysis,
|
| 303 |
-
do_launch_time,
|
| 304 |
-
do_seo,
|
| 305 |
-
do_budget_roi
|
| 306 |
-
],
|
| 307 |
-
outputs=[
|
| 308 |
-
output_box,
|
| 309 |
-
counter_box,
|
| 310 |
-
limit_banner,
|
| 311 |
-
run_btn, generate_hashtags_btn, launch_btn, product_name,
|
| 312 |
-
do_competitors, do_message, do_competitor_analysis, do_launch_time, do_seo, do_budget_roi,
|
| 313 |
-
social_channels, hashtags, launch_checkbox
|
| 314 |
-
],
|
| 315 |
)
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
outputs=[
|
| 321 |
-
run_btn, generate_hashtags_btn, launch_btn, product_name,
|
| 322 |
-
do_competitors, do_message, do_competitor_analysis, do_launch_time, do_seo, do_budget_roi,
|
| 323 |
-
social_channels, hashtags, launch_checkbox
|
| 324 |
-
]
|
| 325 |
-
).then(
|
| 326 |
-
fn=generate_hashtags,
|
| 327 |
-
inputs=[product_name],
|
| 328 |
-
outputs=[
|
| 329 |
-
hashtags,
|
| 330 |
-
counter_box,
|
| 331 |
-
limit_banner,
|
| 332 |
-
run_btn, generate_hashtags_btn, launch_btn, product_name,
|
| 333 |
-
do_competitors, do_message, do_competitor_analysis, do_launch_time, do_seo, do_budget_roi,
|
| 334 |
-
social_channels, hashtags, launch_checkbox
|
| 335 |
-
],
|
| 336 |
)
|
| 337 |
-
|
| 338 |
launch_btn.click(
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
run_btn, generate_hashtags_btn, launch_btn, product_name,
|
| 343 |
-
do_competitors, do_message, do_competitor_analysis, do_launch_time, do_seo, do_budget_roi,
|
| 344 |
-
social_channels, hashtags, launch_checkbox
|
| 345 |
-
]
|
| 346 |
-
).then(
|
| 347 |
-
fn=social_channel_launch,
|
| 348 |
-
inputs=[social_channels, hashtags, launch_checkbox],
|
| 349 |
-
outputs=[
|
| 350 |
-
launch_output,
|
| 351 |
-
counter_box,
|
| 352 |
-
limit_banner,
|
| 353 |
-
run_btn, generate_hashtags_btn, launch_btn, product_name,
|
| 354 |
-
do_competitors, do_message, do_competitor_analysis, do_launch_time, do_seo, do_budget_roi,
|
| 355 |
-
social_channels, hashtags, launch_checkbox
|
| 356 |
-
],
|
| 357 |
-
)
|
| 358 |
-
# ---------------------------------------------
|
| 359 |
-
|
| 360 |
-
demo.load(
|
| 361 |
-
fn=check_ip_on_load,
|
| 362 |
-
inputs=None,
|
| 363 |
-
outputs=[
|
| 364 |
-
counter_box,
|
| 365 |
-
limit_banner,
|
| 366 |
-
run_btn,
|
| 367 |
-
generate_hashtags_btn,
|
| 368 |
-
launch_btn,
|
| 369 |
-
product_name,
|
| 370 |
-
do_competitors,
|
| 371 |
-
do_message,
|
| 372 |
-
do_competitor_analysis,
|
| 373 |
-
do_launch_time,
|
| 374 |
-
do_seo,
|
| 375 |
-
do_budget_roi,
|
| 376 |
-
social_channels,
|
| 377 |
-
hashtags,
|
| 378 |
-
launch_checkbox,
|
| 379 |
-
],
|
| 380 |
)
|
| 381 |
|
| 382 |
-
demo.launch()
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
# -------------------------------
|
| 8 |
+
# Config
|
| 9 |
+
# -------------------------------
|
| 10 |
+
USAGE_FILE = Path("ip_usage.json")
|
| 11 |
+
MAX_USES = 3 # you can adjust this limit anytime
|
| 12 |
|
| 13 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 14 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# -------------------------------
|
| 18 |
+
# IP-based usage tracking
|
| 19 |
# -------------------------------
|
| 20 |
def load_usage():
|
| 21 |
+
if USAGE_FILE.exists():
|
| 22 |
+
with open(USAGE_FILE, "r") as f:
|
| 23 |
+
return json.load(f)
|
|
|
|
|
|
|
|
|
|
| 24 |
return {}
|
| 25 |
|
| 26 |
def save_usage(data):
|
| 27 |
+
with open(USAGE_FILE, "w") as f:
|
| 28 |
+
json.dump(data, f)
|
| 29 |
+
|
| 30 |
+
def check_limit(request: gr.Request):
|
| 31 |
+
ip = request.client.host if request and request.client else "unknown"
|
| 32 |
+
usage = load_usage()
|
| 33 |
+
count = usage.get(ip, 0)
|
| 34 |
+
|
| 35 |
+
if count >= MAX_USES:
|
| 36 |
+
return False, f"โ ๏ธ Usage limit reached for IP {ip}. Contact admin."
|
| 37 |
+
return True, f"Remaining attempts: {MAX_USES - count}/{MAX_USES}"
|
| 38 |
+
|
| 39 |
+
def increment_usage(request: gr.Request):
|
| 40 |
+
ip = request.client.host if request and request.client else "unknown"
|
| 41 |
+
usage = load_usage()
|
| 42 |
+
usage[ip] = usage.get(ip, 0) + 1
|
| 43 |
+
save_usage(usage)
|
| 44 |
|
|
|
|
| 45 |
|
| 46 |
# -------------------------------
|
| 47 |
+
# External API call
|
| 48 |
# -------------------------------
|
| 49 |
+
def groq_api_chat(prompt, model="llama3-8b-8192", max_tokens=500):
|
| 50 |
headers = {
|
| 51 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 52 |
"Content-Type": "application/json"
|
|
|
|
| 55 |
"model": model,
|
| 56 |
"messages": [{"role": "user", "content": prompt}],
|
| 57 |
"max_tokens": max_tokens,
|
|
|
|
|
|
|
| 58 |
}
|
| 59 |
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
|
| 60 |
response.raise_for_status()
|
| 61 |
+
return response.json()["choices"][0]["message"]["content"].strip()
|
|
|
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# -------------------------------
|
| 65 |
+
# Core Functions
|
| 66 |
# -------------------------------
|
| 67 |
+
def run_agent(request: gr.Request, product_name, do_message):
|
| 68 |
+
allowed, status = check_limit(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if not allowed:
|
| 70 |
+
return "โ ๏ธ Limit reached.", status, gr.update(interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
increment_usage(request)
|
| 73 |
+
return f"Marketing plan for {product_name}", status, gr.update(interactive=True)
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
def generate_hashtags(request: gr.Request, product_name):
|
| 77 |
+
allowed, status = check_limit(request)
|
| 78 |
if not allowed:
|
| 79 |
+
return "โ ๏ธ Limit reached.", status, gr.update(interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
increment_usage(request)
|
| 82 |
+
return f"#{product_name} #AI #Launch", status, gr.update(interactive=True)
|
|
|
|
|
|
|
|
|
|
| 83 |
|
|
|
|
| 84 |
|
| 85 |
+
def social_channel_launch(request: gr.Request, channel, hashtags, confirm):
|
| 86 |
+
allowed, status = check_limit(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
if not allowed:
|
| 88 |
+
return "โ ๏ธ Limit reached.", status, gr.update(interactive=False)
|
| 89 |
+
|
| 90 |
+
increment_usage(request)
|
| 91 |
+
if confirm:
|
| 92 |
+
return f"โ
Launched on {channel} with {hashtags}", status, gr.update(interactive=True)
|
| 93 |
+
return "Launch not confirmed.", status, gr.update(interactive=True)
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# -------------------------------
|
| 97 |
# UI
|
| 98 |
# -------------------------------
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
+
counter_box = gr.Markdown("Remaining attempts: 3/3")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
product_name = gr.Textbox(label="Product Name")
|
| 103 |
+
do_message = gr.Checkbox(label="Campaign Messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
run_btn = gr.Button("๐ Generate")
|
|
|
|
|
|
|
|
|
|
| 106 |
output_box = gr.Markdown()
|
| 107 |
|
| 108 |
+
hashtags = gr.Textbox(label="Generated Hashtags")
|
| 109 |
+
generate_btn = gr.Button("Generate Hashtags")
|
| 110 |
+
|
| 111 |
social_channels = gr.Dropdown(
|
| 112 |
+
choices=["Twitter", "Facebook", "LinkedIn"], label="Social Channel"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
)
|
| 114 |
+
confirm_launch = gr.Checkbox(label="Confirm Launch")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
launch_btn = gr.Button("๐ Launch Campaign")
|
|
|
|
| 116 |
launch_output = gr.Markdown()
|
| 117 |
|
| 118 |
+
# === FIXED: Removed gr.Request() from inputs (Gradio auto-injects it) ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
run_btn.click(
|
| 120 |
+
run_agent,
|
| 121 |
+
[product_name, do_message],
|
| 122 |
+
[output_box, counter_box, run_btn]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
)
|
| 124 |
+
generate_btn.click(
|
| 125 |
+
generate_hashtags,
|
| 126 |
+
[product_name],
|
| 127 |
+
[hashtags, counter_box, generate_btn]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
)
|
|
|
|
| 129 |
launch_btn.click(
|
| 130 |
+
social_channel_launch,
|
| 131 |
+
[social_channels, hashtags, confirm_launch],
|
| 132 |
+
[launch_output, counter_box, launch_btn]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
+
demo.launch()
|