File size: 13,570 Bytes
01561b2
8dfe0d1
8033431
d528d5e
01561b2
7b514ad
ef75b1b
01561b2
 
390a76b
 
 
 
b8c9273
 
 
 
 
 
 
 
 
80fcc20
 
 
 
 
 
 
 
 
 
512b8e6
bb69e55
01561b2
44e6349
7a51495
6c78633
5ea8ff3
7a51495
bb69e55
 
 
 
 
 
 
904e7c3
bb69e55
 
 
5ea8ff3
bb69e55
 
 
01561b2
c0915c5
bb69e55
512b8e6
8033431
5de1a56
34c63e0
 
 
bb69e55
 
f0b58d7
d65790b
f0b58d7
 
 
34c63e0
b8d133e
6d07543
bb69e55
c43aa42
80fcc20
6d07543
 
bb69e55
37d4fdf
f9fb255
 
 
a1cef55
f9fb255
5ea8ff3
 
 
c43aa42
37d4fdf
 
c43aa42
80fcc20
37d4fdf
 
f9fb255
d175fc5
80fcc20
 
01561b2
80fcc20
c43aa42
278419b
80fcc20
01561b2
b8d133e
d1acdde
bde6994
 
 
d1acdde
 
bde6994
d1acdde
bde6994
 
d1acdde
7ecb292
f9fb255
bde6994
278419b
 
bde6994
 
 
 
 
 
 
 
 
 
 
 
40a9a17
bde6994
d1acdde
bde6994
 
d1acdde
ce4e83c
bde6994
 
 
 
 
d1acdde
bde6994
 
 
 
 
d1acdde
bde6994
 
 
ce4e83c
bde6994
 
 
 
 
 
40a9a17
 
bde6994
bbc0b32
 
 
 
c68ae17
bde6994
bbc0b32
40a9a17
bde6994
d1acdde
 
bde6994
d1acdde
bde6994
d1acdde
 
40a9a17
bbc0b32
bde6994
e4ad6f0
ba3564e
e4ad6f0
 
 
 
f9fb255
 
 
 
 
 
e4ad6f0
9da3128
dace311
 
9da3128
 
 
 
 
 
 
 
 
44f493d
 
 
 
bde6994
44f493d
 
 
9da3128
 
 
 
 
 
 
 
 
 
 
 
 
 
bde6994
 
 
 
 
 
9da3128
bde6994
 
9da3128
 
 
 
bde6994
 
 
 
 
 
 
 
9da3128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44f493d
bde6994
e4ad6f0
4a1d6ba
3bc063a
4a1d6ba
 
ef75b1b
4a1d6ba
3e82e3f
4a1d6ba
ef75b1b
4a1d6ba
 
ef75b1b
879bba7
ef75b1b
4a1d6ba
b120b28
7b8f2a8
0b62430
b276902
b120b28
0b62430
 
 
 
 
 
 
e4ad6f0
1
2
3
4
5
6
7
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import streamlit as st
from graph import EssayWriter, RouteQuery, GraphState
from language_options import language_options
from crew import *
import os
import re
import traceback
import base64

# Install Graphviz if not found
if os.system("which dot") != 0:
    os.system("apt-get update && apt-get install -y graphviz")

st.markdown(
    """
    <h1 style="text-align: center; white-space: nowrap; font-size: 2.5em;">
        Multi-Agent Essay Writing Assistant
    </h1>
    """,
    unsafe_allow_html=True
)

# Ensure session state variables are initialized properly
if "messages" not in st.session_state:
    st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]

if "app" not in st.session_state:
    st.session_state["app"] = None

if "chat_active" not in st.session_state:
    st.session_state["chat_active"] = True

# Sidebar with essay settings and user-defined length
# Sidebar with essay settings and user-defined length
with st.sidebar:
    st.subheader("📝 Note:")
    st.info(
        "\n\n 1. This app uses the 'gpt-4o-mini-2024-07-18' model."
        "\n\n 2. Writing essays may take some time, approximately 1-2 minutes."
    )

    # API Key Retrieval
    openai_key = st.secrets.get("OPENAI_API_KEY", "")

    st.divider()

    # User-defined essay length selection
    st.subheader("⚙️🛠️ Configure Essay Settings:")
    essay_length = st.number_input(
        "Select Essay Length (words):",
        min_value=150,
        max_value=500,
        value=250,
        step=50
    )

    #st.divider()

    #Language Selection
    #st.subheader("🌍 Select Language:")    
    selected_language = st.selectbox("Choose Language:", sorted(language_options.keys()), index=list(language_options.keys()).index("English"))

    st.divider()

    # Reference section
    st.subheader("📖 References:")
    st.markdown(
        "[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)",
        unsafe_allow_html=True
    )


