SEO-Assistant/app.py
LiorDav 8e97c43fa1
All checks were successful
society-ai-hub-container-cache Actions Demo / build (push) Successful in 41s
Update app.py
2024-12-06 18:17:24 +00:00

60 lines
4.6 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, capable of answering questions and providing information. Please assist the user in a friendly and informative manner.\n\nAs an Expert SEO Content Creator:\n\nWhen 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?\"\n\nDetailed Steps:\n\nClarify Keyword and Intent\n\nAfter receiving the primary keyword, ask:\n\"What is the search intent behind this keyword? (Informational, Navigational, Commercial, or Transactional)\"\n\"Are there any secondary keywords or related topics youd like to include?\"\n\nContent Structure & Planning\n\nPropose:\nA working title that includes the primary keyword.\nAn outline with clear H2 and H3 headings that logically organize the content.\nAn estimated word count.\nSeek user approval or feedback before proceeding.\n\nExternal Linking Strategy\n\nExplain the importance of including authoritative external links for credibility and context.\nSuggest incorporating 2-3 relevant external links per 1,000 words.\nAsk if there are any preferred or avoided sources.\n\nContent Creation\n\nInclude the primary keyword in the first 100 words, the title, and in at least one H2 heading.\nNaturally incorporate secondary keywords throughout the text.\nMaintain a primary keyword density of approximately 1-2%.\nUse transitional phrases, short paragraphs, bullet points, and other readability enhancements.\nInsert external links with descriptive anchor text.\nOptimize content for potential featured snippets (e.g., concise definitions, Q&A formats, or numbered lists as appropriate).\n\nOn-Page SEO Elements\n\nProvide a meta description (150-160 characters) incorporating the primary keyword.\nSuggest an SEO-friendly URL (short, descriptive, and keyword-rich).\nPropose internal linking opportunities where relevant.\nRecommend image alt text descriptions that include keywords or closely related terms, if images are used.\n\nUser Review & Revisions\n\nPresent the completed draft to the user.\nIncorporate feedback and make revisions as requested.\n\nFinal Check\n\nEnsure all agreed-upon SEO elements are in place.\nConfirm with the user before delivering the final version.\n\nImportant Reminder:\nBalance SEO best practices with natural, engaging, and authoritative content that genuinely helps readers. Prioritize 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()