Spaces:
Configuration error
Configuration error
Create optimizer.py
Browse files- optimizer.py +61 -0
optimizer.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
|
| 4 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 5 |
+
|
| 6 |
+
def generate_blog_titles(keyword: str) -> str:
|
| 7 |
+
"""
|
| 8 |
+
Ask GPT-4: "Generate 3 catchy blog titles featuring 'keyword'
|
| 9 |
+
for better AI-model visibility."
|
| 10 |
+
"""
|
| 11 |
+
prompt = (
|
| 12 |
+
f"Generate 3 catchy blog titles featuring '{keyword}', "
|
| 13 |
+
"focused on personal branding and visibility in AI models:"
|
| 14 |
+
)
|
| 15 |
+
resp = openai.ChatCompletion.create(
|
| 16 |
+
model="gpt-4",
|
| 17 |
+
messages=[{"role": "user", "content": prompt}]
|
| 18 |
+
)
|
| 19 |
+
return resp.choices[0].message.content
|
| 20 |
+
|
| 21 |
+
def generate_jsonld_schema(keyword: str) -> str:
|
| 22 |
+
"""
|
| 23 |
+
Ask GPT-4: "Create a JSON-LD 'Person' schema snippet for 'keyword'."
|
| 24 |
+
"""
|
| 25 |
+
prompt = (
|
| 26 |
+
f"Create a JSON-LD 'Person' schema snippet for '{keyword}', "
|
| 27 |
+
"including name, description, and URL."
|
| 28 |
+
)
|
| 29 |
+
resp = openai.ChatCompletion.create(
|
| 30 |
+
model="gpt-4",
|
| 31 |
+
messages=[{"role": "user", "content": prompt}]
|
| 32 |
+
)
|
| 33 |
+
return resp.choices[0].message.content
|
| 34 |
+
|
| 35 |
+
def rewrite_prompt(keyword: str) -> str:
|
| 36 |
+
"""
|
| 37 |
+
Ask GPT-4: "Rewrite 'Tell me about {keyword}' "
|
| 38 |
+
"to sound more authoritative and AI-SEO-friendly."
|
| 39 |
+
"""
|
| 40 |
+
prompt = (
|
| 41 |
+
f"Rewrite the prompt 'Tell me about {keyword}' "
|
| 42 |
+
"to sound more authoritative and AI-SEO-friendly."
|
| 43 |
+
)
|
| 44 |
+
resp = openai.ChatCompletion.create(
|
| 45 |
+
model="gpt-4",
|
| 46 |
+
messages=[{"role": "user", "content": prompt}]
|
| 47 |
+
)
|
| 48 |
+
return resp.choices[0].message.content
|
| 49 |
+
|
| 50 |
+
def get_optimizations(keyword: str) -> dict:
|
| 51 |
+
"""
|
| 52 |
+
Returns a dict containing:
|
| 53 |
+
- blog_titles (string)
|
| 54 |
+
- json_ld (string)
|
| 55 |
+
- rewritten_prompt (string)
|
| 56 |
+
"""
|
| 57 |
+
return {
|
| 58 |
+
"blog_titles": generate_blog_titles(keyword),
|
| 59 |
+
"json_ld": generate_jsonld_schema(keyword),
|
| 60 |
+
"rewritten_prompt": rewrite_prompt(keyword)
|
| 61 |
+
}
|