Spaces:
Sleeping
Sleeping
Commit ·
e8d52de
1
Parent(s): 4accd60
adaptation prompt
Browse files- src/streamlit_app.py +41 -21
src/streamlit_app.py
CHANGED
|
@@ -42,9 +42,12 @@ def generate_response(prompt, tokenizer, model):
|
|
| 42 |
top_p=0.9
|
| 43 |
)
|
| 44 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
-
#
|
| 46 |
if "[RESPONSE]" in text:
|
| 47 |
text = text.split("[RESPONSE]", 1)[-1].strip()
|
|
|
|
|
|
|
|
|
|
| 48 |
return text
|
| 49 |
|
| 50 |
st.set_page_config(page_title="Assistant Juridique IA", layout="wide")
|
|
@@ -147,36 +150,53 @@ if st.button("📤 Envoyer") and user_input.strip():
|
|
| 147 |
unsafe_allow_html=True
|
| 148 |
)
|
| 149 |
|
| 150 |
-
# Préparation
|
| 151 |
context_text = "\n\n".join([
|
| 152 |
f"<doc pertinence={score:.2f}>\n{doc.page_content.strip()}\n</doc>"
|
| 153 |
for doc, score, pertinence in filtered_docs
|
| 154 |
])
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
# Génération des deux réponses en colonnes, d'abord le modèle le plus rapide (flan-t5-small)
|
| 172 |
col1, col2 = st.columns(2)
|
|
|
|
|
|
|
| 173 |
with col1:
|
| 174 |
with st.spinner("Génération de la réponse (flan-t5-small)..."):
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
| 176 |
st.subheader("Réponse (flan-t5-small)")
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
| 178 |
with col2:
|
| 179 |
with st.spinner("Génération de la réponse (t5-base-fr-sum-cnndm)..."):
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
| 181 |
st.subheader("Réponse (t5-base-fr-sum-cnndm)")
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
top_p=0.9
|
| 43 |
)
|
| 44 |
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
+
# Si le tag [RESPONSE] n'est pas généré, on affiche tout
|
| 46 |
if "[RESPONSE]" in text:
|
| 47 |
text = text.split("[RESPONSE]", 1)[-1].strip()
|
| 48 |
+
# Si la réponse est vide, on affiche la sortie brute
|
| 49 |
+
if not text.strip():
|
| 50 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 51 |
return text
|
| 52 |
|
| 53 |
st.set_page_config(page_title="Assistant Juridique IA", layout="wide")
|
|
|
|
| 150 |
unsafe_allow_html=True
|
| 151 |
)
|
| 152 |
|
| 153 |
+
# Préparation du contexte documentaire (doit être défini avant les prompts)
|
| 154 |
context_text = "\n\n".join([
|
| 155 |
f"<doc pertinence={score:.2f}>\n{doc.page_content.strip()}\n</doc>"
|
| 156 |
for doc, score, pertinence in filtered_docs
|
| 157 |
])
|
| 158 |
+
# Définition des mots-clés pour chaque modèle
|
| 159 |
+
flan_keywords = {"question": "Question", "context": "Contexte documentaire", "response": "Réponse"}
|
| 160 |
+
plg_keywords = {"question": "question", "context": "contexte", "response": "résumé"}
|
| 161 |
+
# Prompt template commun
|
| 162 |
+
def build_prompt(keywords):
|
| 163 |
+
return f"""
|
| 164 |
+
Vous êtes un assistant juridique spécialisé en droit français.
|
| 165 |
+
Votre tâche est de proposer une réponse synthétique et argumentée à la {keywords['question']} suivante, en vous appuyant uniquement sur les extraits de documents fournis, classés par pertinence.
|
| 166 |
+
Indiquez clairement si la {keywords['response']} est incertaine ou partielle.
|
| 167 |
+
Répondez en français.
|
| 168 |
+
|
| 169 |
+
{keywords['question']} : {user_input}
|
| 170 |
+
|
| 171 |
+
{keywords['context']} :
|
| 172 |
+
{context_text}
|
| 173 |
+
"""
|
| 174 |
+
prompt_flan = build_prompt(flan_keywords)
|
| 175 |
+
prompt_plg = build_prompt(plg_keywords)
|
| 176 |
|
| 177 |
# Génération des deux réponses en colonnes, d'abord le modèle le plus rapide (flan-t5-small)
|
| 178 |
col1, col2 = st.columns(2)
|
| 179 |
+
output_flan = None
|
| 180 |
+
output_plg = None
|
| 181 |
with col1:
|
| 182 |
with st.spinner("Génération de la réponse (flan-t5-small)..."):
|
| 183 |
+
try:
|
| 184 |
+
output_flan = generate_response(prompt_flan, flan_tokenizer, flan_model)
|
| 185 |
+
except Exception as e:
|
| 186 |
+
st.error(f"Erreur génération flan-t5-small : {e}")
|
| 187 |
st.subheader("Réponse (flan-t5-small)")
|
| 188 |
+
if output_flan:
|
| 189 |
+
st.write(output_flan)
|
| 190 |
+
else:
|
| 191 |
+
st.info("Aucune réponse générée par flan-t5-small.")
|
| 192 |
with col2:
|
| 193 |
with st.spinner("Génération de la réponse (t5-base-fr-sum-cnndm)..."):
|
| 194 |
+
try:
|
| 195 |
+
output_plg = generate_response(prompt_plg, plg_tokenizer, plg_model)
|
| 196 |
+
except Exception as e:
|
| 197 |
+
st.error(f"Erreur génération t5-base-fr-sum-cnndm : {e}")
|
| 198 |
st.subheader("Réponse (t5-base-fr-sum-cnndm)")
|
| 199 |
+
if output_plg:
|
| 200 |
+
st.write(output_plg)
|
| 201 |
+
else:
|
| 202 |
+
st.info("Aucune réponse générée par t5-base-fr-sum-cnndm.")
|