msaifee commited on
Commit
309ec75
Β·
1 Parent(s): 33f56e0

Refactoring

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -8,13 +8,6 @@ from langchain_core.messages import AIMessage, HumanMessage
8
  from typing import Annotated, List
9
 
10
 
11
- # Load environment variables
12
- load_dotenv()
13
-
14
- # Set up Groq client
15
- os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
16
- llm = ChatGroq(model="qwen-2.5-32b")
17
-
18
  # Define BlogState TypedDict
19
  class BlogState(TypedDict):
20
  topic: str
@@ -31,7 +24,10 @@ if 'graph' not in st.session_state:
31
  if 'graph_image' not in st.session_state:
32
  st.session_state.graph_image = None
33
 
34
- def init_graph():
 
 
 
35
  builder = StateGraph(BlogState)
36
 
37
  builder.add_node("title_generator", generate_title)
@@ -59,7 +55,7 @@ def generate_title(state: BlogState):
59
  - Between 6-12 words"""
60
 
61
  with st.status("πŸš€ Generating Titles..."):
62
- response = llm.invoke(prompt)
63
  state["title"] = response.content.split("\n")[0].strip('"')
64
  st.write(f"Selected title: **{state['title']}**")
65
  return state
@@ -74,7 +70,7 @@ def generate_content(state: BlogState):
74
  Style: Professional yet conversational (Flesch-Kincaid 60-70). Use markdown formatting"""
75
 
76
  with st.status("πŸ“ Generating Content..."):
77
- response = llm.invoke(prompt)
78
  state["blog_content"].append(AIMessage(content=response.content))
79
  st.markdown(response.content)
80
  return state
@@ -89,7 +85,7 @@ def review_content(state: BlogState):
89
  Provide specific improvement suggestions. Content:\n{content}"""
90
 
91
  with st.status("πŸ” Reviewing Content..."):
92
- feedback = llm.invoke(prompt)
93
  state["reviewed_content"].append(HumanMessage(content=feedback.content))
94
  st.write(feedback.content)
95
  return state
@@ -104,7 +100,7 @@ def evaluate_content(state: BlogState):
104
  Answer only Pass or Fail:"""
105
 
106
  with st.status("βœ… Evaluating Quality..."):
107
- response = llm.invoke(prompt)
108
  verdict = response.content.strip().upper()
109
  state["is_blog_ready"] = "Pass" if "PASS" in verdict else "Fail"
110
  state["reviewed_content"].append(AIMessage(
@@ -129,7 +125,9 @@ with st.sidebar:
129
  st.subheader("Configuration")
130
 
131
  # Groq API Key Input
132
- api_key = st.session_state["GROQ_API_KEY"] = st.text_input("Groq API Key",type="password")
 
 
133
  # Validate API key
134
  if not api_key:
135
  st.warning("⚠️ Please enter your GROQ API key to proceed. Don't have? refer : https://console.groq.com/keys ")
@@ -143,16 +141,18 @@ topic = st.text_input("Enter your blog topic:", placeholder="Generative AI in He
143
  generate_btn = st.button("Generate Blog Post")
144
 
145
  if generate_btn:
 
 
 
146
 
147
  if not topic:
148
  st.error("Please enter a blog topic!")
149
  st.stop()
150
 
151
  try:
152
- st.session_state.llm = ChatGroq(model="qwen-2.5-32b", groq_api_key=api_key)
153
 
154
  # Initialize and run graph
155
- st.session_state.graph = init_graph()
156
  st.session_state.blog_state = BlogState(
157
  topic=topic,
158
  title="",
 
8
  from typing import Annotated, List
9
 
10
 
 
 
 
 
 
 
 
11
  # Define BlogState TypedDict
12
  class BlogState(TypedDict):
13
  topic: str
 
24
  if 'graph_image' not in st.session_state:
25
  st.session_state.graph_image = None
26
 
27
+ def init_graph(api_key: str):
28
+
29
+ st.session_state.llm = ChatGroq(model="qwen-2.5-32b", api_key=api_key)
30
+
31
  builder = StateGraph(BlogState)
32
 
33
  builder.add_node("title_generator", generate_title)
 
55
  - Between 6-12 words"""
56
 
57
  with st.status("πŸš€ Generating Titles..."):
58
+ response = st.session_state.llm.invoke(prompt)
59
  state["title"] = response.content.split("\n")[0].strip('"')
60
  st.write(f"Selected title: **{state['title']}**")
61
  return state
 
70
  Style: Professional yet conversational (Flesch-Kincaid 60-70). Use markdown formatting"""
71
 
72
  with st.status("πŸ“ Generating Content..."):
73
+ response = st.session_state.llm.invoke(prompt)
74
  state["blog_content"].append(AIMessage(content=response.content))
75
  st.markdown(response.content)
76
  return state
 
85
  Provide specific improvement suggestions. Content:\n{content}"""
86
 
87
  with st.status("πŸ” Reviewing Content..."):
88
+ feedback = st.session_state.llm.invoke(prompt)
89
  state["reviewed_content"].append(HumanMessage(content=feedback.content))
90
  st.write(feedback.content)
91
  return state
 
100
  Answer only Pass or Fail:"""
101
 
102
  with st.status("βœ… Evaluating Quality..."):
103
+ response = st.session_state.llm.invoke(prompt)
104
  verdict = response.content.strip().upper()
105
  state["is_blog_ready"] = "Pass" if "PASS" in verdict else "Fail"
106
  state["reviewed_content"].append(AIMessage(
 
125
  st.subheader("Configuration")
126
 
127
  # Groq API Key Input
128
+ api_key = st.text_input("Groq API Key:",
129
+ type="password",
130
+ value=os.getenv("GROQ_API_KEY", ""))
131
  # Validate API key
132
  if not api_key:
133
  st.warning("⚠️ Please enter your GROQ API key to proceed. Don't have? refer : https://console.groq.com/keys ")
 
141
  generate_btn = st.button("Generate Blog Post")
142
 
143
  if generate_btn:
144
+ if not api_key:
145
+ st.error("Please provide a Groq API key in the sidebar!")
146
+ st.stop()
147
 
148
  if not topic:
149
  st.error("Please enter a blog topic!")
150
  st.stop()
151
 
152
  try:
 
153
 
154
  # Initialize and run graph
155
+ st.session_state.graph = init_graph(api_key)
156
  st.session_state.blog_state = BlogState(
157
  topic=topic,
158
  title="",