Basementup commited on
Commit
970f5cb
Β·
verified Β·
1 Parent(s): 02d15a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -60
app.py CHANGED
@@ -1,6 +1,5 @@
1
 
2
  import gradio as gr
3
- import json
4
  import os
5
  import time
6
  import subprocess
@@ -9,7 +8,10 @@ from pathlib import Path
9
  from huggingface_hub import InferenceClient
10
 
11
  subprocess.run(["pip", "install", "pypdf", "-q"], capture_output=True)
 
 
12
  from pypdf import PdfReader
 
13
 
14
  MODEL = "meta-llama/Llama-3.3-70B-Instruct"
15
 
@@ -111,6 +113,25 @@ def analyse(pdf_file):
111
 
112
  return r1, r2, r3, r4
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  def save_analysis(r1, r2, r3, r4):
115
  if not r1:
116
  return None, "⚠️ Nothing to save β€” run an analysis first."
@@ -135,12 +156,6 @@ def save_analysis(r1, r2, r3, r4):
135
  tmp.close()
136
  return tmp.name, "βœ… Ready to download."
137
 
138
- def read_cycle(text):
139
- return text or ""
140
-
141
- def stop_reading():
142
- return ""
143
-
144
  CSS = """
145
  @import url('https://fonts.googleapis.com/css2?family=Syne:wght@400;700;800&family=DM+Mono:wght@400;500&display=swap');
146
 
@@ -229,44 +244,7 @@ button.secondary:hover { background: var(--accent3) !important; color: #030712 !
229
  }
230
  """
231
 
232
- JS = """
233
- function setupReadback() {
234
- window.mpfSpeak = function(text) {
235
- if (!text || !text.trim()) {
236
- alert('No content yet β€” run the analysis first.');
237
- return;
238
- }
239
- window.speechSynthesis.cancel();
240
- const chunks = text.match(/.{1,200}/g) || [];
241
- let i = 0;
242
- function next() {
243
- if (i >= chunks.length) return;
244
- const u = new SpeechSynthesisUtterance(chunks[i]);
245
- u.rate = 0.95;
246
- u.lang = 'en-GB';
247
- u.onend = () => { i++; next(); };
248
- window.speechSynthesis.speak(u);
249
- }
250
- next();
251
- };
252
- window.mpfStop = function() {
253
- window.speechSynthesis.cancel();
254
- };
255
- window.mpfReadAll = function() {
256
- const areas = document.querySelectorAll('textarea');
257
- const text = Array.from(areas).map(a => a.value).filter(v => v && v.trim()).join('\n\n---\n\n');
258
- window.mpfSpeak(text);
259
- };
260
- window.mpfReadCycle = function(n) {
261
- const areas = document.querySelectorAll('textarea');
262
- const filled = Array.from(areas).filter(a => a.value && a.value.trim());
263
- if (filled[n-1]) window.mpfSpeak(filled[n-1].value);
264
- else alert('Cycle ' + n + ' is empty β€” run the analysis first.');
265
- };
266
- }
267
- """
268
-
269
- with gr.Blocks(css=CSS, js=JS, title="Metabolic Phoenix Framework") as demo:
270
 
