Update app.py
Browse files
app.py
CHANGED
@@ -100,52 +100,62 @@ def generate_response(topic, length):
|
|
100 |
st.error("⚠️ Agents are not initialized. Please check the system or restart the app.")
|
101 |
return {"response": "Error: Agents not initialized."}
|
102 |
|
103 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
refined_prompt = f"""
|
105 |
-
Write a well-structured, engaging, and informative essay on "{topic}"
|
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 |
-
- A **call to action**.
|
135 |
-
- A **broader reflection** on the topic’s long-term significance.
|
136 |
-
|
137 |
-
## 5. Writing & Formatting Guidelines
|
138 |
-
- Maintain **formal, engaging, and precise** language.
|
139 |
-
- Ensure **clear paragraph structure and logical progression**.
|
140 |
-
- Avoid redundancy; keep insights sharp and impactful.
|
141 |
-
- Use **examples, expert opinions, or historical events** to strengthen arguments.
|
142 |
-
- Provide **citations or references** when possible.
|
143 |
"""
|
144 |
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
return response
|
148 |
|
|
|
149 |
# Define Tabs
|
150 |
tab1, tab2 = st.tabs(["📜 Essay Generation", "📊 Workflow Viz"])
|
151 |
|
|
|
100 |
st.error("⚠️ Agents are not initialized. Please check the system or restart the app.")
|
101 |
return {"response": "Error: Agents not initialized."}
|
102 |
|
103 |
+
# Define section allocations dynamically based on length
|
104 |
+
if length <= 300:
|
105 |
+
intro_limit = length // 5 # Shorter intro
|
106 |
+
body_limit = length // 2 # 2-3 key sections only
|
107 |
+
conclusion_limit = length // 5 # Brief closing
|
108 |
+
num_sections = 2 # Fewer key points
|
109 |
+
else:
|
110 |
+
intro_limit = length // 6 # Slightly shorter intro
|
111 |
+
body_limit = length // 1.8 # Allows more depth
|
112 |
+
conclusion_limit = length // 6 # Balanced closing
|
113 |
+
num_sections = 3 if length <= 400 else 4 # More sections for 400-500+ words
|
114 |
+
|
115 |
+
# Adjusted structured prompt
|
116 |
refined_prompt = f"""
|
117 |
+
Write a well-structured, engaging, and informative essay on "{topic}" with **EXACTLY {length} words**.
|
118 |
+
|
119 |
+
### **Structure:**
|
120 |
+
- **Title**: A compelling, creative title (max 10 words).
|
121 |
+
- **Introduction** ({intro_limit} words max):
|
122 |
+
- Define the topic & its importance concisely.
|
123 |
+
- Provide a strong thesis statement.
|
124 |
+
- Briefly mention key themes.
|
125 |
+
|
126 |
+
- **Main Body** ({body_limit} words max):
|
127 |
+
- Cover **{num_sections} key aspects only**.
|
128 |
+
- Each section must have:
|
129 |
+
- A clear **subheading**.
|
130 |
+
- A **topic sentence** & supporting details.
|
131 |
+
- **Examples, statistics, or historical references** (if relevant).
|
132 |
+
- Ensure logical transitions between sections.
|
133 |
+
|
134 |
+
- **Conclusion** ({conclusion_limit} words max):
|
135 |
+
- Summarize key insights concisely.
|
136 |
+
- Reinforce thesis based on discussion.
|
137 |
+
- End with a **thought-provoking** final statement (question, reflection, or call to action).
|
138 |
+
|
139 |
+
### **Important Rules:**
|
140 |
+
- DO NOT exceed {length} words.
|
141 |
+
- Avoid redundancy & filler sentences.
|
142 |
+
- Merge similar ideas to keep flow natural.
|
143 |
+
- Use precise, impactful language.
|
144 |
+
- Ensure balanced coverage** (not too broad or too specific).
|
145 |
+
- STOP when {length} words are reached.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
"""
|
147 |
|
148 |
+
# Invoke AI model with a controlled word limit
|
149 |
+
response = app.graph.invoke(input={
|
150 |
+
"topic": topic,
|
151 |
+
"length": length,
|
152 |
+
"prompt": refined_prompt,
|
153 |
+
"max_tokens": length * 1.2 # Ensures generation doesn’t exceed limit
|
154 |
+
})
|
155 |
|
156 |
return response
|
157 |
|
158 |
+
|
159 |
# Define Tabs
|
160 |
tab1, tab2 = st.tabs(["📜 Essay Generation", "📊 Workflow Viz"])
|
161 |
|