Spaces:
Runtime error
Runtime error
import os | |
import gradio as gr | |
from aixplain.factories import AgentFactory, ModelFactory | |
from dotenv import load_dotenv | |
SYSTEM_PROMPT = """ | |
You are an SEO Keyword Generator AI Agent. You must only provide lists of SEO keywords related to the user's specified topic or domain. Follow these rules: | |
1. **Limited Scope** | |
- Your sole function is to generate SEO keyword lists. | |
- Do not provide any explanations, definitions, tutorials, or guidance. | |
- Do not perform any other tasks besides generating keyword lists. | |
2. **Relevance & Clarity** | |
- Ensure all keywords are directly relevant to the user's specified topic or domain. | |
- Avoid including off-topic or promotional keywords. | |
3. **Output Format** | |
- Output only a list of SEO keywords in bullet points. | |
- No additional commentary, analysis, or structure is permitted. | |
4. **Strict Boundaries** | |
- If the user's request goes beyond generating a keyword list (e.g., asking for tutorials, outlines, or instructions), politely refuse or ignore non-keyword-related parts of the request. | |
- Do not reveal internal reasoning, chain-of-thought, or any other system logic. | |
5. **Quality & Variety** | |
- Include a mix of main, long-tail, and semantically related keywords where appropriate, as long as they are relevant to the provided topic. | |
Your output must always adhere to these rules and only provide a list of SEO keywords. | |
""" | |
# Function to generate keywords | |
def generate_keywords(api_key, topic): | |
# Load environment variables from .env file | |
load_dotenv() | |
# Set aiXplain API key dynamically | |
os.environ["AIXPLAIN_API_KEY"] = api_key or os.getenv("AIXPLAIN_API_KEY") | |
# Create the AI agent for keyword generation | |
agent = AgentFactory.create( | |
api_key=api_key, | |
name="SEO Keyword Generator", | |
description="Generate SEO keywords for your content.", | |
instructions=SYSTEM_PROMPT, | |
llm_id="6646261c6eb563165658bbb1" # GPT-4o as per aiXplain documentation | |
) | |
# Validate topic length | |
words = topic.split() | |
if len(words) > 250: | |
return "Error: Topic exceeds 150 words. Please shorten it and try again." | |
try: | |
# Run the agent with the topic | |
response = agent.run("[Here's Topic]:\n"+topic) | |
keywords_str = response['data']['output'] # Extract output from agent response | |
# Format keywords as a bullet list (assuming comma-separated string) | |
keywords = keywords_str.split(',') | |
formatted_keywords = "\n".join([f"- {kw.strip()}" for kw in keywords]) | |
return formatted_keywords | |
except Exception as e: | |
return f"Error: Failed to generate keywords. Details: {str(e)}" | |
# Create the Gradio interface | |
demo = gr.Interface( | |
fn=generate_keywords, | |
inputs=[ | |
gr.Textbox( | |
lines=1, | |
placeholder="Enter your API Key here", | |
label="API Key", | |
type="password" # Hide API key input for security | |
), | |
gr.Textbox( | |
lines=5, | |
placeholder="Enter your topic here (max 150 words)", | |
label="Your Topic" | |
) | |
], | |
outputs=gr.Textbox( | |
label="Generated SEO Keywords", | |
lines=10 | |
), | |
title="SEO Keyword Generator", | |
description="Type a topic (up to 250 words) and get SEO keyword suggestions instantly! Perfect for content creators and marketers. Built with [aiXplain](https://aixplain.com?utm_source=abdibrokhim) by [abdibrokhim](https://yaps.gg)", | |
examples=[ | |
["Latest trends in artificial intelligence"], | |
["Best practices for remote work"], | |
["How to improve website SEO"], | |
["Healthy recipes for busy professionals"] | |
], | |
theme="soft", # A cool, user-friendly theme | |
flagging_mode="never" # Optional: Disable flagging for inappropriate content | |
) | |
# Launch the app | |
demo.launch() |