Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,15 +26,16 @@ MODEL = "meta-llama/Llama-3.3-70B-Instruct"
|
|
| 26 |
# SHARED: TOKEN / MODEL / TTS
|
| 27 |
# =========================
|
| 28 |
|
| 29 |
-
def call_llama(prompt, max_tokens=1200):
|
| 30 |
"""
|
| 31 |
-
Pulls Llama directly via Hugging Face using
|
| 32 |
-
|
| 33 |
"""
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
if not token:
|
| 37 |
-
return "Model error: HF_TOKEN missing. Add your Hugging Face token as a Space secret named HF_TOKEN."
|
| 38 |
|
| 39 |
if not token.startswith("hf_"):
|
| 40 |
return "Model error: HF_TOKEN exists but does not look like a Hugging Face token. It should start with hf_."
|
|
@@ -125,7 +126,7 @@ def extract_pdf_text(path):
|
|
| 125 |
return f"[PDF read error: {e}]"
|
| 126 |
|
| 127 |
|
| 128 |
-
def phoenix_analyse(pdf_file):
|
| 129 |
if not pdf_file:
|
| 130 |
return "Please upload a PDF.", "", "", ""
|
| 131 |
|
|
@@ -140,14 +141,16 @@ def phoenix_analyse(pdf_file):
|
|
| 140 |
base
|
| 141 |
+ "You are Introbot — part of the Metabolic Phoenix Framework by Dwayne Anthony Brian Galloway.\n\n"
|
| 142 |
+ "From this document extract every key fact, who said it and when; every admission; every denial; every contradiction; every gap; and any statutory duties triggered.\n\n"
|
| 143 |
-
+ "Format as a clean numbered FACT LEDGER."
|
|
|
|
| 144 |
)
|
| 145 |
|
| 146 |
r2 = call_llama(
|
| 147 |
base
|
| 148 |
+ "You are RHB 2.0 + CPF — part of the Metabolic Phoenix Framework by Dwayne Anthony Brian Galloway.\n\n"
|
| 149 |
+ "Map every institution to every statutory duty. Mark COMPLIANT or NON-COMPLIANT with evidence. Identify legal errors, causal chain, Equality Act 2010 issues especially s.13 and s.20, and remedies.\n\n"
|
| 150 |
-
+ "Be forensic. Cite statute sections where possible."
|
|
|
|
| 151 |
)
|
| 152 |
|
| 153 |
r3 = call_llama(
|
|
@@ -158,7 +161,8 @@ def phoenix_analyse(pdf_file):
|
|
| 158 |
+ "RING B — BACKWARD SWEEP: decisions that would be decided differently under current law.\n\n"
|
| 159 |
+ "RING C — TYPOLOGY MUTATION: patterns of institutional misconduct.\n\n"
|
| 160 |
+ "RING D — DRIFT CORRECTION: internal contradictions and inconsistencies.\n\n"
|
| 161 |
-
+ "End with ESCALATION MAP."
|
|
|
|
| 162 |
)
|
| 163 |
|
| 164 |
r4 = call_llama(
|
|
@@ -174,7 +178,8 @@ def phoenix_analyse(pdf_file):
|
|
| 174 |
"TOP 5 ARGUMENTS — strongest first\n"
|
| 175 |
"RECOMMENDED NEXT STEPS — in priority order\n"
|
| 176 |
"ESCALATION MAP — which body gets what and when\n\n"
|
| 177 |
-
"Ms. Nala | Injectiv8 | Dwayne Anthony Brian Galloway"
|
|
|
|
| 178 |
)
|
| 179 |
|
| 180 |
return r1, r2, r3, r4
|
|
@@ -502,9 +507,9 @@ class DocumentMerger:
|
|
| 502 |
return "\n".join(output)
|
| 503 |
|
| 504 |
|
| 505 |
-
def ai_summarise(merged_doc: str, analysis: Dict) -> str:
|
| 506 |
try:
|
| 507 |
-
hf_token = os.environ.get("HF_TOKEN")
|
| 508 |
risk_summary = {}
|
| 509 |
for risk in analysis.get("risk_language", []):
|
| 510 |
risk_summary[risk["type"]] = risk_summary.get(risk["type"], 0) + 1
|
|
@@ -688,7 +693,7 @@ def export_docx(merged_doc: str, analysis_report: str, summary: str) -> str:
|
|
| 688 |
return None
|
| 689 |
|
| 690 |
|
| 691 |
-
def process_documents(file1, file2, file3):
|
| 692 |
try:
|
| 693 |
raw_inputs = [file1, file2, file3]
|
| 694 |
files = []
|
|
@@ -752,7 +757,7 @@ def process_documents(file1, file2, file3):
|
|
| 752 |
else:
|
| 753 |
analysis_report += "No risk language patterns detected.\n"
|
| 754 |
|
| 755 |
-
summary = ai_summarise(merged_doc, analysis)
|
| 756 |
|
| 757 |
txt_path = export_txt(merged_doc, analysis_report)
|
| 758 |
pdf_path = export_pdf(merged_doc, analysis_report)
|
|
@@ -867,6 +872,15 @@ with gr.Blocks(css=CSS, title="The Metamorphic Phoenix") as demo:
|
|
| 867 |
"""
|
| 868 |
)
|
| 869 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 870 |
with gr.Tabs():
|
| 871 |
with gr.TabItem("Metabolic Phoenix"):
|
| 872 |
with gr.Row():
|
|
@@ -919,7 +933,7 @@ with gr.Blocks(css=CSS, title="The Metamorphic Phoenix") as demo:
|
|
| 919 |
lib_dl_file = gr.File(label="Your file", visible=False)
|
| 920 |
lib_msg = gr.Markdown("")
|
| 921 |
|
| 922 |
-
run_btn.click(phoenix_analyse, inputs=[pdf_input], outputs=[out1, out2, out3, out4])
|
| 923 |
|
| 924 |
top_tts_btn.click(
|
| 925 |
lambda a, b, c, d: make_tts(
|
|
@@ -981,7 +995,7 @@ with gr.Blocks(css=CSS, title="The Metamorphic Phoenix") as demo:
|
|
| 981 |
|
| 982 |
analyze_btn.click(
|
| 983 |
fn=process_documents,
|
| 984 |
-
inputs=[file1, file2, file3],
|
| 985 |
outputs=[merged_output, analysis_output, summary_output, txt_file, pdf_file, docx_file],
|
| 986 |
)
|
| 987 |
|
|
|
|
| 26 |
# SHARED: TOKEN / MODEL / TTS
|
| 27 |
# =========================
|
| 28 |
|
| 29 |
+
def call_llama(prompt, user_token=None, max_tokens=1200):
|
| 30 |
"""
|
| 31 |
+
Pulls Llama directly via Hugging Face using HF_TOKEN.
|
| 32 |
+
Prioritizes user_token from UI if provided, otherwise looks for HF_TOKEN env var.
|
| 33 |
"""
|
| 34 |
+
# Use user provided token first, then fallback to env var
|
| 35 |
+
token = (user_token or os.environ.get("HF_TOKEN", "")).strip()
|
| 36 |
|
| 37 |
if not token:
|
| 38 |
+
return "Model error: HF_TOKEN missing. Add your Hugging Face token as a Space secret named HF_TOKEN or enter it in the input field."
|
| 39 |
|
| 40 |
if not token.startswith("hf_"):
|
| 41 |
return "Model error: HF_TOKEN exists but does not look like a Hugging Face token. It should start with hf_."
|
|
|
|
| 126 |
return f"[PDF read error: {e}]"
|
| 127 |
|
| 128 |
|
| 129 |
+
def phoenix_analyse(pdf_file, hf_token_input):
|
| 130 |
if not pdf_file:
|
| 131 |
return "Please upload a PDF.", "", "", ""
|
| 132 |
|
|
|
|
| 141 |
base
|
| 142 |
+ "You are Introbot — part of the Metabolic Phoenix Framework by Dwayne Anthony Brian Galloway.\n\n"
|
| 143 |
+ "From this document extract every key fact, who said it and when; every admission; every denial; every contradiction; every gap; and any statutory duties triggered.\n\n"
|
| 144 |
+
+ "Format as a clean numbered FACT LEDGER.",
|
| 145 |
+
user_token=hf_token_input
|
| 146 |
)
|
| 147 |
|
| 148 |
r2 = call_llama(
|
| 149 |
base
|
| 150 |
+ "You are RHB 2.0 + CPF — part of the Metabolic Phoenix Framework by Dwayne Anthony Brian Galloway.\n\n"
|
| 151 |
+ "Map every institution to every statutory duty. Mark COMPLIANT or NON-COMPLIANT with evidence. Identify legal errors, causal chain, Equality Act 2010 issues especially s.13 and s.20, and remedies.\n\n"
|
| 152 |
+
+ "Be forensic. Cite statute sections where possible.",
|
| 153 |
+
user_token=hf_token_input
|
| 154 |
)
|
| 155 |
|
| 156 |
r3 = call_llama(
|
|
|
|
| 161 |
+ "RING B — BACKWARD SWEEP: decisions that would be decided differently under current law.\n\n"
|
| 162 |
+ "RING C — TYPOLOGY MUTATION: patterns of institutional misconduct.\n\n"
|
| 163 |
+ "RING D — DRIFT CORRECTION: internal contradictions and inconsistencies.\n\n"
|
| 164 |
+
+ "End with ESCALATION MAP.",
|
| 165 |
+
user_token=hf_token_input
|
| 166 |
)
|
| 167 |
|
| 168 |
r4 = call_llama(
|
|
|
|
| 178 |
"TOP 5 ARGUMENTS — strongest first\n"
|
| 179 |
"RECOMMENDED NEXT STEPS — in priority order\n"
|
| 180 |
"ESCALATION MAP — which body gets what and when\n\n"
|
| 181 |
+
"Ms. Nala | Injectiv8 | Dwayne Anthony Brian Galloway",
|
| 182 |
+
user_token=hf_token_input
|
| 183 |
)
|
| 184 |
|
| 185 |
return r1, r2, r3, r4
|
|
|
|
| 507 |
return "\n".join(output)
|
| 508 |
|
| 509 |
|
| 510 |
+
def ai_summarise(merged_doc: str, analysis: Dict, hf_token_input=None) -> str:
|
| 511 |
try:
|
| 512 |
+
hf_token = (hf_token_input or os.environ.get("HF_TOKEN", "")).strip()
|
| 513 |
risk_summary = {}
|
| 514 |
for risk in analysis.get("risk_language", []):
|
| 515 |
risk_summary[risk["type"]] = risk_summary.get(risk["type"], 0) + 1
|
|
|
|
| 693 |
return None
|
| 694 |
|
| 695 |
|
| 696 |
+
def process_documents(file1, file2, file3, hf_token_input):
|
| 697 |
try:
|
| 698 |
raw_inputs = [file1, file2, file3]
|
| 699 |
files = []
|
|
|
|
| 757 |
else:
|
| 758 |
analysis_report += "No risk language patterns detected.\n"
|
| 759 |
|
| 760 |
+
summary = ai_summarise(merged_doc, analysis, hf_token_input=hf_token_input)
|
| 761 |
|
| 762 |
txt_path = export_txt(merged_doc, analysis_report)
|
| 763 |
pdf_path = export_pdf(merged_doc, analysis_report)
|
|
|
|
| 872 |
"""
|
| 873 |
)
|
| 874 |
|
| 875 |
+
with gr.Row():
|
| 876 |
+
with gr.Column(scale=1):
|
| 877 |
+
hf_token_input = gr.Textbox(
|
| 878 |
+
label="Hugging Face Token (HF_TOKEN)",
|
| 879 |
+
placeholder="hf_...",
|
| 880 |
+
type="password",
|
| 881 |
+
info="Pulled from Space Secrets if empty."
|
| 882 |
+
)
|
| 883 |
+
|
| 884 |
with gr.Tabs():
|
| 885 |
with gr.TabItem("Metabolic Phoenix"):
|
| 886 |
with gr.Row():
|
|
|
|
| 933 |
lib_dl_file = gr.File(label="Your file", visible=False)
|
| 934 |
lib_msg = gr.Markdown("")
|
| 935 |
|
| 936 |
+
run_btn.click(phoenix_analyse, inputs=[pdf_input, hf_token_input], outputs=[out1, out2, out3, out4])
|
| 937 |
|
| 938 |
top_tts_btn.click(
|
| 939 |
lambda a, b, c, d: make_tts(
|
|
|
|
| 995 |
|
| 996 |
analyze_btn.click(
|
| 997 |
fn=process_documents,
|
| 998 |
+
inputs=[file1, file2, file3, hf_token_input],
|
| 999 |
outputs=[merged_output, analysis_output, summary_output, txt_file, pdf_file, docx_file],
|
| 1000 |
)
|
| 1001 |
|