Create interim.py
Browse files- lab/interim.py +139 -0
lab/interim.py
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from graph import EssayWriter, RouteQuery, GraphState
|
3 |
+
import os
|
4 |
+
import base64
|
5 |
+
|
6 |
+
st.set_page_config(page_title="Multi-Agent Essay Writer", page_icon="🤖🤖🤖✍️")
|
7 |
+
|
8 |
+
# Button HTML
|
9 |
+
button_html = f'''
|
10 |
+
<div style="display: flex; justify-content: center;">
|
11 |
+
<a href=" " target="_blank">
|
12 |
+
<button style="
|
13 |
+
background-color: #FFDD00;
|
14 |
+
border: none;
|
15 |
+
color: black;
|
16 |
+
padding: 10px 20px;
|
17 |
+
text-align: center;
|
18 |
+
text-decoration: none;
|
19 |
+
display: inline-block;
|
20 |
+
font-size: 16px;
|
21 |
+
margin: 4px 2px;
|
22 |
+
cursor: pointer;
|
23 |
+
border-radius: 10px;
|
24 |
+
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.2);
|
25 |
+
">
|
26 |
+
</button>
|
27 |
+
</a>
|
28 |
+
</div>
|
29 |
+
'''
|
30 |
+
|
31 |
+
# Initialize session state
|
32 |
+
if "messages" not in st.session_state:
|
33 |
+
st.session_state.messages = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
|
34 |
+
st.session_state.app = None
|
35 |
+
st.session_state.chat_active = True
|
36 |
+
|
37 |
+
# Sidebar with instructions and API key input
|
38 |
+
with st.sidebar:
|
39 |
+
st.subheader("About:")
|
40 |
+
st.info(
|
41 |
+
"\n\n* This app uses the 'gpt-4o-mini-2024-07-18' model."
|
42 |
+
"\n\n* Writing essays may take some time, approximately 1-2 minutes."
|
43 |
+
)
|
44 |
+
|
45 |
+
openai_key = st.secrets.get("OPENAI_API_KEY", "")
|
46 |
+
|
47 |
+
st.divider()
|
48 |
+
|
49 |
+
# Reference section
|
50 |
+
st.subheader("References:")
|
51 |
+
st.markdown(
|
52 |
+
'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)',
|
53 |
+
unsafe_allow_html=True
|
54 |
+
)
|
55 |
+
|
56 |
+
# Initialize agents function
|
57 |
+
def initialize_agents():
|
58 |
+
if not openai_key:
|
59 |
+
st.error("OpenAI API key is missing! Please provide a valid key through Hugging Face Secrets.")
|
60 |
+
st.session_state.chat_active = True
|
61 |
+
return None
|
62 |
+
|
63 |
+
os.environ["OPENAI_API_KEY"] = openai_key
|
64 |
+
try:
|
65 |
+
if "app" in st.session_state and st.session_state.app is not None:
|
66 |
+
return st.session_state.app # Prevent re-initialization
|
67 |
+
|
68 |
+
essay_writer = EssayWriter().graph
|
69 |
+
st.success("Agents successfully initialized!")
|
70 |
+
st.session_state.chat_active = False
|
71 |
+
return essay_writer
|
72 |
+
except Exception as e:
|
73 |
+
st.error(f"Error initializing agents: {e}")
|
74 |
+
st.session_state.chat_active = True
|
75 |
+
return None
|
76 |
+
|
77 |
+
# Automatically initialize agents on app load
|
78 |
+
if "app" not in st.session_state or st.session_state.app is None:
|
79 |
+
st.session_state.app = initialize_agents()
|
80 |
+
|
81 |
+
app = st.session_state.app
|
82 |
+
|
83 |
+
# Function to invoke the agent and generate a response
|
84 |
+
def generate_response(topic):
|
85 |
+
return app.invoke(input={"topic": topic}) if app else {"response": "Error: Agents not initialized."}
|
86 |
+
|
87 |
+
# Display chat messages from the session
|
88 |
+
for message in st.session_state.messages:
|
89 |
+
with st.chat_message(message["role"]):
|
90 |
+
st.markdown(message["content"], unsafe_allow_html=True)
|
91 |
+
|
92 |
+
# Handle user input
|
93 |
+
if topic := st.chat_input(placeholder="Ask a question or provide an essay topic...", disabled=st.session_state.chat_active):
|
94 |
+
st.chat_message("user").markdown(topic)
|
95 |
+
st.session_state.messages.append({"role": "user", "content": topic})
|
96 |
+
|
97 |
+
with st.spinner("Thinking..."):
|
98 |
+
response = generate_response(topic)
|
99 |
+
|
100 |
+
# Handle the assistant's response
|
101 |
+
with st.chat_message("assistant"):
|
102 |
+
if "essay" in response: # Display essay preview and download link
|
103 |
+
st.markdown("### Essay Preview:")
|
104 |
+
st.markdown(f"#### {response['essay']['header']}")
|
105 |
+
st.markdown(response["essay"]["entry"])
|
106 |
+
for para in response["essay"]["paragraphs"]:
|
107 |
+
st.markdown(f"**{para['sub_header']}**")
|
108 |
+
st.markdown(para["paragraph"])
|
109 |
+
st.markdown("**Conclusion:**")
|
110 |
+
st.markdown(response["essay"]["conclusion"])
|
111 |
+
|
112 |
+
# Provide download link for the PDF
|
113 |
+
with open(response["pdf_name"], "rb") as pdf_file:
|
114 |
+
b64 = base64.b64encode(pdf_file.read()).decode()
|
115 |
+
href = f"<a href='data:application/octet-stream;base64,{b64}' download='{response['pdf_name']}'>Click here to download the PDF</a>"
|
116 |
+
st.markdown(href, unsafe_allow_html=True)
|
117 |
+
|
118 |
+
# Save the assistant's message to session state
|
119 |
+
st.session_state.messages.append(
|
120 |
+
{"role": "assistant", "content": "Here is your essay preview and the download link."}
|
121 |
+
)
|
122 |
+
else: # For other responses (e.g., general answers)
|
123 |
+
st.markdown(response["response"])
|
124 |
+
st.session_state.messages.append({"role": "assistant", "content": response["response"]})
|
125 |
+
|
126 |
+
# Add acknowledgment at the bottom
|
127 |
+
st.markdown("---")
|
128 |
+
st.markdown(
|
129 |
+
"""
|
130 |
+
<div style="text-align: center; font-size: 14px; color: #555;">
|
131 |
+
<strong>Acknowledgment:</strong> This project is based on Mesut Duman's work:
|
132 |
+
<a href="https://github.com/mesutdmn/Autonomous-Multi-Agent-Systems-with-CrewAI-Essay-Writer/tree/main"
|
133 |
+
target="_blank" style="color: #007BFF; text-decoration: none;">
|
134 |
+
CrewAI Essay Writer
|
135 |
+
</a>
|
136 |
+
</div>
|
137 |
+
""",
|
138 |
+
unsafe_allow_html=True,
|
139 |
+
)
|