271
  gr.HTML("""
272
  <div class="hdr">
@@ -281,38 +259,45 @@ with gr.Blocks(css=CSS, js=JS, title="Metabolic Phoenix Framework") as demo:
281
 
282
  with gr.Accordion("Cycle 1 β€” Fact Ledger", open=True):
283
  out1 = gr.Textbox(label="", lines=12, elem_classes=["out"])
 
 
284
 
285
  with gr.Accordion("Cycle 2 β€” Compliance Analysis", open=False):
286
  out2 = gr.Textbox(label="", lines=12, elem_classes=["out"])
 
 
287
 
288
  with gr.Accordion("Cycle 3 β€” Ouroboros Loops", open=False):
289
  out3 = gr.Textbox(label="", lines=12, elem_classes=["out"])
 
 
290
 
291
  with gr.Accordion("Cycle 4 β€” Full Case Output", open=False):
292
  out4 = gr.Textbox(label="", lines=12, elem_classes=["out"])
 
 
293
 
294
- # ── Read Back Controls ────────────────────────────────────────
295
- with gr.Row():
296
- rb1_btn = gr.Button("πŸ”Š Cycle 1", variant="secondary")
297
- rb2_btn = gr.Button("πŸ”Š Cycle 2", variant="secondary")
298
- rb3_btn = gr.Button("πŸ”Š Cycle 3", variant="secondary")
299
- rb4_btn = gr.Button("πŸ”Š Cycle 4", variant="secondary")
300
- rball_btn = gr.Button("πŸ”Š Read All", variant="primary")
301
- stop_btn = gr.Button("β–  Stop", variant="secondary")
302
 
303
  save_btn = gr.Button("πŸ’Ύ Save & Download Analysis", variant="secondary")
304
  dl_file = gr.File(label="Download your analysis", visible=False)
305
  save_msg = gr.Markdown("")
306
 
307
  run_btn.click(analyse, inputs=[pdf_input], outputs=[out1, out2, out3, out4])
308
- save_btn.click(save_analysis, inputs=[out1, out2, out3, out4], outputs=[dl_file, save_msg])
309
 
310
- rb1_btn.click(None, inputs=[], outputs=[], js="() => window.mpfReadCycle(1)")
311
- rb2_btn.click(None, inputs=[], outputs=[], js="() => window.mpfReadCycle(2)")
312
- rb3_btn.click(None, inputs=[], outputs=[], js="() => window.mpfReadCycle(3)")
313
- rb4_btn.click(None, inputs=[], outputs=[], js="() => window.mpfReadCycle(4)")
314
- rball_btn.click(None, inputs=[], outputs=[], js="() => window.mpfReadAll()")
315
- stop_btn.click(None, inputs=[], outputs=[], js="() => window.mpfStop()")
 
 
 
 
 
 
316
 
317
  gr.HTML("""
318
  <div class="foot">
 
1
 
2
  import gradio as gr
 
3
  import os
4
  import time
5
  import subprocess
 
8
  from huggingface_hub import InferenceClient
9
 
10
  subprocess.run(["pip", "install", "pypdf", "-q"], capture_output=True)
11
+ subprocess.run(["pip", "install", "gtts", "-q"], capture_output=True)
12
+
13
  from pypdf import PdfReader
14
+ from gtts import gTTS
15
 
16
  MODEL = "meta-llama/Llama-3.3-70B-Instruct"
17
 
 
113
 
114
  return r1, r2, r3, r4
115
 
116
+ def make_audio(text):
117
+ if not text or not text.strip():
118
+ return None
119
+ try:
120
+ tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
121
+ tts = gTTS(text=text[:3000], lang="en", tld="co.uk", slow=False)
122
+ tts.save(tmp.name)
123
+ return tmp.name
124
+ except Exception as e:
125
+ return None
126
+
127
+ def read_cycle1(t): return make_audio(t)
128
+ def read_cycle2(t): return make_audio(t)
129
+ def read_cycle3(t): return make_audio(t)
130
+ def read_cycle4(t): return make_audio(t)
131
+ def read_all(t1, t2, t3, t4):
132
+ combined = "\n\n".join([x for x in [t1, t2, t3, t4] if x and x.strip()])
133
+ return make_audio(combined)
134
+
135
  def save_analysis(r1, r2, r3, r4):
136
  if not r1:
137
  return None, "⚠️ Nothing to save β€” run an analysis first."
 
156
  tmp.close()
157
  return tmp.name, "βœ… Ready to download."
158
 
 
 
 
 
 
 
159
  CSS = """
160
  @import url('https://fonts.googleapis.com/css2?family=Syne:wght@400;700;800&family=DM+Mono:wght@400;500&display=swap');
161
 
 
244
  }
245
  """
246
 
247
+ with gr.Blocks(css=CSS, title="Metabolic Phoenix Framework") as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
 
249
  gr.HTML("""
250
  <div class="hdr">
 
259
 
260
  with gr.Accordion("Cycle 1 β€” Fact Ledger", open=True):
261
  out1 = gr.Textbox(label="", lines=12, elem_classes=["out"])
262
+ rb1_btn = gr.Button("πŸ”Š Read Cycle 1", variant="secondary")
263
+ audio1 = gr.Audio(label="", autoplay=True, visible=False)
264
 
265
  with gr.Accordion("Cycle 2 β€” Compliance Analysis", open=False):
266
  out2 = gr.Textbox(label="", lines=12, elem_classes=["out"])
267
+ rb2_btn = gr.Button("πŸ”Š Read Cycle 2", variant="secondary")
268
+ audio2 = gr.Audio(label="", autoplay=True, visible=False)
269
 
270
  with gr.Accordion("Cycle 3 β€” Ouroboros Loops", open=False):
271
  out3 = gr.Textbox(label="", lines=12, elem_classes=["out"])
272
+ rb3_btn = gr.Button("πŸ”Š Read Cycle 3", variant="secondary")
273
+ audio3 = gr.Audio(label="", autoplay=True, visible=False)
274
 
275
  with gr.Accordion("Cycle 4 β€” Full Case Output", open=False):
276
  out4 = gr.Textbox(label="", lines=12, elem_classes=["out"])
277
+ rb4_btn = gr.Button("πŸ”Š Read Cycle 4", variant="secondary")
278
+ audio4 = gr.Audio(label="", autoplay=True, visible=False)
279
 
280
+ rball_btn = gr.Button("πŸ”Š Read Full Analysis", variant="primary")
281
+ audio_all = gr.Audio(label="Full Analysis Audio", autoplay=True, visible=False)
 
 
 
 
 
 
282
 
283
  save_btn = gr.Button("πŸ’Ύ Save & Download Analysis", variant="secondary")
284
  dl_file = gr.File(label="Download your analysis", visible=False)
285
  save_msg = gr.Markdown("")
286
 
287
  run_btn.click(analyse, inputs=[pdf_input], outputs=[out1, out2, out3, out4])
 
288
 
289
+ rb1_btn.click(read_cycle1, inputs=[out1], outputs=[audio1]).then(
290
+ lambda: gr.update(visible=True), outputs=[audio1])
291
+ rb2_btn.click(read_cycle2, inputs=[out2], outputs=[audio2]).then(
292
+ lambda: gr.update(visible=True), outputs=[audio2])
293
+ rb3_btn.click(read_cycle3, inputs=[out3], outputs=[audio3]).then(
294
+ lambda: gr.update(visible=True), outputs=[audio3])
295
+ rb4_btn.click(read_cycle4, inputs=[out4], outputs=[audio4]).then(
296
+ lambda: gr.update(visible=True), outputs=[audio4])
297
+ rball_btn.click(read_all, inputs=[out1, out2, out3, out4], outputs=[audio_all]).then(
298
+ lambda: gr.update(visible=True), outputs=[audio_all])
299
+
300
+ save_btn.click(save_analysis, inputs=[out1, out2, out3, out4], outputs=[dl_file, save_msg])
301
 
302
  gr.HTML("""
303
  <div class="foot">