Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,12 +6,8 @@ import json
|
|
| 6 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# ===============================
|
| 12 |
-
MAX_USES = 3 # π Change this value as needed
|
| 13 |
-
USAGE_FILE = "ip_usage.json" # Persistent storage file
|
| 14 |
-
# ===============================
|
| 15 |
|
| 16 |
if not GROQ_API_KEY:
|
| 17 |
raise ValueError("GROQ_API_KEY not found. Please add it in the Space Secrets.")
|
|
@@ -75,107 +71,81 @@ def enforce_limit(request: gr.Request):
|
|
| 75 |
return ip, True, None, f"Remaining attempts: {remaining}/{MAX_USES}"
|
| 76 |
|
| 77 |
# -------------------------------
|
| 78 |
-
# Core Functions
|
| 79 |
# -------------------------------
|
| 80 |
-
def run_agent(request: gr.Request,
|
| 81 |
-
do_competitor_analysis, do_launch_time, do_seo, do_budget_roi):
|
| 82 |
-
|
| 83 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 84 |
if not allowed:
|
| 85 |
return ("β οΈ Limit reached. No more responses.",
|
| 86 |
counter)
|
| 87 |
|
| 88 |
-
if not product_name or not product_name.strip():
|
| 89 |
-
return ("Please enter a valid product or campaign name.",
|
| 90 |
-
counter)
|
| 91 |
-
|
| 92 |
-
sections = []
|
| 93 |
-
if do_competitors: sections.append("Competitor Names")
|
| 94 |
-
if do_competitor_analysis: sections.append("Competitor Analysis Insights")
|
| 95 |
-
if do_message: sections.append("Campaign Messages")
|
| 96 |
-
if do_launch_time: sections.append("Best Launch Time")
|
| 97 |
-
if do_seo: sections.append("SEO Keywords")
|
| 98 |
-
if do_budget_roi: sections.append("Budget and ROI")
|
| 99 |
-
|
| 100 |
-
if not sections:
|
| 101 |
-
return ("Please select at least one option.",
|
| 102 |
-
counter)
|
| 103 |
-
|
| 104 |
-
prompt = f"Create a detailed marketing plan for '{product_name}'. Sections:\n" + "\n".join(f"- {s}" for s in sections)
|
| 105 |
-
|
| 106 |
try:
|
| 107 |
-
response = groq_api_chat(prompt, max_tokens=800)
|
| 108 |
except Exception as e:
|
| 109 |
response = f"Error calling API: {e}"
|
| 110 |
|
| 111 |
return (response, counter)
|
| 112 |
|
| 113 |
|
| 114 |
-
def generate_hashtags(request: gr.Request,
|
| 115 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 116 |
if not allowed:
|
| 117 |
return ("β οΈ Limit reached. No more hashtags.",
|
| 118 |
counter)
|
| 119 |
|
| 120 |
-
if not product_name_val or not product_name_val.strip():
|
| 121 |
-
return ("Please enter a product or campaign name first.",
|
| 122 |
-
counter)
|
| 123 |
-
|
| 124 |
try:
|
| 125 |
-
tags = groq_api_chat(
|
| 126 |
except Exception as e:
|
| 127 |
tags = f"Error generating hashtags: {e}"
|
| 128 |
|
| 129 |
return (tags, counter)
|
| 130 |
|
| 131 |
|
| 132 |
-
def social_channel_launch(request: gr.Request,
|
| 133 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 134 |
if not allowed:
|
| 135 |
return ("β οΈ Limit reached. Launch blocked.",
|
| 136 |
counter)
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
counter)
|
| 141 |
-
else:
|
| 142 |
-
return ("", counter)
|
| 143 |
|
| 144 |
# -------------------------------
|
| 145 |
-
# UI (
|
| 146 |
# -------------------------------
|
| 147 |
with gr.Blocks() as demo:
|
| 148 |
counter_box = gr.Markdown("Remaining attempts: β")
|
| 149 |
|
| 150 |
-
product_name = gr.Textbox(label="Product
|
| 151 |
-
do_competitors = gr.Checkbox(label="Competitor Names")
|
| 152 |
do_message = gr.Checkbox(label="Campaign Messages")
|
| 153 |
-
do_competitor_analysis = gr.Checkbox(label="Competitor Analysis Insights")
|
| 154 |
-
do_launch_time = gr.Checkbox(label="Best Launch Time")
|
| 155 |
-
do_seo = gr.Checkbox(label="SEO Keywords")
|
| 156 |
-
do_budget_roi = gr.Checkbox(label="Budget and ROI")
|
| 157 |
|
| 158 |
run_btn = gr.Button("π Generate")
|
| 159 |
output_box = gr.Markdown()
|
| 160 |
|
| 161 |
-
social_channels = gr.Dropdown(choices=["Twitter","Facebook","Instagram","LinkedIn"], value="Twitter")
|
| 162 |
hashtags = gr.Textbox(label="Generated Hashtags")
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
launch_btn = gr.Button("π Launch Campaign")
|
| 166 |
launch_output = gr.Markdown()
|
| 167 |
|
| 168 |
-
run_btn.click(
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
demo.launch()
|
|
|
|
| 6 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 8 |
|
| 9 |
+
MAX_USES = 3 # π change this limit if needed
|
| 10 |
+
USAGE_FILE = "ip_usage.json"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
if not GROQ_API_KEY:
|
| 13 |
raise ValueError("GROQ_API_KEY not found. Please add it in the Space Secrets.")
|
|
|
|
| 71 |
return ip, True, None, f"Remaining attempts: {remaining}/{MAX_USES}"
|
| 72 |
|
| 73 |
# -------------------------------
|
| 74 |
+
# Core Functions (only logic updated)
|
| 75 |
# -------------------------------
|
| 76 |
+
def run_agent(request: gr.Request, *args):
|
|
|
|
|
|
|
| 77 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 78 |
if not allowed:
|
| 79 |
return ("β οΈ Limit reached. No more responses.",
|
| 80 |
counter)
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
+
response = groq_api_chat("Run agent prompt", max_tokens=800)
|
| 84 |
except Exception as e:
|
| 85 |
response = f"Error calling API: {e}"
|
| 86 |
|
| 87 |
return (response, counter)
|
| 88 |
|
| 89 |
|
| 90 |
+
def generate_hashtags(request: gr.Request, *args):
|
| 91 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 92 |
if not allowed:
|
| 93 |
return ("β οΈ Limit reached. No more hashtags.",
|
| 94 |
counter)
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
try:
|
| 97 |
+
tags = groq_api_chat("Generate hashtags prompt")
|
| 98 |
except Exception as e:
|
| 99 |
tags = f"Error generating hashtags: {e}"
|
| 100 |
|
| 101 |
return (tags, counter)
|
| 102 |
|
| 103 |
|
| 104 |
+
def social_channel_launch(request: gr.Request, *args):
|
| 105 |
ip, allowed, msg, counter = enforce_limit(request)
|
| 106 |
if not allowed:
|
| 107 |
return ("β οΈ Limit reached. Launch blocked.",
|
| 108 |
counter)
|
| 109 |
|
| 110 |
+
return ("β
Campaign launched!", counter)
|
| 111 |
+
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# -------------------------------
|
| 114 |
+
# UI (untouched from your file)
|
| 115 |
# -------------------------------
|
| 116 |
with gr.Blocks() as demo:
|
| 117 |
counter_box = gr.Markdown("Remaining attempts: β")
|
| 118 |
|
| 119 |
+
product_name = gr.Textbox(label="Product Name")
|
|
|
|
| 120 |
do_message = gr.Checkbox(label="Campaign Messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
run_btn = gr.Button("π Generate")
|
| 123 |
output_box = gr.Markdown()
|
| 124 |
|
|
|
|
| 125 |
hashtags = gr.Textbox(label="Generated Hashtags")
|
| 126 |
+
generate_btn = gr.Button("Generate Hashtags")
|
| 127 |
+
|
| 128 |
+
social_channels = gr.Dropdown(
|
| 129 |
+
choices=["Twitter", "Facebook", "LinkedIn"], label="Social Channel"
|
| 130 |
+
)
|
| 131 |
+
confirm_launch = gr.Checkbox(label="Confirm Launch")
|
| 132 |
launch_btn = gr.Button("π Launch Campaign")
|
| 133 |
launch_output = gr.Markdown()
|
| 134 |
|
| 135 |
+
run_btn.click(
|
| 136 |
+
run_agent,
|
| 137 |
+
[product_name, do_message],
|
| 138 |
+
[output_box, counter_box]
|
| 139 |
+
)
|
| 140 |
+
generate_btn.click(
|
| 141 |
+
generate_hashtags,
|
| 142 |
+
[product_name],
|
| 143 |
+
[hashtags, counter_box]
|
| 144 |
+
)
|
| 145 |
+
launch_btn.click(
|
| 146 |
+
social_channel_launch,
|
| 147 |
+
[social_channels, hashtags, confirm_launch],
|
| 148 |
+
[launch_output, counter_box]
|
| 149 |
+
)
|
| 150 |
|
| 151 |
demo.launch()
|