Rajan Sharma commited on
Commit
d18bfbf
·
verified ·
1 Parent(s): 5613174

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -11
app.py CHANGED
@@ -227,6 +227,67 @@ _session_rag = SessionRAG()
227
  # NEW: session-scoped data registry
228
  _data_registry = DataRegistry()
229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  # ---------- Core chat logic (generic scenario handling) ----------
231
  def clarityops_reply(user_msg, history, tz, uploaded_files_paths, awaiting_answers=False):
232
  try:
@@ -296,16 +357,26 @@ def clarityops_reply(user_msg, history, tz, uploaded_files_paths, awaiting_answe
296
  mapping = map_concepts(safe_in, _data_registry)
297
 
298
  if not awaiting_answers:
299
- # PHASE 1: ask for missing/ambiguous information
300
- phase1 = build_phase1_questions(scenario_text=safe_in, registry=_data_registry, mapping=mapping)
301
- phase1 = _sanitize_text(phase1)
302
- log_event("assistant_reply", None, {
303
- **hash_summary("prompt", safe_in if not PERSIST_CONTENT else ""),
304
- **hash_summary("reply", phase1 if not PERSIST_CONTENT else ""),
305
- "mode": "scenario_phase1",
306
- "awaiting_next_phase": True
307
- })
308
- return history + [(user_msg, phase1)], True
 
 
 
 
 
 
 
 
 
 
309
 
310
  # PHASE 2: compute data analysis and generate structured response
311
  data_findings_md, missing_keys = build_data_findings_markdown(_data_registry, mapping)
@@ -550,4 +621,3 @@ if __name__ == "__main__":
550
 
551
 
552
 
553
-
 
227
  # NEW: session-scoped data registry
228
  _data_registry = DataRegistry()
229
 
230
+ def _assess_scenario_completeness(scenario_text: str, data_registry: DataRegistry, mapping: MappingResult) -> bool:
231
+ """Intelligently assess if scenario has enough info to proceed directly to analysis."""
232
+ if not scenario_text or not data_registry.names():
233
+ return False
234
+
235
+ scenario_lower = scenario_text.lower()
236
+
237
+ # Check for explicit instructions/tasks
238
+ has_explicit_tasks = any(phrase in scenario_lower for phrase in [
239
+ 'identify', 'analyze', 'calculate', 'determine', 'compare', 'assess', 'rank', 'list',
240
+ 'your tasks', 'deliverables', 'requirements', 'you should', 'you need to',
241
+ 'find', 'show', 'report', 'evaluate', 'examine', 'investigate'
242
+ ])
243
+
244
+ # Check for data descriptions that match uploaded files
245
+ mentions_data_files = any(phrase in scenario_lower for phrase in [
246
+ '.csv', 'dataset', 'data file', 'database', 'records', 'columns', 'spreadsheet', 'table'
247
+ ])
248
+
249
+ # Check if scenario describes what the data contains
250
+ describes_data_structure = any(phrase in scenario_lower for phrase in [
251
+ 'column', 'field', 'contains', 'includes', 'reports', 'each record', 'data shows', 'file has'
252
+ ])
253
+
254
+ # NEW: Check if files were uploaded (implicit data context)
255
+ has_uploaded_files = len(data_registry.names()) > 0
256
+
257
+ # NEW: Check for general analysis requests that imply using uploaded data
258
+ implies_data_analysis = any(phrase in scenario_lower for phrase in [
259
+ 'this data', 'the data', 'analyze', 'analysis', 'insights', 'patterns', 'trends'
260
+ ])
261
+
262
+ # Check mapping success rate
263
+ total_concepts = len(mapping.resolved) + len(mapping.ambiguous) + len(mapping.missing)
264
+ if total_concepts == 0:
265
+ return False
266
+
267
+ mapping_success_rate = len(mapping.resolved) / total_concepts
268
+ has_good_mappings = mapping_success_rate >= 0.5 # At least half of concepts mapped
269
+
270
+ # Check if critical ambiguities exist (more than 3 unresolved concepts)
271
+ critical_ambiguities = len(mapping.ambiguous) + len(mapping.missing) > 3
272
+
273
+ # Enhanced decision logic: proceed if scenario is instructional AND either:
274
+ # 1. Explicitly describes data/files, OR
275
+ # 2. Files are uploaded and scenario implies analysis of "the data"
276
+ data_context_clear = (
277
+ mentions_data_files or
278
+ describes_data_structure or
279
+ (has_uploaded_files and implies_data_analysis)
280
+ )
281
+
282
+ can_proceed = (
283
+ has_explicit_tasks and
284
+ data_context_clear and
285
+ has_good_mappings and
286
+ not critical_ambiguities
287
+ )
288
+
289
+ return can_proceed
290
+
291
  # ---------- Core chat logic (generic scenario handling) ----------
292
  def clarityops_reply(user_msg, history, tz, uploaded_files_paths, awaiting_answers=False):
293
  try:
 
357
  mapping = map_concepts(safe_in, _data_registry)
358
 
359
  if not awaiting_answers:
360
+ # Intelligent scenario assessment: can we proceed directly to analysis?
361
+ can_proceed = _assess_scenario_completeness(safe_in, _data_registry, mapping)
362
+
363
+ if can_proceed:
364
+ awaiting_answers = True # Skip directly to Phase 2
365
+ else:
366
+ # PHASE 1: ask for missing/ambiguous information only when truly needed
367
+ phase1 = build_phase1_questions(scenario_text=safe_in, registry=_data_registry, mapping=mapping)
368
+ if phase1.strip() == "**Data Analysis Ready**: Your data appears well-structured. Please provide any additional context about your analysis goals.":
369
+ # If only generic message, skip to analysis
370
+ awaiting_answers = True
371
+ else:
372
+ phase1 = _sanitize_text(phase1)
373
+ log_event("assistant_reply", None, {
374
+ **hash_summary("prompt", safe_in if not PERSIST_CONTENT else ""),
375
+ **hash_summary("reply", phase1 if not PERSIST_CONTENT else ""),
376
+ "mode": "scenario_phase1",
377
+ "awaiting_next_phase": True
378
+ })
379
+ return history + [(user_msg, phase1)], True
380
 
381
  # PHASE 2: compute data analysis and generate structured response
382
  data_findings_md, missing_keys = build_data_findings_markdown(_data_registry, mapping)
 
621
 
622
 
623