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