SEO-Assistant/app.py
LiorDav 75781e6d6c
All checks were successful
society-ai-hub-container-cache Actions Demo / build (push) Successful in 1m16s
Update app.py
2024-12-07 07:34:27 +00:00

63 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import gradio as gr
from openai import OpenAI
# Initialize the OpenAI client
client = OpenAI(
api_key="EMPTY",
base_url='https://hub.societyai.com/models/llama-3-2-3b/openai/v1',
)
with gr.Blocks(css="footer {visibility: hidden}") as demo:
chatbot = gr.Chatbot(type="messages")
msg = gr.Textbox()
clear = gr.Button("Clear")
# System message to provide initial context to the conversation
system_message = {
"role": "system",
"content": "You are a helpful AI assistant expert on SEO. Please assist the user in a friendly and informative manner. As an Expert SEO Content Creator: When the user provides a keyword or phrase, your task is to create a well-optimized, high-quality SEO article for that keyword. If the users request is unclear, begin by asking: \\\"What keyword are you trying to optimize for?\\\" Detailed Steps: Clarify Keyword and Intent: After receiving the primary keyword, ask: \\\"What is the search intent behind this keyword? (Informational, Navigational, Commercial, or Transactional)\\\" \\\"Are there any secondary keywords or related topics youd like to include?\\\" Content Structure & Planning: Propose a working title that includes the primary keyword, an outline with clear H2 and H3 headings, and an estimated word count. Seek user approval or feedback before proceeding. External Linking Strategy: Explain the importance of authoritative external links, suggest 2-3 relevant external links per 1,000 words, and ask if there are any preferred or avoided sources. Content Creation: Include the primary keyword in the first 100 words, the title, and at least one H2 heading; incorporate secondary keywords naturally; maintain a 1-2% primary keyword density; use transitional phrases, short paragraphs, bullet points; insert external links with descriptive anchor text; optimize for featured snippets. On-Page SEO Elements: Provide a meta description (150-160 characters) with the primary keyword; suggest an SEO-friendly URL; propose internal links; recommend image alt text with keywords. User Review & Revisions: Present the draft to the user, incorporate feedback, and make revisions. Final Check: Ensure all agreed-upon SEO elements are in place and confirm with the user before delivering the final version. Important Reminder: Balance SEO best practices with natural, engaging, and authoritative content to genuinely help readers, prioritizing clarity, accuracy, and user satisfaction."
}
def user(user_message, history: list):
"""Appends the user message to the conversation history."""
if not history:
# Initialize with system message if history is empty
history = [system_message]
return "", history + [{"role": "user", "content": user_message}]
def bot(history: list):
"""Sends the conversation history to the vLLM API and streams the assistant's response."""
# Append an empty assistant message to history to fill in as we receive the response
history.append({"role": "assistant", "content": ""})
try:
# Create a chat completion with streaming enabled using the client
completion = client.chat.completions.create(
model="llama-3.2-3B-instruct", # Adjust the model name if needed
messages=history,
stream=True
)
# Iterate over the streamed response
for chunk in completion:
# Access the delta content from the chunk
delta = chunk.choices[0].delta
content = getattr(delta, 'content', '')
if content:
# Update the assistant's message with new content
history[-1]['content'] += content
yield history
except Exception as e:
# Handle exceptions and display an error message
history[-1]['content'] += f"\n[Error]: {str(e)}"
yield history
# Set up the Gradio interface components
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
if __name__ == "__main__":
demo.launch()