Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,9 @@ from transformers import pipeline
|
|
| 2 |
import gradio as gr
|
| 3 |
import re
|
| 4 |
|
| 5 |
-
# ============
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
MODEL_TRANSLATE_FR_EN = "Helsinki-NLP/opus-mt-fr-en"
|
| 9 |
-
|
| 10 |
-
simplifier = pipeline("text2text-generation", model=MODEL_SIMPLIFY)
|
| 11 |
-
translate_en_fr = pipeline("translation", model=MODEL_TRANSLATE_EN_FR)
|
| 12 |
-
translate_fr_en = pipeline("translation", model=MODEL_TRANSLATE_FR_EN)
|
| 13 |
|
| 14 |
|
| 15 |
# ============ HELPERS ============
|
|
@@ -17,7 +12,9 @@ translate_fr_en = pipeline("translation", model=MODEL_TRANSLATE_FR_EN)
|
|
| 17 |
def too_similar(src: str, out: str) -> bool:
|
| 18 |
src_clean = src.lower().strip()
|
| 19 |
out_clean = out.lower().strip()
|
| 20 |
-
if out_clean.startswith(src_clean)
|
|
|
|
|
|
|
| 21 |
return True
|
| 22 |
src_words = set(src_clean.split())
|
| 23 |
out_words = set(out_clean.split())
|
|
@@ -28,22 +25,25 @@ def too_similar(src: str, out: str) -> bool:
|
|
| 28 |
|
| 29 |
|
| 30 |
def fallback_explanation(text: str, lang: str = "en") -> str:
|
|
|
|
|
|
|
| 31 |
if lang == "fr":
|
| 32 |
return (
|
| 33 |
"### 🗣️ Explication en français simple\n"
|
| 34 |
-
f"- **Texte original :** {
|
| 35 |
"- Ce texte contient une règle ou une obligation administrative/juridique.\n"
|
| 36 |
-
"-
|
| 37 |
-
"-
|
| 38 |
-
"\n**En résumé :** ce texte
|
| 39 |
)
|
|
|
|
| 40 |
return (
|
| 41 |
"### 🗣️ Explanation in simple English\n"
|
| 42 |
-
f"- **Original clause:** {
|
| 43 |
-
"- This text
|
| 44 |
"- Check who must act, what must be done, and when.\n"
|
| 45 |
-
"- If it
|
| 46 |
-
"\n**In short:** this
|
| 47 |
)
|
| 48 |
|
| 49 |
|
|
@@ -90,23 +90,6 @@ Explanation in simple English:
|
|
| 90 |
return result
|
| 91 |
|
| 92 |
|
| 93 |
-
# ============ TRANSLATION FUNCTION ============
|
| 94 |
-
|
| 95 |
-
def translate_text(text, lang):
|
| 96 |
-
"""Translate output between FR <-> EN"""
|
| 97 |
-
if not text.strip():
|
| 98 |
-
return "Please generate an explanation first." if lang == "en" else "Veuillez d’abord générer une explication."
|
| 99 |
-
try:
|
| 100 |
-
if lang == "fr":
|
| 101 |
-
translated = translate_fr_en(text)[0]["translation_text"]
|
| 102 |
-
return f"### 🌍 Translated to English\n{translated}"
|
| 103 |
-
else:
|
| 104 |
-
translated = translate_en_fr(text)[0]["translation_text"]
|
| 105 |
-
return f"### 🌍 Traduit en français\n{translated}"
|
| 106 |
-
except Exception as e:
|
| 107 |
-
return f"⚠️ Translation error: {e}"
|
| 108 |
-
|
| 109 |
-
|
| 110 |
# ============ INTERFACE ============
|
| 111 |
|
| 112 |
with gr.Blocks(
|
|
@@ -117,14 +100,11 @@ with gr.Blocks(
|
|
| 117 |
# --- HEADER ---
|
| 118 |
gr.HTML(
|
| 119 |
"""
|
| 120 |
-
<div style="text-align:center; margin-bottom:25px;">
|
| 121 |
-
<
|
| 122 |
-
alt="Talk2Law Logo" width="85" style="margin-bottom:10px; opacity:0.9;">
|
| 123 |
-
<h1 style="color:#283593; font-size:2.2em;">⚖️ Talk2Law</h1>
|
| 124 |
<h3 style="color:#455A64;">Explain complex legal texts in simple French or English</h3>
|
| 125 |
-
<p style="color:#607D8B; font-size:0.95em;">
|
| 126 |
-
Paste a clause below — the app will simplify, reformulate, and explain it clearly
|
| 127 |
-
Collez un texte ci-dessous — l’application le reformulera et l’expliquera clairement.
|
| 128 |
</p>
|
| 129 |
</div>
|
| 130 |
"""
|
|
@@ -133,7 +113,12 @@ with gr.Blocks(
|
|
| 133 |
# --- INPUT AREA ---
|
| 134 |
with gr.Row():
|
| 135 |
with gr.Column(scale=1):
|
| 136 |
-
lang = gr.Radio(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
inp = gr.Textbox(
|
| 139 |
lines=8,
|
|
@@ -156,13 +141,10 @@ with gr.Blocks(
|
|
| 156 |
|
| 157 |
with gr.Column(scale=1):
|
| 158 |
out = gr.Markdown(label="✅ Explanation / Explication")
|
| 159 |
-
translate_btn = gr.Button("🌍 Translate / Traduire", variant="secondary")
|
| 160 |
-
translated_out = gr.Markdown(label="🌎 Translated Output / Texte traduit")
|
| 161 |
|
| 162 |
# --- BUTTON ACTIONS ---
|
| 163 |
btn.click(fn=simplify_legal, inputs=[inp, lang], outputs=out)
|
| 164 |
clear_btn.click(fn=lambda: "", inputs=None, outputs=inp)
|
| 165 |
-
translate_btn.click(fn=translate_text, inputs=[out, lang], outputs=translated_out)
|
| 166 |
|
| 167 |
# --- FOOTER ---
|
| 168 |
gr.HTML(
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import re
|
| 4 |
|
| 5 |
+
# ============ MODEL ============
|
| 6 |
+
MODEL_ID = "google/flan-t5-small"
|
| 7 |
+
simplifier = pipeline("text2text-generation", model=MODEL_ID)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
# ============ HELPERS ============
|
|
|
|
| 12 |
def too_similar(src: str, out: str) -> bool:
|
| 13 |
src_clean = src.lower().strip()
|
| 14 |
out_clean = out.lower().strip()
|
| 15 |
+
if out_clean.startswith(src_clean):
|
| 16 |
+
return True
|
| 17 |
+
if len(out_clean) < 40:
|
| 18 |
return True
|
| 19 |
src_words = set(src_clean.split())
|
| 20 |
out_words = set(out_clean.split())
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
def fallback_explanation(text: str, lang: str = "en") -> str:
|
| 28 |
+
txt = text.strip()
|
| 29 |
+
|
| 30 |
if lang == "fr":
|
| 31 |
return (
|
| 32 |
"### 🗣️ Explication en français simple\n"
|
| 33 |
+
f"- **Texte original :** {txt}\n"
|
| 34 |
"- Ce texte contient une règle ou une obligation administrative/juridique.\n"
|
| 35 |
+
"- Il faut bien vérifier qui doit faire quoi, dans quel délai, et quelles sont les conséquences.\n"
|
| 36 |
+
"- Si le texte mentionne une *décision définitive*, cela signifie qu’on **ne peut plus la contester** après ce délai.\n"
|
| 37 |
+
"\n**En résumé :** ce texte décrit ce qu’il faut faire et les conséquences si on ne le fait pas."
|
| 38 |
)
|
| 39 |
+
|
| 40 |
return (
|
| 41 |
"### 🗣️ Explanation in simple English\n"
|
| 42 |
+
f"- **Original clause:** {txt}\n"
|
| 43 |
+
"- This text contains a rule or an obligation.\n"
|
| 44 |
"- Check who must act, what must be done, and when.\n"
|
| 45 |
+
"- If it says a decision becomes *final*, that means it **cannot be challenged later**.\n"
|
| 46 |
+
"\n**In short:** this tells you what must be done and what happens if you don’t."
|
| 47 |
)
|
| 48 |
|
| 49 |
|
|
|
|
| 90 |
return result
|
| 91 |
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# ============ INTERFACE ============
|
| 94 |
|
| 95 |
with gr.Blocks(
|
|
|
|
| 100 |
# --- HEADER ---
|
| 101 |
gr.HTML(
|
| 102 |
"""
|
| 103 |
+
<div style="text-align: center; margin-bottom: 25px;">
|
| 104 |
+
<h1 style="color:#283593; font-size: 2.2em;">⚖️ Talk2Law</h1>
|
|
|
|
|
|
|
| 105 |
<h3 style="color:#455A64;">Explain complex legal texts in simple French or English</h3>
|
| 106 |
+
<p style="color:#607D8B; font-size: 0.95em;">
|
| 107 |
+
Paste a clause below — the app will simplify, reformulate, and explain it clearly.
|
|
|
|
| 108 |
</p>
|
| 109 |
</div>
|
| 110 |
"""
|
|
|
|
| 113 |
# --- INPUT AREA ---
|
| 114 |
with gr.Row():
|
| 115 |
with gr.Column(scale=1):
|
| 116 |
+
lang = gr.Radio(
|
| 117 |
+
["English", "Français"],
|
| 118 |
+
value="English",
|
| 119 |
+
label="🌐 Language / Langue",
|
| 120 |
+
interactive=True,
|
| 121 |
+
)
|
| 122 |
|
| 123 |
inp = gr.Textbox(
|
| 124 |
lines=8,
|
|
|
|
| 141 |
|
| 142 |
with gr.Column(scale=1):
|
| 143 |
out = gr.Markdown(label="✅ Explanation / Explication")
|
|
|
|
|
|
|
| 144 |
|
| 145 |
# --- BUTTON ACTIONS ---
|
| 146 |
btn.click(fn=simplify_legal, inputs=[inp, lang], outputs=out)
|
| 147 |
clear_btn.click(fn=lambda: "", inputs=None, outputs=inp)
|
|
|
|
| 148 |
|
| 149 |
# --- FOOTER ---
|
| 150 |
gr.HTML(
|