abhidgp1978 commited on
Commit
02b5914
Β·
verified Β·
1 Parent(s): 80e2576

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -4
app.py CHANGED
@@ -1,13 +1,46 @@
1
  import gradio as gr
2
  import os
3
  import requests
 
4
 
5
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
6
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
7
 
 
 
 
 
 
 
 
 
8
  if not GROQ_API_KEY:
9
  raise ValueError("GROQ_API_KEY not found. Please add it in the Space Secrets.")
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def groq_api_chat(prompt, model="llama3-8b-8192", max_tokens=512):
12
  headers = {
13
  "Authorization": f"Bearer {GROQ_API_KEY}",
@@ -25,13 +58,27 @@ def groq_api_chat(prompt, model="llama3-8b-8192", max_tokens=512):
25
  data = response.json()
26
  return data["choices"][0]["message"]["content"].strip()
27
 
28
- def run_agent(product_name,
 
 
 
 
29
  do_competitors,
30
  do_message,
31
  do_competitor_analysis,
32
  do_launch_time,
33
  do_seo,
34
  do_budget_roi):
 
 
 
 
 
 
 
 
 
 
35
  if not product_name or not product_name.strip():
36
  return "Please enter a valid product or campaign name."
37
 
@@ -58,7 +105,7 @@ def run_agent(product_name,
58
 
59
  prompt += "Respond with the following sections only, using exactly these section headers:\n"
60
  for s in sections:
61
- prompt += f"- {s.split(':')[0]}\n" # Just the section title before colon
62
 
63
  prompt += "\nRespond with each section clearly headed as shown above."
64
 
@@ -69,13 +116,18 @@ def run_agent(product_name,
69
 
70
  return response
71
 
72
-
 
 
73
  def social_channel_launch(channel, hashtags, checkbox_value):
74
  if checkbox_value:
75
  return f"βœ… Launched campaign on {channel} with hashtags: {hashtags}"
76
  else:
77
  return ""
78
 
 
 
 
79
  with gr.Blocks() as demo:
80
  product_name = gr.Textbox(label="Product or Campaign Name", placeholder="Enter product or campaign name here...")
81
 
@@ -106,7 +158,6 @@ with gr.Blocks() as demo:
106
  )
107
 
108
  hashtags = gr.Textbox(label="Generated Hashtags", interactive=False)
109
-
110
  generate_hashtags_btn = gr.Button("Generate Hashtags")
111
 
112
  launch_checkbox = gr.Checkbox(label="Confirm Launch Campaign", value=False)
 
1
  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 # πŸ‘ˆ Set to False (or comment) to disable
13
+ MAX_USES = 3 # πŸ‘ˆ Maximum times allowed per IP
14
+ USAGE_FILE = "ip_usage.json" # πŸ‘ˆ Persistent storage
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
+ # Persistent usage tracking
22
+ # -------------------------------
23
+ def load_usage():
24
+ if os.path.exists(USAGE_FILE):
25
+ try:
26
+ with open(USAGE_FILE, "r") as f:
27
+ return json.load(f)
28
+ except Exception:
29
+ return {}
30
+ return {}
31
+
32
+ def save_usage(data):
33
+ try:
34
+ with open(USAGE_FILE, "w") as f:
35
+ json.dump(data, f)
36
+ except Exception:
37
+ pass
38
+
39
+ ip_usage = load_usage()
40
+
41
+ # -------------------------------
42
+ # API Call
43
+ # -------------------------------
44
  def groq_api_chat(prompt, model="llama3-8b-8192", max_tokens=512):
45
  headers = {
46
  "Authorization": f"Bearer {GROQ_API_KEY}",
 
58
  data = response.json()
59
  return data["choices"][0]["message"]["content"].strip()
60
 
61
+ # -------------------------------
62
+ # Main Agent
63
+ # -------------------------------
64
+ def run_agent(request: gr.Request,
65
+ product_name,
66
  do_competitors,
67
  do_message,
68
  do_competitor_analysis,
69
  do_launch_time,
70
  do_seo,
71
  do_budget_roi):
72
+
73
+ ip = request.client.host if request and request.client else "unknown"
74
+
75
+ # βœ… Enforce usage limit
76
+ if ENABLE_IP_LIMIT:
77
+ if ip_usage.get(ip, 0) >= MAX_USES:
78
+ return f"⚠️ This IP has reached the maximum limit of {MAX_USES} uses. Please contact the admin."
79
+ ip_usage[ip] = ip_usage.get(ip, 0) + 1
80
+ save_usage(ip_usage)
81
+
82
  if not product_name or not product_name.strip():
83
  return "Please enter a valid product or campaign name."
84
 
 
105
 
106
  prompt += "Respond with the following sections only, using exactly these section headers:\n"
107
  for s in sections:
108
+ prompt += f"- {s.split(':')[0]}\n"
109
 
110
  prompt += "\nRespond with each section clearly headed as shown above."
111
 
 
116
 
117
  return response
118
 
119
+ # -------------------------------
120
+ # Social Media Launch
121
+ # -------------------------------
122
  def social_channel_launch(channel, hashtags, checkbox_value):
123
  if checkbox_value:
124
  return f"βœ… Launched campaign on {channel} with hashtags: {hashtags}"
125
  else:
126
  return ""
127
 
128
+ # -------------------------------
129
+ # UI
130
+ # -------------------------------
131
  with gr.Blocks() as demo:
132
  product_name = gr.Textbox(label="Product or Campaign Name", placeholder="Enter product or campaign name here...")
133
 
 
158
  )
159
 
160
  hashtags = gr.Textbox(label="Generated Hashtags", interactive=False)
 
161
  generate_hashtags_btn = gr.Button("Generate Hashtags")
162
 
163
  launch_checkbox = gr.Checkbox(label="Confirm Launch Campaign", value=False)