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ggingFaceEndpoint
|
| 3 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
+
from langchain.schema import HumanMessage
|
| 5 |
+
from langgraph.graph import StateGraph, START, END
|
| 6 |
+
from typing import TypedDict
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Load environment variables from a .env file
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# --- Page Configuration ---
|
| 14 |
+
st.set_page_config(
|
| 15 |
+
page_title="AI Blog Writer",
|
| 16 |
+
page_icon="🤖",
|
| 17 |
+
layout="wide"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# --- API Key Management ---
|
| 21 |
+
# Make sure to create a .env file in the same directory with:
|
| 22 |
+
# GOOGLE_API_KEY="your_google_api_key"
|
| 23 |
+
# HUGGINGFACEHUB_API_TOKEN="your_huggingface_api_token"
|
| 24 |
+
google_api_key = os.getenv("GOOGLE_API_KEY")
|
| 25 |
+
huggingface_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 26 |
+
|
| 27 |
+
# --- LangGraph State Definition ---
|
| 28 |
+
class BlogState(TypedDict):
|
| 29 |
+
"""
|
| 30 |
+
Represents the state of our blog generation graph. It holds the data
|
| 31 |
+
that moves between the nodes of the graph.
|
| 32 |
+
"""
|
| 33 |
+
topic: str
|
| 34 |
+
blog_outline: str
|
| 35 |
+
final_blog: str
|
| 36 |
+
|
| 37 |
+
# --- Model Initialization ---
|
| 38 |
+
def initialize_models():
|
| 39 |
+
"""
|
| 40 |
+
Initializes and returns the language models.
|
| 41 |
+
Handles errors if API keys are missing.
|
| 42 |
+
"""
|
| 43 |
+
if not google_api_key:
|
| 44 |
+
st.error("Google API key is missing. Please set it in your .env file.")
|
| 45 |
+
return None, None
|
| 46 |
+
if not huggingface_token:
|
| 47 |
+
st.error("Hugging Face API token is missing. Please set it in your .env file.")
|
| 48 |
+
return None, None
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
gemini = ChatGoogleGenerativeAI(
|
| 52 |
+
model="gemini-1.5-flash",
|
| 53 |
+
temperature=1.0,
|
| 54 |
+
max_retries=2,
|
| 55 |
+
google_api_key=google_api_key,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
mistral_llm = HuggingFaceEndpoint(
|
| 59 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.2",
|
| 60 |
+
task="text-generation",
|
| 61 |
+
huggingfacehub_api_token=huggingface_token,
|
| 62 |
+
)
|
| 63 |
+
mistral = ChatHuggingFace(llm=mistral_llm)
|
| 64 |
+
return gemini, mistral
|
| 65 |
+
except Exception as e:
|
| 66 |
+
st.error(f"Failed to initialize models: {e}")
|
| 67 |
+
return None, None
|
| 68 |
+
|
| 69 |
+
gemini, mistral = initialize_models()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# --- LangGraph Node Functions ---
|
| 73 |
+
def outline_gen_node(state: BlogState) -> BlogState:
|
| 74 |
+
"""Node to generate the blog outline using the Mistral model."""
|
| 75 |
+
print("Executing outline generation node...")
|
| 76 |
+
with st.status("Generating blog outline...", expanded=True) as status:
|
| 77 |
+
response = mistral.invoke([
|
| 78 |
+
HumanMessage(content=f"Create a detailed blog outline on the topic: {state['topic']}. Be comprehensive and structure it logically for a compelling article.")
|
| 79 |
+
])
|
| 80 |
+
status.update(label="Outline generated!", state="complete")
|
| 81 |
+
state['blog_outline'] = response.content
|
| 82 |
+
return state
|
| 83 |
+
|
| 84 |
+
def blog_gen_node(state: BlogState) -> BlogState:
|
| 85 |
+
"""Node to generate the final blog post using the Gemini model."""
|
| 86 |
+
print("Executing blog generation node...")
|
| 87 |
+
blog_outline = state["blog_outline"]
|
| 88 |
+
with st.status("Writing the final blog post...", expanded=True) as status:
|
| 89 |
+
response = gemini.invoke([
|
| 90 |
+
HumanMessage(content=f"Write a detailed, engaging, and well-written blog post based on the following outline. Use markdown for formatting, including headings, bold text, and lists where appropriate. Make it ready for publishing.\n\n## Blog Outline\n{blog_outline}")
|
| 91 |
+
])
|
| 92 |
+
status.update(label="Blog post complete!", state="complete")
|
| 93 |
+
state["final_blog"] = response.content
|
| 94 |
+
return state
|
| 95 |
+
|
| 96 |
+
# --- Graph and Workflow Compilation ---
|
| 97 |
+
if gemini and mistral:
|
| 98 |
+
# Define the state graph
|
| 99 |
+
graph = StateGraph(BlogState)
|
| 100 |
+
|
| 101 |
+
# Add nodes to the graph
|
| 102 |
+
graph.add_node("outline_gen", outline_gen_node)
|
| 103 |
+
graph.add_node("blog_gen", blog_gen_node)
|
| 104 |
+
|
| 105 |
+
# Add edges to define the flow
|
| 106 |
+
graph.add_edge(START, "outline_gen")
|
| 107 |
+
graph.add_edge("outline_gen", "blog_gen")
|
| 108 |
+
graph.add_edge("blog_gen", END)
|
| 109 |
+
|
| 110 |
+
# Compile the graph into a runnable workflow
|
| 111 |
+
workflow = graph.compile()
|
| 112 |
+
else:
|
| 113 |
+
workflow = None
|
| 114 |
+
|
| 115 |
+
# --- Streamlit User Interface ---
|
| 116 |
+
st.title("🤖 AI Blog Writer")
|
| 117 |
+
st.markdown("Enter a topic and let two AI models collaborate to write a blog post. Mistral creates the outline, and Gemini writes the content.")
|
| 118 |
+
|
| 119 |
+
st.sidebar.header("How It Works")
|
| 120 |
+
st.sidebar.info(
|
| 121 |
+
"""
|
| 122 |
+
This app uses a **LangGraph** workflow to automate blog writing:
|
| 123 |
+
1. **You Enter a Topic**: Provide a subject for the blog post.
|
| 124 |
+
2. **Outline Generation**: A `Mistral-7B` model generates a detailed outline.
|
| 125 |
+
3. **Blog Generation**: Google's `Gemini` model writes the full blog post based on the outline.
|
| 126 |
+
4. **Final Output**: The final, formatted blog post is displayed.
|
| 127 |
+
"""
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
topic = st.text_input("Enter the blog topic here:", placeholder="e.g., The Impact of AI on Software Development")
|
| 131 |
+
|
| 132 |
+
if st.button("Generate Blog Post", disabled=(not workflow)):
|
| 133 |
+
if topic:
|
| 134 |
+
# Define the initial state for the graph
|
| 135 |
+
initial_state = BlogState(topic=topic, blog_outline="", final_blog="")
|
| 136 |
+
|
| 137 |
+
# Invoke the workflow
|
| 138 |
+
final_state = workflow.invoke(initial_state)
|
| 139 |
+
|
| 140 |
+
# Display the final blog
|
| 141 |
+
st.divider()
|
| 142 |
+
st.subheader("Your Generated Blog Post")
|
| 143 |
+
st.markdown(final_state['final_blog'])
|
| 144 |
+
else:
|
| 145 |
+
st.warning("Please enter a topic to generate a blog post.")
|
| 146 |
+
|
| 147 |
+
if not workflow:
|
| 148 |
+
st.error("Workflow could not be initialized. Please check your API keys and environment setup.")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,10 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.46.1
|
| 2 |
+
langchain==0.3.26
|
| 3 |
+
langchain-huggingface==0.3.0
|
| 4 |
+
langchain-community==0.3.27
|
| 5 |
+
sentence-transformers==5.0.0
|
| 6 |
+
transformers==4.53.0
|
| 7 |
+
python-dotenv==1.1.1
|
| 8 |
+
youtube-transcript-api==1.2.1
|
| 9 |
+
langchain_google_genai
|
| 10 |
+
langgraph
|