# Initialize agents function
def initialize_agents():
    if not openai_key:
        st.error("⚠️ OpenAI API key is missing! Please provide a valid key through Hugging Face Secrets.")
        st.session_state["chat_active"] = True
        return None

    os.environ["OPENAI_API_KEY"] = openai_key
    try:
        # Prevent re-initialization
        if "app" in st.session_state and st.session_state["app"] is not None:
            return st.session_state["app"]
        
        # Initialize the full EssayWriter instance
        essay_writer = EssayWriter()  # Store the full instance
        st.session_state["app"] = essay_writer  # Now contains `graph`
        st.session_state["chat_active"] = False  # Enable chat after successful initialization

        return essay_writer
    except Exception as e:
        st.error(f"❌ Error initializing agents: {e}")
        st.session_state["chat_active"] = True
        return None


# Automatically initialize agents on app load
if st.session_state["app"] is None:
    st.session_state["app"] = initialize_agents()

if st.session_state["app"] is None:
    st.error("⚠️ Failed to initialize agents. Please check your API key and restart the app.")

app = st.session_state["app"]

# Function to invoke the agent and generate a response
def enforce_word_limit(text, limit):
    """Enforces strict word limit by truncating extra words."""
    words = re.findall(r'\b\w+\b', text)
    return ' '.join(words[:limit]) if len(words) > limit else text

def detect_unexpected_english(text, selected_language):
    """Detects unintended English words when another language is selected."""
    if selected_language != "English":
        english_words = re.findall(r'\b(?:is|the|and|or|in|on|at|to|with|for|of|by|it|that|this|was|he|she|they|we|you|I)\b', text)
        return len(english_words) > 5  # Allow a small tolerance

def generate_response(topic, length, selected_language):
    if not app or not hasattr(app, "graph"):
        st.error("Agents are not initialized. Please check the system or restart the app.")
        return {"response": "Error: Agents not initialized."}

    # Dynamically adjust structure based on length
    if length <= 250:
        intro_limit, body_limit, conclusion_limit = length // 5, length // 2, length // 5
        num_sections = 2  # Shorter essays should have fewer sections
    elif length <= 350:
        intro_limit, body_limit, conclusion_limit = length // 6, length // 1.8, length // 6
        num_sections = 3
    else:
        intro_limit, body_limit, conclusion_limit = length // 7, length // 1.7, length // 7
        num_sections = 4

    # Optimized Structured Prompt
    refined_prompt = f"""
    Write a **well-structured, informative, and engaging** essay on "{topic}" **strictly in {selected_language}.**
    
    **Word Limit:** Exactly {length} words. **Do not exceed or fall short of this limit.**
    **Language Rules:** Use natural linguistic style from {selected_language}. **Do not use English** unless explicitly requested.
    
    **Essay Structure:**
    - **Title**: Max 10 words.
    - **Introduction ({intro_limit} words max)**:
      - Clearly define the topic and its significance.
      - Provide a strong thesis statement.
      - Preview the key points covered in the essay.
    - **Main Body ({body_limit} words max, {num_sections} sections)**:
      - Each section must have:
        - A **clear subheading**.
        - A concise topic sentence with supporting details.
        - Relevant **examples, statistics, or historical references**.
      - Maintain natural **flow** between sections.
    - **Conclusion ({conclusion_limit} words max)**:
      - Summarize key insights **without repetition**.
      - Reinforce the thesis **based on discussion**.
      - End with a strong **closing statement** (reflection or call to action).
    
    **Hard Rules:**
    - **Use only {selected_language}**. No English unless explicitly requested.
    - **Do not exceed {length} words.** Absolute limit.
    - **Write concisely and avoid fluff**. No redundancy.
    - **Merge similar ideas** to maintain smooth readability.
    - **Ensure strict adherence to section word limits**.
    """

    # Invoke AI model with enforced word limit
    response = app.graph.invoke(input={
        "topic": topic,
        "length": length,
        "prompt": refined_prompt,
        "language": selected_language,
        "max_tokens": length + 10  # Small buffer for better trimming
    })

    # Strict word limit enforcement
    essay_text = enforce_word_limit(response.get("essay", ""), length)

    # Detect unintended English words in non-English essays
    if detect_unexpected_english(essay_text, selected_language):
        return {"response": f"⚠️ Warning: Some English words were detected in the {selected_language} essay. Try regenerating."}

    return {"essay": essay_text}



# Define Tabs
tab1, tab2 = st.tabs(["📜 Essay Generation", "📊 Workflow Viz"])

