Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,25 +13,23 @@ reasoning_model = pipeline("text2text-generation", model="google/flan-t5-base")
|
|
| 13 |
# -----------------------------
|
| 14 |
# Config
|
| 15 |
# -----------------------------
|
| 16 |
-
CHANNELS = ["Instagram", "
|
| 17 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 18 |
GOOGLE_CX = os.getenv("GOOGLE_CX")
|
| 19 |
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
|
| 20 |
SERPAPI_KEY = os.getenv("SERPAPI_KEY")
|
| 21 |
|
| 22 |
# -----------------------------
|
| 23 |
-
# Reason Generator
|
| 24 |
# -----------------------------
|
| 25 |
def generate_reason(product_desc, channel):
|
| 26 |
-
"""
|
| 27 |
-
Generate distinct, marketing-oriented AI reasoning for each platform.
|
| 28 |
-
"""
|
| 29 |
prompt = f"""
|
| 30 |
You are a social media marketing expert.
|
| 31 |
The product is: {product_desc}.
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
"""
|
| 36 |
try:
|
| 37 |
out = reasoning_model(prompt, max_new_tokens=120, truncation=True)
|
|
@@ -52,7 +50,7 @@ def score_channels(product_desc):
|
|
| 52 |
return {ch: round(random.uniform(0.1, 0.5), 2) for ch in CHANNELS}
|
| 53 |
|
| 54 |
# -----------------------------
|
| 55 |
-
# Search Functions (
|
| 56 |
# -----------------------------
|
| 57 |
def google_search(query, site=None, num=3, region_code=None):
|
| 58 |
if not GOOGLE_API_KEY or not GOOGLE_CX:
|
|
@@ -110,10 +108,8 @@ def youtube_search(query, num=3, region_code=None):
|
|
| 110 |
for item in data["items"][:num]:
|
| 111 |
title = item["snippet"]["title"]
|
| 112 |
video_id = item["id"]["videoId"]
|
| 113 |
-
|
| 114 |
-
results.append((title, url_link))
|
| 115 |
return results
|
| 116 |
-
|
| 117 |
except Exception:
|
| 118 |
return []
|
| 119 |
|
|
@@ -128,12 +124,14 @@ def suggest_channels(product_desc, region_code):
|
|
| 128 |
ranked_channels = sorted(scores.items(), key=lambda x: x[1], reverse=True)
|
| 129 |
|
| 130 |
for rank, (channel, score) in enumerate(ranked_channels, 1):
|
|
|
|
| 131 |
reason = generate_reason(product_desc, channel)
|
| 132 |
|
| 133 |
-
# Build search query
|
| 134 |
-
query = f"top {channel} influencers, trending posts, popular
|
| 135 |
site_filter = channel.lower() + ".com" if channel != "YouTube" else None
|
| 136 |
|
|
|
|
| 137 |
if channel == "YouTube":
|
| 138 |
links = youtube_search(query, num=3, region_code=region_code)
|
| 139 |
if not links:
|
|
@@ -143,7 +141,7 @@ def suggest_channels(product_desc, region_code):
|
|
| 143 |
if not links:
|
| 144 |
links = serpapi_search(query, site=site_filter, num=3, region_code=region_code)
|
| 145 |
|
| 146 |
-
# Format links
|
| 147 |
if not links:
|
| 148 |
formatted_links = ["- ⚠️ No relevant influencers or posts found"]
|
| 149 |
else:
|
|
@@ -159,7 +157,7 @@ def suggest_channels(product_desc, region_code):
|
|
| 159 |
# -----------------------------
|
| 160 |
with gr.Blocks() as demo:
|
| 161 |
gr.Markdown("## 📢 Social Channel AI Recommender (Ranked)")
|
| 162 |
-
product = gr.Textbox(label="Describe your product", placeholder="e.g.,
|
| 163 |
region = gr.Dropdown(choices=["IN", "US", "UK", "CA", "AU", "SG", "None"], value="IN", label="Select Region (ISO code)")
|
| 164 |
output = gr.HTML()
|
| 165 |
btn = gr.Button("Suggest Channels")
|
|
|
|
| 13 |
# -----------------------------
|
| 14 |
# Config
|
| 15 |
# -----------------------------
|
| 16 |
+
CHANNELS = ["Instagram", "YouTube", "Facebook", "LinkedIn"]
|
| 17 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 18 |
GOOGLE_CX = os.getenv("GOOGLE_CX")
|
| 19 |
YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
|
| 20 |
SERPAPI_KEY = os.getenv("SERPAPI_KEY")
|
| 21 |
|
| 22 |
# -----------------------------
|
| 23 |
+
# Reason Generator (Platform-specific)
|
| 24 |
# -----------------------------
|
| 25 |
def generate_reason(product_desc, channel):
|
|
|
|
|
|
|
|
|
|
| 26 |
prompt = f"""
|
| 27 |
You are a social media marketing expert.
|
| 28 |
The product is: {product_desc}.
|
| 29 |
+
Provide 2-3 sentences explaining why '{channel}' is ideal for promoting it.
|
| 30 |
+
Focus on: target audience, content format (e.g., Reels, videos, posts), influencer collaborations, and engagement strategies.
|
| 31 |
+
Each platform should have a distinct, actionable recommendation in marketing style, suitable for a report.
|
| 32 |
+
Do NOT include instruction text in the output.
|
| 33 |
"""
|
| 34 |
try:
|
| 35 |
out = reasoning_model(prompt, max_new_tokens=120, truncation=True)
|
|
|
|
| 50 |
return {ch: round(random.uniform(0.1, 0.5), 2) for ch in CHANNELS}
|
| 51 |
|
| 52 |
# -----------------------------
|
| 53 |
+
# Search Functions (Top influencers/trending posts)
|
| 54 |
# -----------------------------
|
| 55 |
def google_search(query, site=None, num=3, region_code=None):
|
| 56 |
if not GOOGLE_API_KEY or not GOOGLE_CX:
|
|
|
|
| 108 |
for item in data["items"][:num]:
|
| 109 |
title = item["snippet"]["title"]
|
| 110 |
video_id = item["id"]["videoId"]
|
| 111 |
+
results.append((title, f"https://www.youtube.com/watch?v={video_id}"))
|
|
|
|
| 112 |
return results
|
|
|
|
| 113 |
except Exception:
|
| 114 |
return []
|
| 115 |
|
|
|
|
| 124 |
ranked_channels = sorted(scores.items(), key=lambda x: x[1], reverse=True)
|
| 125 |
|
| 126 |
for rank, (channel, score) in enumerate(ranked_channels, 1):
|
| 127 |
+
# AI-generated reasoning
|
| 128 |
reason = generate_reason(product_desc, channel)
|
| 129 |
|
| 130 |
+
# Build search query for influencers/posts
|
| 131 |
+
query = f"top {channel} influencers, trending posts, popular content for {product_desc}"
|
| 132 |
site_filter = channel.lower() + ".com" if channel != "YouTube" else None
|
| 133 |
|
| 134 |
+
# Get relevant links
|
| 135 |
if channel == "YouTube":
|
| 136 |
links = youtube_search(query, num=3, region_code=region_code)
|
| 137 |
if not links:
|
|
|
|
| 141 |
if not links:
|
| 142 |
links = serpapi_search(query, site=site_filter, num=3, region_code=region_code)
|
| 143 |
|
| 144 |
+
# Format links
|
| 145 |
if not links:
|
| 146 |
formatted_links = ["- ⚠️ No relevant influencers or posts found"]
|
| 147 |
else:
|
|
|
|
| 157 |
# -----------------------------
|
| 158 |
with gr.Blocks() as demo:
|
| 159 |
gr.Markdown("## 📢 Social Channel AI Recommender (Ranked)")
|
| 160 |
+
product = gr.Textbox(label="Describe your product", placeholder="e.g., Small local coffee shop in Mumbai")
|
| 161 |
region = gr.Dropdown(choices=["IN", "US", "UK", "CA", "AU", "SG", "None"], value="IN", label="Select Region (ISO code)")
|
| 162 |
output = gr.HTML()
|
| 163 |
btn = gr.Button("Suggest Channels")
|