Spaces:
Running
Running
File size: 2,189 Bytes
6a7fdb0 af23695 6a7fdb0 b6cdd20 6a7fdb0 b6cdd20 6a7fdb0 b6cdd20 6a7fdb0 b6cdd20 6a7fdb0 b6cdd20 6a7fdb0 b6cdd20 6a7fdb0 b6cdd20 6a7fdb0 b6cdd20 6a7fdb0 b6cdd20 |
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 |
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() |