Rajan Sharma commited on
Commit
efb40ce
·
verified ·
1 Parent(s): 6ae435c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -19
app.py CHANGED
@@ -1,4 +1,5 @@
1
- # app.py
 
2
  from __future__ import annotations
3
  import os
4
  import io
@@ -13,10 +14,8 @@ from datetime import datetime
13
  import regex as re2
14
  import re
15
 
16
- # --- BACKEND IMPORTS ---
17
  from langchain_cohere import ChatCohere
18
 
19
- # --- LOCAL MODULE IMPORTS ---
20
  from settings import (
21
  GENERAL_CONVERSATION_PROMPT,
22
  COHERE_MODEL_PRIMARY, COHERE_TIMEOUT_S, USE_OPEN_FALLBACKS
@@ -25,12 +24,10 @@ from audit_log import log_event
25
  from privacy import safety_filter, refusal_reply
26
  from llm_router import cohere_chat, _co_client, cohere_embed
27
 
28
- # --- UTILITY FUNCTIONS ---
29
-
30
  def load_markdown_text(filepath: str) -> str:
31
- """Safely loads text content from a markdown file."""
32
  try:
33
- with open(filepath, 'r', encoding='utf-8') as f: return f.read()
 
34
  except FileNotFoundError:
35
  return f"**Error:** Document `{os.path.basename(filepath)}` not found."
36
 
@@ -38,11 +35,7 @@ def _sanitize_text(s: str) -> str:
38
  if not isinstance(s, str): return s
39
  return re2.sub(r'[\p{C}--[\n\t]]+', '', s)
40
 
41
- # --- THE "ANALYST-WRITER" PIPELINE ---
42
-
43
  def _create_python_script(user_scenario: str, schema_context: str) -> str:
44
- """Asks the AI to write a Python script that outputs raw, structured JSON."""
45
-
46
  EXPERT_ANALYTICAL_GUIDELINES = """
47
  --- EXPERT ANALYTICAL GUIDELINES ---
48
  When writing your script, you MUST follow these expert business rules:
@@ -81,7 +74,6 @@ Now, write the complete Python script that performs the analysis and prints a si
81
  return "print(json.dumps({'error': 'Failed to generate a valid Python script.'}))"
82
 
83
  def _generate_long_report(prompt: str) -> str:
84
- """Calls the Cohere API directly with a much higher max_tokens limit."""
85
  try:
86
  client = _co_client()
87
  if not client: return "Error: Cohere client not initialized."
@@ -96,7 +88,6 @@ def _generate_long_report(prompt: str) -> str:
96
  return f"Error during final report generation: {e}"
97
 
98
  def _generate_final_report(user_scenario: str, raw_data_json: str) -> str:
99
- """Asks the AI to act as a consultant and write a polished report from the raw data."""
100
  prompt_for_writer = f"""
101
  You are an expert management consultant and data analyst.
102
  A data science script has run to extract key findings. You have the user's original request and the raw JSON data.
@@ -123,7 +114,6 @@ def _append_msg(h: List[Dict[str, str]], r: str, c: str) -> List[Dict[str, str]]
123
  return (h or []) + [{"role": r, "content": c}]
124
 
125
  def ping_cohere() -> str:
126
- """Lightweight health check."""
127
  try:
128
  cli = _co_client()
129
  if not cli: return "Cohere client not initialized."
@@ -132,7 +122,6 @@ def ping_cohere() -> str:
132
  except Exception as e: return f"Cohere ping failed: {e}"
133
 
134
  def handle(user_msg: str, files: list, yield_update) -> str:
135
- """Orchestrates the 'Analyst-Writer' pipeline."""
136
  try:
137
  safe_in, blocked_in, reason_in = safety_filter(user_msg, mode="input")
138
  if blocked_in: return refusal_reply(reason_in)
@@ -218,8 +207,6 @@ with gr.Blocks(theme="soft", css="style.css") as demo:
218
  privacy_link = gr.Button("Privacy Policy", variant="link")
219
  terms_link = gr.Button("Terms of Service", variant="link")
220
 
221
- # --- DEFINITIVE FIX: ALL UI LOGIC IS NOW CORRECTLY INDENTED ---
222
-
223
  def run_analysis_wrapper(prompt, files, chat_history_list, history_state_list):
224
  if not prompt:
225
  gr.Warning("Please enter a prompt.")
@@ -251,7 +238,6 @@ with gr.Blocks(theme="soft", css="style.css") as demo:
251
  def view_history(selection, history_state_list):
252
  if not selection or not history_state_list:
253
  return ""
254
- # FIX 1: Correctly extract the ID (the timestamp) from the selection string
255
  selected_id = selection.split(" - ")
256
  selected_assessment = next((item for item in history_state_list if item["id"] == selected_id), None)
257
 
@@ -279,7 +265,6 @@ with gr.Blocks(theme="soft", css="style.css") as demo:
279
  inputs=[history_dropdown, assessment_history],
280
  outputs=[history_display]
281
  )
