DrishtiSharma commited on
Commit
bbc0b32
·
verified ·
1 Parent(s): 6d6e03d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -40
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
- # Refined prompt for better essay generation
 
 
 
 
 
 
 
 
 
 
 
 
104
  refined_prompt = f"""
105
- Write a well-structured, engaging, and informative essay on "{topic}". The essay should be approximately {length} words and follow this structured format:
106
-
107
- ## 1. Title
108
- - Generate a compelling, creative, and relevant title that encapsulates the theme of the essay.
109
-
110
- ## 2. Introduction (100-150 words)
111
- - Clearly define the topic and its importance in the broader context.
112
- - Provide a strong **thesis statement** that outlines the essay’s key argument.
113
- - Briefly mention the **key themes** that will be explored in the body.
114
- - Engage the reader with a thought-provoking fact, quote, or question.
115
-
116
- ## 3. Main Body (Ensure clear organization and logical transitions)
117
- Each section should:
118
- - **Have a distinct, engaging subheading**.
119
- - **Begin with a topic sentence** introducing the section’s main idea.
120
- - **Include real-world examples, historical references, or statistical data**.
121
- - **Maintain smooth transitions** between sections for cohesive reading.
122
-
123
- ### Suggested Sections (Modify as Needed)
124
- - **Historical Context**: Trace the origins and evolution of the topic over time.
125
- - **Key Aspects**: Break down essential components (e.g., cultural, political, economic influences).
126
- - **Modern Challenges & Debates**: Discuss **contemporary issues** and **conflicting viewpoints**.
127
- - **Impact & Future Trends**: Examine how the topic influences the present and future.
128
-
129
- ## 4. Conclusion (100-150 words)
130
- - Concisely summarize key insights and arguments.
131
- - Reinforce the essay’s thesis in light of the discussion.
132
- - End with a **thought-provoking final statement**, such as:
133
- - A **rhetorical question**.
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
- response = app.graph.invoke(input={"topic": topic, "length": length, "prompt": refined_prompt})
 
 
 
 
 
 
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