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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -61,16 +61,30 @@ def groq_api_chat(prompt, model="llama3-8b-8192", max_tokens=800):
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 enforce_limit(request: gr.Request):
75
  ip = request.client.host if request and request.client else "unknown"
76
  if ENABLE_IP_LIMIT:
@@ -101,14 +115,14 @@ def check_ip_on_load(request: gr.Request):
101
  return (
102
  f"Remaining attempts: 0/{MAX_USES}",
103
  gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
104
- *updates_freeze(disable=True)
105
  )
106
  else:
107
  remaining = MAX_USES - used if ENABLE_IP_LIMIT else "∞"
108
  return (
109
  f"Remaining attempts: {remaining}/{MAX_USES}" if ENABLE_IP_LIMIT else "Unlimited attempts",
110
  gr.update(visible=False),
111
- *updates_freeze(disable=False)
112
  )
113
 
114
  # -------------------------------
@@ -130,11 +144,11 @@ def run_agent(request: gr.Request,
130
  "⚠️ Limit reached. No more responses.",
131
  counter,
132
  gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
133
- *updates_freeze(disable=True)
134
  )
135
 
136
  if not product_name or not product_name.strip():
137
- return ("Please enter a valid product or campaign name.", counter, gr.update(visible=False), *updates_freeze(disable=False))
138
 
139
  prompt = f"Create a detailed marketing plan for '{product_name}'.\nOnly include these sections:\n"
140
  sections = []
@@ -152,7 +166,7 @@ def run_agent(request: gr.Request,
152
  sections.append("Budget and ROI")
153
 
154
  if not sections:
155
- return ("Please select at least one option to generate the marketing plan.", counter, gr.update(visible=False), *updates_freeze(disable=False))
156
 
157
  for s in sections:
158
  prompt += f"- {s}\n"
@@ -164,7 +178,7 @@ def run_agent(request: gr.Request,
164
  except Exception as e:
165
  response = f"Error calling API: {e}"
166
 
167
- return (response, counter, gr.update(visible=False), *updates_freeze(disable=False))
168
 
169
  # -------------------------------
170
  # Hashtag Generator
@@ -177,11 +191,11 @@ def generate_hashtags(request: gr.Request, product_name_val):
177
  "⚠️ Limit reached. No more hashtags.",
178
  counter,
179
  gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
180
- *updates_freeze(disable=True)
181
  )
182
 
183
  if not product_name_val or not product_name_val.strip():
184
- return ("Please enter a product or campaign name first.", counter, gr.update(visible=False), *updates_freeze(disable=False))
185
 
186
  prompt = f"Generate relevant social media hashtags for the product or campaign named '{product_name_val}'. List hashtags separated by commas."
187
  try:
@@ -189,7 +203,7 @@ def generate_hashtags(request: gr.Request, product_name_val):
189
  except Exception as e:
190
  tags = f"Error generating hashtags: {e}"
191
 
192
- return (tags, counter, gr.update(visible=False), *updates_freeze(disable=False))
193
 
194
  # -------------------------------
195
  # Social Media Launch
@@ -202,13 +216,13 @@ def social_channel_launch(request: gr.Request, channel, hashtags, checkbox_value
202
  "⚠️ Limit reached. Launch blocked.",
203
  counter,
204
  gr.update(value=f"<div style='color:white; background-color:red; padding:10px; font-weight:bold;'>{msg}</div>", visible=True),
205
- *updates_freeze(disable=True)
206
  )
207
 
208
  if checkbox_value:
209
- return (f"✅ Launched campaign on {channel} with hashtags: {hashtags}", counter, gr.update(visible=False), *updates_freeze(disable=False))
210
  else:
211
- return ("", counter, gr.update(visible=False), *updates_freeze(disable=False))
212
 
213
  # -------------------------------
214
  # UI
@@ -279,7 +293,7 @@ with gr.Blocks() as demo:
279
  do_seo,
280
  do_budget_roi],
281
  outputs=run_btn)
282
-
283
  run_btn.click(
284
  fn=run_agent,
285
  inputs=[
 
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):
89
  ip = request.client.host if request and request.client else "unknown"
90
  if ENABLE_IP_LIMIT:
 
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
  "⚠️ 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
  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
  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
  "⚠️ 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
  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
  "⚠️ 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
 
293
  do_seo,
294
  do_budget_roi],
295
  outputs=run_btn)
296
+
297
  run_btn.click(
298
  fn=run_agent,
299
  inputs=[