Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from graph import EssayWriter
|
3 |
+
import os
|
4 |
+
import base64
|
5 |
+
|
6 |
+
st.set_page_config(page_title="Essay Writer Chat Bot", page_icon="🤖")
|
7 |
+
st.image("./media/cover.jpg", use_column_width=True)
|
8 |
+
|
9 |
+
button_html = f'''
|
10 |
+
<div style="display: flex; justify-content: center;">
|
11 |
+
<a href="https://buymeacoffee.com/mesutduman" 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 |
+
☕ Buy Me a Coffee
|
27 |
+
</button>
|
28 |
+
</a>
|
29 |
+
</div>
|
30 |
+
'''
|
31 |
+
|
32 |
+
if "messages" not in st.session_state:
|
33 |
+
st.session_state.messages = [{"role": "assistant", "content": "Hello!"}]
|
34 |
+
st.session_state.app = None
|
35 |
+
st.session_state.chat_active = True
|
36 |
+
|
37 |
+
with st.sidebar:
|
38 |
+
st.info(" * This app uses the OpenAI API to generate text, please provide your API key."
|
39 |
+
"\n\n * This app uses the 'gpt-4o-mini-2024-07-18' model. Cost effective and efficient."
|
40 |
+
"\n\n * If you don't have an API key, you can get one [here](https://platform.openai.com/signup)."
|
41 |
+
"\n\n * You can also find the source code for this app [here](https://github.com/mesutdmn/Autonomous-Multi-Agent-Systems-with-CrewAI-Essay-Writer)"
|
42 |
+
"\n\n * App keys are not stored or saved in any way."
|
43 |
+
"\n\n * Writing essay may take some time, please be patient. Approximately 1-2 minutes."
|
44 |
+
"\n\n * If you like this app, consider buying me a coffee ☕")
|
45 |
+
openai_key_input = st.text_input("OpenAI API Key", type="password")
|
46 |
+
developer_mode = st.checkbox("I don't have key, use developer's money 😓", value=False)
|
47 |
+
openai_key = openai_key_input or (st.secrets["OpenAI_API_KEY"] if developer_mode else "")
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
def initialize_agents():
|
53 |
+
os.environ["OPENAI_API_KEY"] = openai_key
|
54 |
+
essay_writer = EssayWriter().graph
|
55 |
+
|
56 |
+
if len(openai_key) < 1:
|
57 |
+
st.error("Please enter your OpenAI API key and Initialize the agents.")
|
58 |
+
|
59 |
+
st.session_state.chat_active = True
|
60 |
+
else:
|
61 |
+
st.success("Agents successfully initialized")
|
62 |
+
st.session_state.chat_active = False
|
63 |
+
|
64 |
+
return essay_writer
|
65 |
+
|
66 |
+
with st.sidebar:
|
67 |
+
if st.button("Initialize Agents", type="primary"):
|
68 |
+
st.session_state.app = initialize_agents()
|
69 |
+
st.divider()
|
70 |
+
st.markdown(button_html, unsafe_allow_html=True)
|
71 |
+
|
72 |
+
app = st.session_state.app
|
73 |
+
def generate_response(topic):
|
74 |
+
return app.invoke(input={"topic": topic})
|
75 |
+
|
76 |
+
|
77 |
+
for message in st.session_state.messages:
|
78 |
+
with st.chat_message(message["role"]):
|
79 |
+
st.markdown(message["content"], unsafe_allow_html=True)
|
80 |
+
|
81 |
+
if topic:= st.chat_input(placeholder="Ask a question", disabled=st.session_state.chat_active):
|
82 |
+
st.chat_message("user").markdown(topic)
|
83 |
+
|
84 |
+
st.session_state.messages.append({"role": "user", "content": topic})
|
85 |
+
with st.spinner("Thinking..."):
|
86 |
+
response = generate_response(topic)
|
87 |
+
|
88 |
+
with st.chat_message("assistant"):
|
89 |
+
if "pdf_name" in response:
|
90 |
+
with open(f"./{response['pdf_name']}", "rb") as file:
|
91 |
+
file_bytes = file.read()
|
92 |
+
b64 = base64.b64encode(file_bytes).decode()
|
93 |
+
href = f"<a href='data:application/octet-stream;base64,{b64}' download='{response['pdf_name']}'>Click here to download the PDF</a>"
|
94 |
+
|
95 |
+
st.markdown(f"{response['response']}: {href}", unsafe_allow_html=True)
|
96 |
+
st.session_state.messages.append({"role": "assistant", "content": f"{response['response']}: {href}"})
|
97 |
+
else:
|
98 |
+
st.markdown(response["response"])
|
99 |
+
st.session_state.messages.append({"role": "assistant", "content": response["response"]})
|