Update app.py
Browse files
app.py
CHANGED
@@ -95,26 +95,31 @@ if st.session_state["app"] is None:
|
|
95 |
app = st.session_state["app"]
|
96 |
|
97 |
# Function to invoke the agent and generate a response
|
98 |
-
def generate_response(topic, length):
|
99 |
if not app or not hasattr(app, "graph"):
|
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 //
|
111 |
-
body_limit = length // 1.
|
112 |
-
conclusion_limit = length //
|
113 |
-
num_sections =
|
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).
|
@@ -137,15 +142,16 @@ def generate_response(topic, length):
|
|
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
|
149 |
response = app.graph.invoke(input={
|
150 |
"topic": topic,
|
151 |
"length": length,
|
|
|
95 |
app = st.session_state["app"]
|
96 |
|
97 |
# Function to invoke the agent and generate a response
|
98 |
+
def generate_response(topic, length, selected_language):
|
99 |
if not app or not hasattr(app, "graph"):
|
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 (~20% of total)
|
106 |
+
body_limit = length // 2 # 2-3 key sections only (~50% of total)
|
107 |
+
conclusion_limit = length // 5 # Brief closing (~20% of total)
|
108 |
num_sections = 2 # Fewer key points
|
109 |
+
elif length <= 400:
|
110 |
+
intro_limit = length // 6 # Slightly shorter intro (~17% of total)
|
111 |
+
body_limit = length // 1.8 # Allows more depth (~55% of total)
|
112 |
+
conclusion_limit = length // 6 # Balanced closing (~17% of total)
|
113 |
+
num_sections = 3 # More sections
|
114 |
else:
|
115 |
+
intro_limit = length // 7 # Even shorter intro (~15% of total)
|
116 |
+
body_limit = length // 1.7 # More depth in body (~60% of total)
|
117 |
+
conclusion_limit = length // 7 # Shorter but strong closing (~15% of total)
|
118 |
+
num_sections = 4 # Maximum sections for deeper discussion
|
119 |
|
120 |
+
# Adjusted structured prompt with language enforcement
|
121 |
refined_prompt = f"""
|
122 |
+
Write a well-structured, engaging, and informative essay on "{topic}" in **{selected_language}** with **EXACTLY {length} words**.
|
123 |
|
124 |
### **Structure:**
|
125 |
- **Title**: A compelling, creative title (max 10 words).
|
|
|
142 |
- End with a **thought-provoking** final statement (question, reflection, or call to action).
|
143 |
|
144 |
### **Important Rules:**
|
145 |
+
- 🚫 **DO NOT exceed {length} words**.
|
146 |
+
- ✅ **Avoid redundancy & filler sentences**.
|
147 |
+
- 🔄 **Merge similar ideas** to keep flow natural.
|
148 |
+
- ✂ **Use precise, impactful language**.
|
149 |
+
- 🎯 **Ensure balanced coverage** (not too broad or too specific).
|
150 |
+
- **STOP when {length} words are reached**.
|
151 |
+
- Write in **{selected_language}** ONLY.
|
152 |
"""
|
153 |
|
154 |
+
# Invoke AI model with controlled word limit
|
155 |
response = app.graph.invoke(input={
|
156 |
"topic": topic,
|
157 |
"length": length,
|