Update app.py
Browse files
app.py
CHANGED
@@ -4,78 +4,95 @@ from crew import *
|
|
4 |
import os
|
5 |
import base64
|
6 |
|
7 |
-
st.set_page_config(page_title="Multi-Agent Essay Writer", page_icon="
|
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 |
with st.sidebar:
|
40 |
-
st.
|
41 |
-
st.
|
42 |
-
"
|
43 |
-
"
|
44 |
-
|
45 |
-
|
46 |
-
openai_key = st.secrets.get("OPENAI_API_KEY", "")
|
47 |
|
48 |
st.divider()
|
49 |
-
|
50 |
-
# Reference section
|
51 |
-
st.subheader("📖 References:")
|
52 |
st.markdown(
|
53 |
-
|
54 |
unsafe_allow_html=True
|
55 |
)
|
56 |
|
57 |
# Initialize agents function
|
58 |
def initialize_agents():
|
59 |
-
if not
|
60 |
st.error("⚠️ OpenAI API key is missing! Please provide a valid key through Hugging Face Secrets.")
|
61 |
st.session_state.chat_active = True
|
62 |
return None
|
63 |
|
64 |
-
os.environ["OPENAI_API_KEY"] =
|
65 |
try:
|
66 |
if "app" in st.session_state and st.session_state.app is not None:
|
67 |
return st.session_state.app # Prevent re-initialization
|
68 |
|
69 |
essay_writer = EssayWriter().graph
|
70 |
st.session_state.chat_active = False
|
71 |
-
|
72 |
-
st.markdown(
|
73 |
-
"<div style='text-align: center; padding: 10px; margin-bottom: 20px; "
|
74 |
-
"background-color: #d4edda; color: #155724; border-radius: 8px;'>"
|
75 |
-
"<b>✅ Agents successfully initialized!</b></div>",
|
76 |
-
unsafe_allow_html=True
|
77 |
-
)
|
78 |
|
|
|
|
|
79 |
return essay_writer
|
80 |
except Exception as e:
|
81 |
st.error(f"❌ Error initializing agents: {e}")
|
@@ -99,24 +116,9 @@ def generate_response(topic, length):
|
|
99 |
|
100 |
return app.invoke(input={"topic": topic, "length": length})
|
101 |
|
102 |
-
# **NEW:
|
103 |
-
st.markdown(
|
104 |
-
|
105 |
-
<div style="text-align: center; padding: 15px; background-color: #f8f9fa;
|
106 |
-
border-radius: 10px; box-shadow: 2px 2px 10px rgba(0,0,0,0.1);">
|
107 |
-
<h3 style="color: #333;">✍️ Set Essay Length</h3>
|
108 |
-
</div>
|
109 |
-
""",
|
110 |
-
unsafe_allow_html=True
|
111 |
-
)
|
112 |
-
|
113 |
-
essay_length = st.number_input(
|
114 |
-
"Enter the word count for the essay:",
|
115 |
-
min_value=150,
|
116 |
-
max_value=350,
|
117 |
-
value=250,
|
118 |
-
step=50
|
119 |
-
)
|
120 |
|
121 |
# Display chat messages from the session
|
122 |
for message in st.session_state.messages:
|
@@ -128,7 +130,7 @@ if topic := st.chat_input(placeholder="📝 Ask a question or provide an essay t
|
|
128 |
st.chat_message("user").markdown(topic)
|
129 |
st.session_state.messages.append({"role": "user", "content": topic})
|
130 |
|
131 |
-
with st.spinner("⏳
|
132 |
response = generate_response(topic, essay_length) # **Pass user-defined length**
|
133 |
|
134 |
# Handle the assistant's response
|
@@ -157,19 +159,14 @@ if topic := st.chat_input(placeholder="📝 Ask a question or provide an essay t
|
|
157 |
st.markdown(response["response"])
|
158 |
st.session_state.messages.append({"role": "assistant", "content": response["response"]})
|
159 |
|
160 |
-
#
|
161 |
-
st.markdown("---")
|
162 |
st.markdown(
|
163 |
"""
|
164 |
-
<div
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
<a href="https://github.com/mesutdmn/Autonomous-Multi-Agent-Systems-with-CrewAI-Essay-Writer/tree/main"
|
169 |
-
target="_blank" style="color: #007BFF; text-decoration: none;">
|
170 |
-
CrewAI Essay Writer
|
171 |
-
</a>
|
172 |
</div>
|
173 |
""",
|
174 |
-
unsafe_allow_html=True
|
175 |
)
|
|
|
4 |
import os
|
5 |
import base64
|
6 |
|
7 |
+
st.set_page_config(page_title="Multi-Agent Essay Writer", page_icon="🤖")
|
8 |
+
|
9 |
+
# CSS for Better Styling
|
10 |
+
st.markdown("""
|
11 |
+
<style>
|
12 |
+
/* Center align everything */
|
13 |
+
.center-text {
|
14 |
+
text-align: center;
|
15 |
+
}
|
16 |
+
|
17 |
+
/* Remove excessive spacing */
|
18 |
+
.compact {
|
19 |
+
margin-bottom: 10px;
|
20 |
+
}
|
21 |
+
|
22 |
+
/* Styling for banners */
|
23 |
+
.success-banner {
|
24 |
+
text-align: center;
|
25 |
+
padding: 10px;
|
26 |
+
background-color: #d4edda;
|
27 |
+
color: #155724;
|
28 |
+
border-radius: 8px;
|
29 |
+
font-weight: bold;
|
30 |
+
margin-bottom: 15px;
|
31 |
+
}
|
32 |
+
|
33 |
+
/* Styling for input container */
|
34 |
+
.input-container {
|
35 |
+
padding: 15px;
|
36 |
+
background-color: #ffffff;
|
37 |
+
border-radius: 8px;
|
38 |
+
box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
|
39 |
+
text-align: center;
|
40 |
+
margin-bottom: 20px;
|
41 |
+
}
|
42 |
+
|
43 |
+
/* Styling for acknowledgment section */
|
44 |
+
.acknowledgment {
|
45 |
+
text-align: center;
|
46 |
+
padding: 10px;
|
47 |
+
background-color: #ffffff;
|
48 |
+
border-radius: 8px;
|
49 |
+
border: 1px solid #ddd;
|
50 |
+
font-size: 14px;
|
51 |
+
margin-top: 20px;
|
52 |
+
}
|
53 |
+
|
54 |
+
/* Adjust sidebar */
|
55 |
+
.sidebar-info {
|
56 |
+
padding: 10px;
|
57 |
+
background-color: #f8f9fa;
|
58 |
+
border-radius: 8px;
|
59 |
+
}
|
60 |
+
</style>
|
61 |
+
""", unsafe_allow_html=True)
|
62 |
+
|
63 |
+
# Sidebar
|
64 |
with st.sidebar:
|
65 |
+
st.markdown("📌 **About:**", unsafe_allow_html=True)
|
66 |
+
st.markdown(
|
67 |
+
"<div class='sidebar-info'>"
|
68 |
+
"<b>🔹 This app uses the 'gpt-4o-mini-2024-07-18' model.</b><br>"
|
69 |
+
"🔹 Writing essays may take approximately 1-2 minutes."
|
70 |
+
"</div>", unsafe_allow_html=True)
|
|
|
71 |
|
72 |
st.divider()
|
73 |
+
st.markdown("📖 **References:**")
|
|
|
|
|
74 |
st.markdown(
|
75 |
+
"[1. Multi-Agent System with CrewAI and LangChain](https://discuss.streamlit.io/t/new-project-i-have-build-a-multi-agent-system-with-crewai-and-langchain/84002)",
|
76 |
unsafe_allow_html=True
|
77 |
)
|
78 |
|
79 |
# Initialize agents function
|
80 |
def initialize_agents():
|
81 |
+
if not st.secrets.get("OPENAI_API_KEY"):
|
82 |
st.error("⚠️ OpenAI API key is missing! Please provide a valid key through Hugging Face Secrets.")
|
83 |
st.session_state.chat_active = True
|
84 |
return None
|
85 |
|
86 |
+
os.environ["OPENAI_API_KEY"] = st.secrets.get("OPENAI_API_KEY")
|
87 |
try:
|
88 |
if "app" in st.session_state and st.session_state.app is not None:
|
89 |
return st.session_state.app # Prevent re-initialization
|
90 |
|
91 |
essay_writer = EssayWriter().graph
|
92 |
st.session_state.chat_active = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
+
# Success message
|
95 |
+
st.markdown("<div class='success-banner'>✅ Agents successfully initialized!</div>", unsafe_allow_html=True)
|
96 |
return essay_writer
|
97 |
except Exception as e:
|
98 |
st.error(f"❌ Error initializing agents: {e}")
|
|
|
116 |
|
117 |
return app.invoke(input={"topic": topic, "length": length})
|
118 |
|
119 |
+
# **NEW: Clean & Centered Input Section**
|
120 |
+
st.markdown("<div class='input-container'><h3>✍️ Set Essay Length</h3></div>", unsafe_allow_html=True)
|
121 |
+
essay_length = st.number_input("Enter the word count for the essay:", min_value=150, max_value=350, value=250, step=50)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
# Display chat messages from the session
|
124 |
for message in st.session_state.messages:
|
|
|
130 |
st.chat_message("user").markdown(topic)
|
131 |
st.session_state.messages.append({"role": "user", "content": topic})
|
132 |
|
133 |
+
with st.spinner("⏳ Generating your essay..."):
|
134 |
response = generate_response(topic, essay_length) # **Pass user-defined length**
|
135 |
|
136 |
# Handle the assistant's response
|
|
|
159 |
st.markdown(response["response"])
|
160 |
st.session_state.messages.append({"role": "assistant", "content": response["response"]})
|
161 |
|
162 |
+
# Acknowledgment Section
|
|
|
163 |
st.markdown(
|
164 |
"""
|
165 |
+
<div class='acknowledgment'>
|
166 |
+
📌 <b>Acknowledgment:</b> This project is based on Mesut Duman's work.<br><br>
|
167 |
+
⚒️ Source: <a href="https://github.com/mesutdmn/Autonomous-Multi-Agent-Systems-with-CrewAI-Essay-Writer/tree/main"
|
168 |
+
target="_blank" style="color: #007BFF; text-decoration: none;">CrewAI Essay Writer</a>
|
|
|
|
|
|
|
|
|
169 |
</div>
|
170 |
""",
|
171 |
+
unsafe_allow_html=True
|
172 |
)
|