-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
104 lines (84 loc) · 3.4 KB
/
app.py
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
101
102
103
104
import streamlit as st
import openai
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation
import os
# Define your functions here (placeholders for now)
def extract_keywords(text):
# Placeholder for keyword extraction logic
return ["keyword1", "keyword2"]
def embed_semantic_context(text):
# Placeholder for embedding logic
return [0.1, 0.2, 0.3, 0.4]
def generate_optimized_content(prompt, api_key):
# openai.api_key = api_key
openai.api_key = st.secrets["OPENAI_API_KEY"]
response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
],
max_tokens=300,
)
return response.choices[0].message.content
def get_seo_advice(question, api_key):
# openai.api_key = api_key
openai.api_key = st.secrets["OPENAI_API_KEY"]
response = openai.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "You are a helpful assistant with expertise in SEO.",
},
{"role": "user", "content": question},
],
max_tokens=100,
)
return response.choices[0].message.content
# Initialize the LDA model and vectorizer
lda_model = LatentDirichletAllocation(n_components=5, random_state=42)
vectorizer = CountVectorizer(max_features=1000, stop_words="english")
# Streamlit app
st.title("AI-Powered SEO Optimization Tool")
st.sidebar.header("Optimize Content")
text = st.sidebar.text_area(
"Content:", "Generative AI can significantly enhance SEO strategies..."
)
if st.sidebar.button("Extract Keywords"):
keywords = extract_keywords(text)
st.write(f"Keywords: {', '.join(keywords)}")
if st.sidebar.button("Embed Semantic Context"):
embedding = embed_semantic_context(text)
st.write(f"Semantic Context Embedding: {embedding}")
if st.sidebar.button("Perform Topic Modeling"):
topic_dist = lda_model.transform(vectorizer.transform([text]))[0]
st.write(f"Topic Distribution: {topic_dist}")
st.sidebar.header("Generate Optimized Content")
prompt = st.sidebar.text_area(
"Content Generation Prompt:", "Write an SEO-optimized blog post about..."
)
if st.sidebar.button("Generate Content"):
# api_key = st.sidebar.text_input(st.secrets["OPENAI_API_KEY"], type="password")
generated_content = generate_optimized_content(prompt, st.secrets["OPENAI_API_KEY"])
st.write(f"Generated Content: {generated_content}")
# if api_key:
# generated_content = generate_optimized_content(prompt, st.secrets["OPENAI_API_KEY"])
# st.write(f"Generated Content: {generated_content}")
# else:
# st.write("Please enter your OpenAI API key.")
st.sidebar.header("Get SEO Advice")
question = st.sidebar.text_input(
"SEO Question:",
'How can I improve my website\'s SEO for the keyword "Generative AI"?',
)
if st.sidebar.button("Get Advice"):
# api_key = st.sidebar.text_input("OpenAI API Key", type="password")
advice = get_seo_advice(question, st.secrets["OPENAI_API_KEY"])
st.write(f"SEO Advice: {advice}")
# if api_key:
# advice = get_seo_advice(question, st.secrets["OPENAI_API_KEY"])
# st.write(f"SEO Advice: {advice}")
# else:
# st.write("Please enter your OpenAI API key.")