abhidgp1978 commited on
Commit
25d2837
·
verified ·
1 Parent(s): 74e8a92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -31
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 get_all_components():
65
- """Returns a list of all interactive components for updates."""
 
 
 
 
 
 
 
 
 
 
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
- *get_all_components()
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
- *get_unfrozen_components()
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
- *get_all_components()
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), *get_unfrozen_components())
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), *get_unfrozen_components())
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), *get_unfrozen_components())
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
- *get_all_components()
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), *get_unfrozen_components())
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), *get_unfrozen_components())
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
- *get_all_components()
220
  )
221
 
222
  if checkbox_value:
223
- return (f"✅ Launched campaign on {channel} with hashtags: {hashtags}", counter, gr.update(visible=False), *get_unfrozen_components())
224
  else:
225
- return ("", counter, gr.update(visible=False), *get_unfrozen_components())
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,