Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,16 +24,17 @@ SERPAPI_KEY = os.getenv("SERPAPI_KEY")
|
|
| 24 |
# -----------------------------
|
| 25 |
def generate_reason(product_desc, channel):
|
| 26 |
"""
|
| 27 |
-
Generate AI reasoning for
|
| 28 |
-
This reasoning is based on the channel + product type, not random search results.
|
| 29 |
"""
|
| 30 |
-
prompt =
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
-
out = reasoning_model(prompt, max_new_tokens=
|
| 37 |
return out[0]["generated_text"].strip()
|
| 38 |
except Exception as e:
|
| 39 |
return f"⚠️ AI error: {str(e)}"
|
|
@@ -51,7 +52,7 @@ def score_channels(product_desc):
|
|
| 51 |
return {ch: round(random.uniform(0.1, 0.5), 2) for ch in CHANNELS}
|
| 52 |
|
| 53 |
# -----------------------------
|
| 54 |
-
# Search Functions
|
| 55 |
# -----------------------------
|
| 56 |
def google_search(query, site=None, num=3, region_code=None):
|
| 57 |
if not GOOGLE_API_KEY or not GOOGLE_CX:
|
|
@@ -112,6 +113,7 @@ def youtube_search(query, num=3, region_code=None):
|
|
| 112 |
url_link = f"https://www.youtube.com/watch?v={video_id}"
|
| 113 |
results.append((title, url_link))
|
| 114 |
return results
|
|
|
|
| 115 |
except Exception:
|
| 116 |
return []
|
| 117 |
|
|
@@ -128,12 +130,12 @@ def suggest_channels(product_desc, region_code):
|
|
| 128 |
for rank, (channel, score) in enumerate(ranked_channels, 1):
|
| 129 |
reason = generate_reason(product_desc, channel)
|
| 130 |
|
| 131 |
-
#
|
| 132 |
-
query = f"top {channel}
|
| 133 |
site_filter = channel.lower() + ".com" if channel != "YouTube" else None
|
| 134 |
|
| 135 |
if channel == "YouTube":
|
| 136 |
-
links = youtube_search(
|
| 137 |
if not links:
|
| 138 |
links = serpapi_search(query, site="youtube.com", num=3, region_code=region_code)
|
| 139 |
else:
|
|
@@ -143,7 +145,7 @@ def suggest_channels(product_desc, region_code):
|
|
| 143 |
|
| 144 |
# Format links as clickable URLs
|
| 145 |
if not links:
|
| 146 |
-
formatted_links = ["- ⚠️ No
|
| 147 |
else:
|
| 148 |
formatted_links = [f"- <a href='{url}' target='_blank'>{title}</a>" for title, url in links]
|
| 149 |
|
|
|
|
| 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 |
+
Explain in 2-3 sentences why the social media channel '{channel}' is the BEST choice for promoting it.
|
| 33 |
+
Include platform-specific content types, audience engagement methods, and post examples (like Reels, videos, influencer posts, or articles).
|
| 34 |
+
Make each channel's reasoning unique and actionable.
|
| 35 |
+
"""
|
| 36 |
try:
|
| 37 |
+
out = reasoning_model(prompt, max_new_tokens=120, truncation=True)
|
| 38 |
return out[0]["generated_text"].strip()
|
| 39 |
except Exception as e:
|
| 40 |
return f"⚠️ AI error: {str(e)}"
|
|
|
|
| 52 |
return {ch: round(random.uniform(0.1, 0.5), 2) for ch in CHANNELS}
|
| 53 |
|
| 54 |
# -----------------------------
|
| 55 |
+
# Search Functions (top influencers/posts)
|
| 56 |
# -----------------------------
|
| 57 |
def google_search(query, site=None, num=3, region_code=None):
|
| 58 |
if not GOOGLE_API_KEY or not GOOGLE_CX:
|
|
|
|
| 113 |
url_link = f"https://www.youtube.com/watch?v={video_id}"
|
| 114 |
results.append((title, url_link))
|
| 115 |
return results
|
| 116 |
+
|
| 117 |
except Exception:
|
| 118 |
return []
|
| 119 |
|
|
|
|
| 130 |
for rank, (channel, score) in enumerate(ranked_channels, 1):
|
| 131 |
reason = generate_reason(product_desc, channel)
|
| 132 |
|
| 133 |
+
# Build search query to get top influencers/posts
|
| 134 |
+
query = f"top {channel} influencers, trending posts, popular {channel} content for {product_desc}"
|
| 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:
|
| 140 |
links = serpapi_search(query, site="youtube.com", num=3, region_code=region_code)
|
| 141 |
else:
|
|
|
|
| 145 |
|
| 146 |
# Format links as clickable URLs
|
| 147 |
if not links:
|
| 148 |
+
formatted_links = ["- ⚠️ No relevant influencers or posts found"]
|
| 149 |
else:
|
| 150 |
formatted_links = [f"- <a href='{url}' target='_blank'>{title}</a>" for title, url in links]
|
| 151 |
|