DrishtiSharma commited on
Commit
c68ae17
·
verified ·
1 Parent(s): 8033431

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -34
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("⚠️ 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,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
- # Adjusted structured prompt with language enforcement
130
  refined_prompt = f"""
131
- Write a well-structured, engaging, and informative essay on "{topic}" in **{selected_language}** with **EXACTLY {length} words**.
132
-
133
- ### **Structure:**
134
- - **Title**: A compelling, creative title (max 10 words).
135
- - **Introduction** ({intro_limit} words max):
136
- - Define the topic & its importance concisely.
137
- - Provide a strong thesis statement.
138
- - Briefly mention key themes.
139
-
140
- - **Main Body** ({body_limit} words max):
141
- - Cover **{num_sections} key aspects only**.
142
- - Each section must have:
143
- - A clear **subheading**.
144
- - A **topic sentence** & supporting details.
145
- - **Examples, statistics, or historical references** (if relevant).
146
- - Ensure logical transitions between sections.
147
-
148
- - **Conclusion** ({conclusion_limit} words max):
149
- - Summarize key insights concisely.
150
- - Reinforce thesis based on discussion.
151
- - End with a **thought-provoking** final statement (question, reflection, or call to action).
152
-
153
- ### **Important Rules:**
154
- - 🚫 **DO NOT exceed {length} words**.
155
- - **Avoid redundancy & filler sentences**.
156
- - 🔄 **Merge similar ideas** to keep flow natural.
157
- - ✂ **Use precise, impactful language**.
158
- - 🎯 **Ensure balanced coverage** (not too broad or too specific).
159
- - **STOP when {length} words are reached**.
160
- - Write in **{selected_language}** ONLY.
161
  """
162
 
163
- # Invoke AI model with controlled word limit
164
  response = app.graph.invoke(input={
165
  "topic": topic,
166
  "length": length,
167
  "prompt": refined_prompt,
168
- "max_tokens": length * 1.2 # Ensures generation doesn’t exceed limit
 
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