AI-Blog-Writer / app.py
wangoes-dev's picture
Update app.py
b6cdd20 verified
import gradio as gr
from langchain_groq import ChatGroq
from langchain.schema import SystemMessage, HumanMessage
import os
# Securely fetch API Key from environment variable
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
raise ValueError("⚠️ GROQ_API_KEY is missing! Set it in your environment variables.")
# Initialize the Groq Model
llm = ChatGroq(
temperature=0,
groq_api_key=GROQ_API_KEY,
model_name="llama3-8b-8192"
)
def blog_generator(Blog_Topic, Blog_Keyword, Blog_Tone, Blog_Numberofwords, Blog_Target_audience):
"""Generate a blog using Groq AI"""
system_prompt = f"""
You are an expert blog writer. Follow these instructions:
1. Write a blog on: {Blog_Topic}.
2. Include keywords: {Blog_Keyword}.
3. Use tone: {Blog_Tone}.
4. Limit to {Blog_Numberofwords} words.
5. Target audience: {Blog_Target_audience}.
"""
user_prompt = f"Write a blog on {Blog_Topic}, with keywords {Blog_Keyword}, tone {Blog_Tone}, {Blog_Numberofwords} words, for audience {Blog_Target_audience}."
response = llm.invoke([SystemMessage(content=system_prompt), HumanMessage(content=user_prompt)])
return response.content
# Gradio Interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# πŸ“ AI Blog Generator")
with gr.Row():
topic = gr.Textbox(label="Blog Topic", placeholder="Enter the topic of your blog...")
keyword = gr.Textbox(label="Keywords (comma-separated)", placeholder="e.g., AI, Blogging, Technology")
with gr.Row():
tone = gr.Dropdown(["Formal", "Casual", "Professional"], label="Blog Tone", value="Professional")
words = gr.Slider(100, 1000, step=50, label="Number of Words", value=500)
audience = gr.Dropdown(["General Public", "Tech Enthusiasts", "Business Professionals", "Students"], label="Target Audience", value="General Public")
generate_btn = gr.Button("Generate Blog", variant="primary")
output = gr.Textbox(label="Generated Blog", lines=10, placeholder="Your generated blog will appear here...")
generate_btn.click(blog_generator, inputs=[topic, keyword, tone, words, audience], outputs=output)
demo.launch()