# 📜 Tab 1: Essay Generation
with tab1:
    # Display chat messages from the session
    if "messages" not in st.session_state:
        st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]

    for message in st.session_state["messages"]:
        with st.chat_message(message["role"]):
            st.markdown(message["content"], unsafe_allow_html=True)

    # Input
    topic = st.text_input("📝 Provide an essay topic:", value="Write an essay on the cultural diversity of India")

    # Add spacing
    st.write("")

    # Generate button
    if st.button("🚀 Generate Essay"):
        if topic and topic.strip():  # Ensure it's not empty
            # Store user message only if it's not already stored
            if not any(msg["content"] == topic for msg in st.session_state["messages"]):
                st.session_state["messages"].append({"role": "user", "content": topic})

            with st.spinner("⏳ Generating your essay..."):
                response = None
                if app:
                    response = app.write_essay({"topic": topic})
                else:
                    st.error("⚠️ Agents are not initialized. Please check the system or restart the app.")

            # Store and display assistant response
            if response and "essay" in response:
                essay = response["essay"]

                assistant_response = f"Here is your {essay_length}-word essay preview and the download link."
                st.session_state["messages"].append({"role": "assistant", "content": assistant_response})

                st.chat_message("assistant").markdown(assistant_response)

                # Create Two-Column Layout
                col1, col2 = st.columns(2)

                with col1:
                    st.markdown(f"### 📝 Essay Preview ({essay_length} words)")
                    st.markdown(f"#### {essay['header']}")
                    st.markdown(essay["entry"])

                    for para in essay["paragraphs"]:
                        st.markdown(f"**{para['sub_header']}**")
                        st.markdown(para["paragraph"])

                    st.markdown("**🖊️ Conclusion:**")
                    st.markdown(essay["conclusion"])

                with col2:
                    st.markdown("### ✍️ Edit Your Essay:")

                    # Combine all parts of the essay into one editable text field
                    full_essay_text = f"## {essay['header']}\n\n{essay['entry']}\n\n"
                    for para in essay["paragraphs"]:
                        full_essay_text += f"### {para['sub_header']}\n{para['paragraph']}\n\n"
                    full_essay_text += f"**Conclusion:**\n{essay['conclusion']}"

                    # Editable text area for the user
                    edited_essay = st.text_area("Edit Here:", value=full_essay_text, height=300)

                    # Save and Download buttons
                    save_col1, save_col2 = st.columns(2)

                    with save_col1:
                        if st.button("💾 Save as TXT"):
                            with open("edited_essay.txt", "w", encoding="utf-8") as file:
                                file.write(edited_essay)
                            with open("edited_essay.txt", "rb") as file:
                                st.download_button(label="⬇️ Download TXT", data=file, file_name="edited_essay.txt", mime="text/plain")

                    with save_col2:
                        if st.button("📄 Save as PDF"):
                            from fpdf import FPDF

                            pdf = FPDF()
                            pdf.set_auto_page_break(auto=True, margin=15)
                            pdf.add_page()
                            pdf.set_font("Arial", size=12)

                            for line in edited_essay.split("\n"):
                                pdf.cell(200, 10, txt=line, ln=True, align='L')

                            pdf.output("edited_essay.pdf")

                            with open("edited_essay.pdf", "rb") as file:
                                st.download_button(label="⬇️ Download PDF", data=file, file_name="edited_essay.pdf", mime="application/pdf")

                # Provide download link for the original PDF 
                pdf_name = response.get("pdf_name")
                if pdf_name and os.path.exists(pdf_name):
                    with open(pdf_name, "rb") as pdf_file:
                        b64 = base64.b64encode(pdf_file.read()).decode()
                        href = f"<a href='data:application/octet-stream;base64,{b64}' download='{pdf_name}'>📄 Click here to download the original PDF</a>"
                        st.markdown(href, unsafe_allow_html=True)

                # Save response in session state
                st.session_state["messages"].append(
                    {"role": "assistant", "content": f"Here is your {essay_length}-word essay preview and the download link."}
                )
            elif response:
                st.markdown(response["response"])
                st.session_state["messages"].append({"role": "assistant", "content": response["response"]})
            else:
                st.error("⚠️ No response received. Please try again.")



# 📊 Tab 2: Workflow Visualization
with tab2:
    #st.subheader("📊 Multi-Agent Essay Writer Workflow Viz")

    try:
        graph_path = "/tmp/graph.png"  
        if os.path.exists(graph_path):
            st.image(graph_path, caption="Multi-Agent Essay Writer Workflow Visualization", use_container_width=True)
        else:
            st.warning("⚠️ Workflow graph not found. Please run `graph.py` to regenerate `graph.png`.")

    except Exception as e:
        st.error("❌ An error occurred while generating the workflow visualization.")
        st.text_area("Error Details:", traceback.format_exc(), height=500)


# Acknowledgement Section
st.markdown(
    """
    <div style="text-align: center; font-size: 14px; color: #555; padding-top: 200px; margin-top: 200px;">
        <strong>Acknowledgement:</strong> This app is based on Mesut Duman's work: 
        <a href="https://github.com/mesutdmn/Autonomous-Multi-Agent-Systems-with-CrewAI-Essay-Writer/tree/main" 
           target="_blank" style="color: #007BFF; text-decoration: none;">
           CrewAI Essay Writer
        </a>
    </div>
    """,
    unsafe_allow_html=True,
)