Basementup commited on
Commit
3c3a958
·
verified ·
1 Parent(s): 142a4ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -9
app.py CHANGED
@@ -9,6 +9,7 @@ from huggingface_hub import HfApi
9
  from pypdf import PdfReader
10
  import docx
11
  import traceback
 
12
 
13
  DATA_FILE = "legislation_rules.json"
14
  RULINGS_FILE = "rulings_log.json"
@@ -72,22 +73,31 @@ def process_rule_document(file_obj, act_name):
72
  if not text: return "Error: Could not extract text from rule document."
73
  return add_rule_manually(act_name, os.path.basename(file_obj.name), text, f"File: {os.path.basename(file_obj.name)}")
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  def rule_on_issue(issue_description, issue_file, llm_endpoint, llm_key, llm_model):
76
- """
77
- Framework 2.2: Regulatory Reasoning Engine with Document Intake.
78
- """
79
- # Combine text input and file input
80
  file_text = extract_text_from_any(issue_file)
81
  combined_issue = f"{issue_description}\n\n[EXTRACTED FROM DOCUMENT]:\n{file_text}".strip()
82
 
83
  if not combined_issue:
84
- return "Error: No issue description or document provided.", ""
85
 
86
  rules = load_data(DATA_FILE)
87
  if not rules:
88
- return "Error: Dataset is empty. Add rules (CRA, FCA, etc.) first.", ""
89
 
90
- # Context window management
91
  context = "\n".join([f"[{r['act']} - {r['section_title']}]: {r['text'][:600]}..." for r in rules[:20]])
92
 
93
  prompt = f"""
@@ -115,6 +125,9 @@ def rule_on_issue(issue_description, issue_file, llm_endpoint, llm_key, llm_mode
115
  except Exception as e:
116
  ruling_text = f"Inference Error: {str(e)}"
117
 
 
 
 
118
  # Log the ruling
119
  rulings_log = load_data(RULINGS_FILE)
120
  new_ruling = {
@@ -126,7 +139,7 @@ def rule_on_issue(issue_description, issue_file, llm_endpoint, llm_key, llm_mode
126
  rulings_log.append(new_ruling)
127
  save_data(rulings_log, RULINGS_FILE)
128
 
129
- return ruling_text, f"Ruling Logged (ID: {new_ruling['ruling_hash'][:8]})"
130
 
131
  def sync_all(token, dataset_id):
132
  if not token or not dataset_id: return "Error: Missing credentials."
@@ -161,6 +174,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
161
  rule_btn = gr.Button("Generate Formal Ruling", variant="primary")
162
  with gr.Column():
163
  ruling_out = gr.Markdown(label="Official Ruling")
 
164
  log_status = gr.Textbox(label="Audit Status")
165
 
166
  with gr.Tab("➕ Manage Rules"):
@@ -184,7 +198,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
184
  s_btn = gr.Button("Sync Everything to Dataset")
185
  s_out = gr.Textbox(label="Sync Status")
186
 
187
- rule_btn.click(fn=rule_on_issue, inputs=[issue_desc, issue_file, end, key, mod], outputs=[ruling_out, log_status])
188
  doc_btn.click(fn=process_rule_document, inputs=[doc_in, doc_act], outputs=op_status)
189
  m_btn.click(fn=add_rule_manually, inputs=[m_act, m_tit, m_txt, m_src], outputs=op_status)
190
  refresh_btn.click(fn=view_stats, outputs=stats_out)
 
9
  from pypdf import PdfReader
10
  import docx
11
  import traceback
12
+ from gtts import gTTS
13
 
14
  DATA_FILE = "legislation_rules.json"
15
  RULINGS_FILE = "rulings_log.json"
 
73
  if not text: return "Error: Could not extract text from rule document."
74
  return add_rule_manually(act_name, os.path.basename(file_obj.name), text, f"File: {os.path.basename(file_obj.name)}")
75
 
76
+ def generate_tts(text):
77
+ """Generates an audio file from text using gTTS."""
78
+ if not text or len(text) < 5:
79
+ return None
80
+ try:
81
+ clean_text = text.replace("#", "").replace("*", "").replace("`", "")
82
+ tts = gTTS(text=clean_text[:1500], lang='en') # Limit to first 1500 chars for speed
83
+ filename = f"ruling_audio_{int(datetime.now().timestamp())}.mp3"
84
+ tts.save(filename)
85
+ return filename
86
+ except Exception as e:
87
+ print(f"TTS Error: {e}")
88
+ return None
89
+
90
  def rule_on_issue(issue_description, issue_file, llm_endpoint, llm_key, llm_model):
 
 
 
 
91
  file_text = extract_text_from_any(issue_file)
92
  combined_issue = f"{issue_description}\n\n[EXTRACTED FROM DOCUMENT]:\n{file_text}".strip()
93
 
94
  if not combined_issue:
95
+ return "Error: No issue description or document provided.", "", None
96
 
97
  rules = load_data(DATA_FILE)
98
  if not rules:
99
+ return "Error: Dataset is empty. Add rules (CRA, FCA, etc.) first.", "", None
100
 
 
101
  context = "\n".join([f"[{r['act']} - {r['section_title']}]: {r['text'][:600]}..." for r in rules[:20]])
102
 
103
  prompt = f"""
 
125
  except Exception as e:
126
  ruling_text = f"Inference Error: {str(e)}"
127
 
128
+ # Generate Audio for the Ruling
129
+ audio_file = generate_tts(ruling_text)
130
+
131
  # Log the ruling
132
  rulings_log = load_data(RULINGS_FILE)
133
  new_ruling = {
 
139
  rulings_log.append(new_ruling)
140
  save_data(rulings_log, RULINGS_FILE)
141
 
142
+ return ruling_text, f"Ruling Logged (ID: {new_ruling['ruling_hash'][:8]})", audio_file
143
 
144
  def sync_all(token, dataset_id):
145
  if not token or not dataset_id: return "Error: Missing credentials."
 
174
  rule_btn = gr.Button("Generate Formal Ruling", variant="primary")
175
  with gr.Column():
176
  ruling_out = gr.Markdown(label="Official Ruling")
177
+ audio_out = gr.Audio(label="Audible Ruling", interactive=False)
178
  log_status = gr.Textbox(label="Audit Status")
179
 
180
  with gr.Tab("➕ Manage Rules"):
 
198
  s_btn = gr.Button("Sync Everything to Dataset")
199
  s_out = gr.Textbox(label="Sync Status")
200
 
201
+ rule_btn.click(fn=rule_on_issue, inputs=[issue_desc, issue_file, end, key, mod], outputs=[ruling_out, log_status, audio_out])
202
  doc_btn.click(fn=process_rule_document, inputs=[doc_in, doc_act], outputs=op_status)
203
  m_btn.click(fn=add_rule_manually, inputs=[m_act, m_tit, m_txt, m_src], outputs=op_status)
204
  refresh_btn.click(fn=view_stats, outputs=stats_out)