Spaces:
Running
Running
Gaurav vashistha commited on
Commit ·
1a0a513
1
Parent(s): bc73634
Automation: Implement AI fallback for SEO keyword generation
Browse files
main.py
CHANGED
|
@@ -58,6 +58,31 @@ async def generate_catalog(file: UploadFile = File(...)):
|
|
| 58 |
query = f"{visual_data.get('main_color', '')} {visual_data.get('product_type', 'product')}"
|
| 59 |
seo_keywords = memory_agent.retrieve_keywords(query)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
print("▶️ Writing Listing...")
|
| 62 |
listing = writer_agent.write_listing(visual_data, seo_keywords)
|
| 63 |
|
|
|
|
| 58 |
query = f"{visual_data.get('main_color', '')} {visual_data.get('product_type', 'product')}"
|
| 59 |
seo_keywords = memory_agent.retrieve_keywords(query)
|
| 60 |
|
| 61 |
+
# 2b. AI Fallback: Generate keywords if Pinecone returns empty
|
| 62 |
+
if not seo_keywords:
|
| 63 |
+
print("🤖 Database empty. Using AI Fallback for SEO keywords.")
|
| 64 |
+
try:
|
| 65 |
+
fallback_prompt = (
|
| 66 |
+
f"The internal keyword database is empty for this product. "
|
| 67 |
+
f"Based on these visual features: {json.dumps(visual_data)}, "
|
| 68 |
+
f"generate a list of 10 high-converting e-commerce SEO tags "
|
| 69 |
+
f"and return them as a JSON array of strings. Return ONLY the JSON array."
|
| 70 |
+
)
|
| 71 |
+
fallback_response = visual_agent.client.models.generate_content(
|
| 72 |
+
model="gemini-2.5-flash",
|
| 73 |
+
contents=fallback_prompt
|
| 74 |
+
)
|
| 75 |
+
import re
|
| 76 |
+
match = re.search(r'\[.*\]', fallback_response.text, re.DOTALL)
|
| 77 |
+
if match:
|
| 78 |
+
seo_keywords = json.loads(match.group(0))
|
| 79 |
+
else:
|
| 80 |
+
seo_keywords = [tag.strip() for tag in fallback_response.text.split(",") if tag.strip()]
|
| 81 |
+
print(f"✅ AI Fallback generated {len(seo_keywords)} keywords.")
|
| 82 |
+
except Exception as fallback_err:
|
| 83 |
+
print(f"⚠️ AI Fallback also failed: {fallback_err}")
|
| 84 |
+
seo_keywords = []
|
| 85 |
+
|
| 86 |
print("▶️ Writing Listing...")
|
| 87 |
listing = writer_agent.write_listing(visual_data, seo_keywords)
|
| 88 |
|