Spaces:
Configuration error
Configuration error
Update optimizer.py
Browse files- optimizer.py +24 -34
optimizer.py
CHANGED
|
@@ -1,59 +1,49 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
| 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 |
-
|
| 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),
|
|
|
|
| 1 |
import os
|
| 2 |
+
import requests
|
| 3 |
|
| 4 |
+
def query_modal_gpt(prompt):
|
| 5 |
+
token_id = os.getenv("MODAL_TOKEN_ID")
|
| 6 |
+
token_secret = os.getenv("MODAL_TOKEN_SECRET")
|
| 7 |
+
|
| 8 |
+
url = "https://api.modal.com/gpt" # Update if Modal gives you another endpoint
|
| 9 |
+
headers = {
|
| 10 |
+
"Authorization": f"Bearer {token_id}:{token_secret}",
|
| 11 |
+
"Content-Type": "application/json"
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
data = {
|
| 15 |
+
"model": "gpt-4",
|
| 16 |
+
"messages": [
|
| 17 |
+
{"role": "system", "content": "You are a GEO scoring and optimization expert."},
|
| 18 |
+
{"role": "user", "content": prompt}
|
| 19 |
+
]
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
response = requests.post(url, headers=headers, json=data)
|
| 23 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 24 |
|
| 25 |
def generate_blog_titles(keyword: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
prompt = (
|
| 27 |
f"Generate 3 catchy blog titles featuring '{keyword}', "
|
| 28 |
"focused on personal branding and visibility in AI models:"
|
| 29 |
)
|
| 30 |
+
return query_modal_gpt(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
def generate_jsonld_schema(keyword: str) -> str:
|
|
|
|
|
|
|
|
|
|
| 33 |
prompt = (
|
| 34 |
f"Create a JSON-LD 'Person' schema snippet for '{keyword}', "
|
| 35 |
"including name, description, and URL."
|
| 36 |
)
|
| 37 |
+
return query_modal_gpt(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def rewrite_prompt(keyword: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
prompt = (
|
| 41 |
f"Rewrite the prompt 'Tell me about {keyword}' "
|
| 42 |
"to sound more authoritative and AI-SEO-friendly."
|
| 43 |
)
|
| 44 |
+
return query_modal_gpt(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def get_optimizations(keyword: str) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
return {
|
| 48 |
"blog_titles": generate_blog_titles(keyword),
|
| 49 |
"json_ld": generate_jsonld_schema(keyword),
|