File size: 3,880 Bytes
774d59e
 
 
ecf661c
774d59e
ccee732
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
774d59e
 
ecf661c
 
 
774d59e
ecf661c
774d59e
 
 
 
 
ccee732
 
774d59e
 
 
 
 
 
 
 
 
 
ccee732
774d59e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccee732
774d59e
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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()