282
- # FIX 2: The 'assessment_history' state is no longer cleared.
283
  clear_btn.click(
284
  lambda: (None, None, []),
285
  outputs=[prompt_input, files_input, chat_history_output]
 
1
+ works app.py
2
+
3
  from __future__ import annotations
4
  import os
5
  import io
 
14
  import regex as re2
15
  import re
16
 
 
17
  from langchain_cohere import ChatCohere
18
 
 
19
  from settings import (
20
  GENERAL_CONVERSATION_PROMPT,
21
  COHERE_MODEL_PRIMARY, COHERE_TIMEOUT_S, USE_OPEN_FALLBACKS
 
24
  from privacy import safety_filter, refusal_reply
25
  from llm_router import cohere_chat, _co_client, cohere_embed
26
 
 
 
27
  def load_markdown_text(filepath: str) -> str:
 
28
  try:
29
+ with open(filepath, 'r', encoding='utf-8') as f:
30
+ return f.read()
31
  except FileNotFoundError:
32
  return f"**Error:** Document `{os.path.basename(filepath)}` not found."
33
 
 
35
  if not isinstance(s, str): return s
36
  return re2.sub(r'[\p{C}--[\n\t]]+', '', s)
37
 
 
 
38
  def _create_python_script(user_scenario: str, schema_context: str) -> str:
 
 
39
  EXPERT_ANALYTICAL_GUIDELINES = """
40
  --- EXPERT ANALYTICAL GUIDELINES ---
41
  When writing your script, you MUST follow these expert business rules:
 
74
  return "print(json.dumps({'error': 'Failed to generate a valid Python script.'}))"
75
 
76
  def _generate_long_report(prompt: str) -> str:
 
77
  try:
78
  client = _co_client()
79
  if not client: return "Error: Cohere client not initialized."
 
88
  return f"Error during final report generation: {e}"
89
 
90
  def _generate_final_report(user_scenario: str, raw_data_json: str) -> str:
 
91
  prompt_for_writer = f"""
92
  You are an expert management consultant and data analyst.
93
  A data science script has run to extract key findings. You have the user's original request and the raw JSON data.
 
114
  return (h or []) + [{"role": r, "content": c}]
115
 
116
  def ping_cohere() -> str:
 
117
  try:
118
  cli = _co_client()
119
  if not cli: return "Cohere client not initialized."
 
122
  except Exception as e: return f"Cohere ping failed: {e}"
123
 
124
  def handle(user_msg: str, files: list, yield_update) -> str:
 
125
  try:
126
  safe_in, blocked_in, reason_in = safety_filter(user_msg, mode="input")
127
  if blocked_in: return refusal_reply(reason_in)
 
207
  privacy_link = gr.Button("Privacy Policy", variant="link")
208
  terms_link = gr.Button("Terms of Service", variant="link")
209
 
 
 
210
  def run_analysis_wrapper(prompt, files, chat_history_list, history_state_list):
211
  if not prompt:
212
  gr.Warning("Please enter a prompt.")
 
238
  def view_history(selection, history_state_list):
239
  if not selection or not history_state_list:
240
  return ""
 
241
  selected_id = selection.split(" - ")
242
  selected_assessment = next((item for item in history_state_list if item["id"] == selected_id), None)
243
 
 
265
  inputs=[history_dropdown, assessment_history],
266
  outputs=[history_display]
267
  )
 
268
  clear_btn.click(
269
  lambda: (None, None, []),
270
  outputs=[prompt_input, files_input, chat_history_output]