Spaces:
Sleeping
Sleeping
Commit ·
dc90d7f
1
Parent(s): 5e8b427
modele : falcon
Browse files- requirements.txt +5 -2
- src/streamlit_app.py +32 -18
requirements.txt
CHANGED
|
@@ -17,5 +17,8 @@ chromadb
|
|
| 17 |
tqdm # Pour affichage des barres de progression
|
| 18 |
python-dotenv # Pour lire .env si tu stockes des clés API
|
| 19 |
|
| 20 |
-
transformers
|
| 21 |
-
torch
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
tqdm # Pour affichage des barres de progression
|
| 18 |
python-dotenv # Pour lire .env si tu stockes des clés API
|
| 19 |
|
| 20 |
+
transformers>=4.36.0
|
| 21 |
+
torch>=2.1.0
|
| 22 |
+
accelerate
|
| 23 |
+
|
| 24 |
+
|
src/streamlit_app.py
CHANGED
|
@@ -11,10 +11,11 @@ import datetime
|
|
| 11 |
|
| 12 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 13 |
import torch
|
|
|
|
| 14 |
|
| 15 |
@st.cache_resource
|
| 16 |
def load_local_model():
|
| 17 |
-
model_id = "
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 19 |
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
model_id,
|
|
@@ -25,6 +26,9 @@ def load_local_model():
|
|
| 25 |
|
| 26 |
tokenizer, model = load_local_model()
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
def generate_response(prompt):
|
| 29 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 30 |
with torch.no_grad():
|
|
@@ -35,8 +39,11 @@ def generate_response(prompt):
|
|
| 35 |
temperature=0.7,
|
| 36 |
top_p=0.9
|
| 37 |
)
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
st.set_page_config(page_title="Assistant Juridique IA", layout="wide")
|
| 42 |
st.title("📚 Assistant Juridique avec IA")
|
|
@@ -158,7 +165,7 @@ if st.button("📤 Envoyer") and user_input.strip():
|
|
| 158 |
score = max(0, min(score, max_dist))
|
| 159 |
return round((1 - score / max_dist) * 100)
|
| 160 |
|
| 161 |
-
with st.spinner("Recherche
|
| 162 |
embeddings = get_local_embeddings()
|
| 163 |
db_path = os.path.abspath("./db")
|
| 164 |
db = Chroma(persist_directory=db_path, embedding_function=embeddings)
|
|
@@ -206,21 +213,28 @@ if st.button("📤 Envoyer") and user_input.strip():
|
|
| 206 |
unsafe_allow_html=True
|
| 207 |
)
|
| 208 |
|
| 209 |
-
# Préparation d'un prompt optimisé pour
|
| 210 |
context_text = "\n\n".join([
|
| 211 |
f"<doc pertinence={score:.2f}>\n{doc.page_content.strip()}\n</doc>"
|
| 212 |
for doc, score, pertinence in filtered_docs
|
| 213 |
])
|
| 214 |
-
prompt =
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 13 |
import torch
|
| 14 |
+
import streamlit as st
|
| 15 |
|
| 16 |
@st.cache_resource
|
| 17 |
def load_local_model():
|
| 18 |
+
model_id = "tiiuae/falcon-7b-instruct"
|
| 19 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
model_id,
|
|
|
|
| 26 |
|
| 27 |
tokenizer, model = load_local_model()
|
| 28 |
|
| 29 |
+
|
| 30 |
+
tokenizer, model = load_local_model()
|
| 31 |
+
|
| 32 |
def generate_response(prompt):
|
| 33 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 34 |
with torch.no_grad():
|
|
|
|
| 39 |
temperature=0.7,
|
| 40 |
top_p=0.9
|
| 41 |
)
|
| 42 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 43 |
+
# Nettoyage optionnel : couper avant [RESPONSE] si tu utilises ce tag
|
| 44 |
+
if "[RESPONSE]" in text:
|
| 45 |
+
text = text.split("[RESPONSE]", 1)[-1].strip()
|
| 46 |
+
return text
|
| 47 |
|
| 48 |
st.set_page_config(page_title="Assistant Juridique IA", layout="wide")
|
| 49 |
st.title("📚 Assistant Juridique avec IA")
|
|
|
|
| 165 |
score = max(0, min(score, max_dist))
|
| 166 |
return round((1 - score / max_dist) * 100)
|
| 167 |
|
| 168 |
+
with st.spinner("Recherche des documents pertinents..."):
|
| 169 |
embeddings = get_local_embeddings()
|
| 170 |
db_path = os.path.abspath("./db")
|
| 171 |
db = Chroma(persist_directory=db_path, embedding_function=embeddings)
|
|
|
|
| 213 |
unsafe_allow_html=True
|
| 214 |
)
|
| 215 |
|
| 216 |
+
# Préparation d'un prompt optimisé pour Falcon, tenant compte du score de pertinence
|
| 217 |
context_text = "\n\n".join([
|
| 218 |
f"<doc pertinence={score:.2f}>\n{doc.page_content.strip()}\n</doc>"
|
| 219 |
for doc, score, pertinence in filtered_docs
|
| 220 |
])
|
| 221 |
+
prompt = f"""
|
| 222 |
+
<s>
|
| 223 |
+
[INSTRUCTION]
|
| 224 |
+
Vous êtes un assistant juridique spécialisé en droit français.
|
| 225 |
+
Votre tâche est de proposer une réponse synthétique et argumentée à la question suivante, en vous appuyant uniquement sur les extraits de documents fournis, classés par pertinence.
|
| 226 |
+
Indiquez clairement si la réponse est incertaine ou partielle.
|
| 227 |
+
Répondez en français.
|
| 228 |
+
|
| 229 |
+
Question : {user_input}
|
| 230 |
+
|
| 231 |
+
Contexte documentaire (avec pertinence) :
|
| 232 |
+
{context_text}
|
| 233 |
+
|
| 234 |
+
[RESPONSE]
|
| 235 |
+
"""
|
| 236 |
+
|
| 237 |
+
with st.spinner("Génération de la réponse..."):
|
| 238 |
+
output_text = generate_response(prompt)
|
| 239 |
+
st.subheader("✅ Réponse générée")
|
| 240 |
+
st.write(output_text)
|