Update app.py
Browse files
app.py
CHANGED
@@ -106,7 +106,7 @@ app = st.session_state["app"]
|
|
106 |
# Function to invoke the agent and generate a response
|
107 |
def generate_response(topic, length, selected_language):
|
108 |
if not app or not hasattr(app, "graph"):
|
109 |
-
st.error("
|
110 |
return {"response": "Error: Agents not initialized."}
|
111 |
|
112 |
# Define section allocations dynamically based on length
|
@@ -126,51 +126,53 @@ def generate_response(topic, length, selected_language):
|
|
126 |
conclusion_limit = length // 7 # Shorter but strong closing (~15% of total)
|
127 |
num_sections = 4 # Maximum sections for deeper discussion
|
128 |
|
129 |
-
#
|
130 |
refined_prompt = f"""
|
131 |
-
Write a well-structured,
|
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 |
-
# Invoke AI model with
|
164 |
response = app.graph.invoke(input={
|
165 |
"topic": topic,
|
166 |
"length": length,
|
167 |
"prompt": refined_prompt,
|
168 |
-
"
|
|
|
169 |
})
|
170 |
|
171 |
return response
|
172 |
|
173 |
|
|
|
174 |
# Define Tabs
|
175 |
tab1, tab2 = st.tabs(["📜 Essay Generation", "📊 Workflow Viz"])
|
176 |
|
|
|
106 |
# Function to invoke the agent and generate a response
|
107 |
def generate_response(topic, length, selected_language):
|
108 |
if not app or not hasattr(app, "graph"):
|
109 |
+
st.error("Agents are not initialized. Please check the system or restart the app.")
|
110 |
return {"response": "Error: Agents not initialized."}
|
111 |
|
112 |
# Define section allocations dynamically based on length
|
|
|
126 |
conclusion_limit = length // 7 # Shorter but strong closing (~15% of total)
|
127 |
num_sections = 4 # Maximum sections for deeper discussion
|
128 |
|
129 |
+
# Improved Structured Prompt with Language Reinforcement
|
130 |
refined_prompt = f"""
|
131 |
+
Write a well-structured, informative, and engaging essay on "{topic}" strictly in {selected_language}.
|
132 |
+
|
133 |
+
Word Limit: Exactly {length} words. Do not exceed or fall short of this limit.
|
134 |
+
Use native linguistic style and expressions from {selected_language}.
|
135 |
+
|
136 |
+
Structure:
|
137 |
+
- Title: Maximum 10 words
|
138 |
+
- Introduction ({intro_limit} words max):
|
139 |
+
- Clearly define the topic and its relevance.
|
140 |
+
- Provide a strong thesis statement.
|
141 |
+
- Outline key points covered in the essay.
|
142 |
+
- Main Body ({body_limit} words max):
|
143 |
+
- Cover {num_sections} key aspects only.
|
144 |
+
- Each section must have:
|
145 |
+
- A clear subheading.
|
146 |
+
- A concise topic sentence with supporting details.
|
147 |
+
- Examples, statistics, or historical references (if relevant).
|
148 |
+
- Do not exceed section limits.
|
149 |
+
- Conclusion ({conclusion_limit} words max):
|
150 |
+
- Summarize key insights without repetition.
|
151 |
+
- Reinforce thesis based on discussion.
|
152 |
+
- End with a strong closing statement, such as a reflection or call to action.
|
153 |
+
|
154 |
+
Rules:
|
155 |
+
- Write only in {selected_language}. Do not use English unless explicitly requested.
|
156 |
+
- Do not exceed {length} words. Hard limit.
|
157 |
+
- Use concise, direct, and impactful language.
|
158 |
+
- Avoid redundancy and unnecessary filler.
|
159 |
+
- Merge similar ideas for natural flow.
|
160 |
+
- Stop exactly at {length} words. Do not go over.
|
161 |
"""
|
162 |
|
163 |
+
# Invoke AI model with enforced word limit
|
164 |
response = app.graph.invoke(input={
|
165 |
"topic": topic,
|
166 |
"length": length,
|
167 |
"prompt": refined_prompt,
|
168 |
+
"language": selected_language,
|
169 |
+
"max_tokens": int(length * 1.05) # Ensures strict word cap
|
170 |
})
|
171 |
|
172 |
return response
|
173 |
|
174 |
|
175 |
+
|
176 |
# Define Tabs
|
177 |
tab1, tab2 = st.tabs(["📜 Essay Generation", "📊 Workflow Viz"])
|
178 |
|