Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,13 @@ import json
|
|
3 |
import os
|
4 |
from sentence_transformers import SentenceTransformer, util
|
5 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Load the processed legal code data
|
8 |
@st.cache_resource
|
@@ -15,17 +22,58 @@ def load_data(file_path):
|
|
15 |
def load_model():
|
16 |
return SentenceTransformer('distiluse-base-multilingual-cased-v1')
|
17 |
|
18 |
-
def
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
chunk_embeddings = model.encode([chunk['text'] for chunk in chunks], convert_to_tensor=True)
|
21 |
|
22 |
-
cos_scores = util.pytorch_cos_sim(
|
23 |
-
top_results = torch.topk(cos_scores, k=top_k)
|
24 |
|
25 |
return [chunks[idx] for idx in top_results.indices]
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
def main():
|
28 |
-
st.title("Chatbot Prawny")
|
29 |
|
30 |
# Load data and model
|
31 |
data_file = "processed_kodeksy.json"
|
@@ -51,19 +99,21 @@ def main():
|
|
51 |
with st.chat_message("user"):
|
52 |
st.markdown(prompt)
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
response = "Oto co znalazłem w kodeksie:\n\n"
|
59 |
-
for chunk in relevant_chunks:
|
60 |
-
response += f"**{chunk['metadata']['nazwa']} - Artykuł {chunk['metadata']['article']}**\n"
|
61 |
-
response += f"{chunk['text']}\n\n"
|
62 |
|
63 |
-
#
|
64 |
with st.chat_message("assistant"):
|
65 |
-
st.
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
# Sidebar for additional options
|
69 |
with st.sidebar:
|
|
|
3 |
import os
|
4 |
from sentence_transformers import SentenceTransformer, util
|
5 |
import torch
|
6 |
+
from huggingface_hub import InferenceClient
|
7 |
+
import asyncio
|
8 |
+
|
9 |
+
# Load the Hugging Face token from environment variable
|
10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
11 |
+
if not HF_TOKEN:
|
12 |
+
raise ValueError("HF_TOKEN environment variable is not set. Please set it before running the application.")
|
13 |
|
14 |
# Load the processed legal code data
|
15 |
@st.cache_resource
|
|
|
22 |
def load_model():
|
23 |
return SentenceTransformer('distiluse-base-multilingual-cased-v1')
|
24 |
|
25 |
+
async def generate_keywords(query):
|
26 |
+
client = InferenceClient(token=HF_TOKEN)
|
27 |
+
|
28 |
+
prompt = f"Na podstawie poniższego pytania, wygeneruj 3-5 słów kluczowych, które najlepiej opisują główne tematy i koncepcje prawne zawarte w pytaniu. Podaj tylko słowa kluczowe, oddzielone przecinkami.\n\nPytanie: {query}\n\nSłowa kluczowe:"
|
29 |
+
|
30 |
+
response = await client.text_generation(
|
31 |
+
"Qwen/Qwen2.5-72B-Instruct",
|
32 |
+
prompt,
|
33 |
+
max_new_tokens=50,
|
34 |
+
temperature=0.3,
|
35 |
+
top_p=0.9
|
36 |
+
)
|
37 |
+
|
38 |
+
keywords = [keyword.strip() for keyword in response.split(',')]
|
39 |
+
return keywords
|
40 |
+
|
41 |
+
def search_relevant_chunks(keywords, chunks, model, top_k=3):
|
42 |
+
keyword_embedding = model.encode(keywords, convert_to_tensor=True)
|
43 |
chunk_embeddings = model.encode([chunk['text'] for chunk in chunks], convert_to_tensor=True)
|
44 |
|
45 |
+
cos_scores = util.pytorch_cos_sim(keyword_embedding, chunk_embeddings)
|
46 |
+
top_results = torch.topk(cos_scores.mean(dim=0), k=top_k)
|
47 |
|
48 |
return [chunks[idx] for idx in top_results.indices]
|
49 |
|
50 |
+
async def generate_ai_response(query, relevant_chunks):
|
51 |
+
client = InferenceClient(token=HF_TOKEN)
|
52 |
+
|
53 |
+
context = "Kontekst prawny:\n\n"
|
54 |
+
for chunk in relevant_chunks:
|
55 |
+
context += f"{chunk['metadata']['nazwa']} - Artykuł {chunk['metadata']['article']}:\n"
|
56 |
+
context += f"{chunk['text']}\n\n"
|
57 |
+
|
58 |
+
messages = [
|
59 |
+
{"role": "system", "content": "Jesteś asystentem prawniczym. Odpowiadaj na pytania na podstawie podanego kontekstu prawnego."},
|
60 |
+
{"role": "user", "content": f"Kontekst: {context}\n\nPytanie: {query}"}
|
61 |
+
]
|
62 |
+
|
63 |
+
response = ""
|
64 |
+
async for token in client.text_generation(
|
65 |
+
"Qwen/Qwen2.5-72B-Instruct",
|
66 |
+
messages,
|
67 |
+
max_new_tokens=2048,
|
68 |
+
temperature=0.5,
|
69 |
+
top_p=0.7,
|
70 |
+
stream=True
|
71 |
+
):
|
72 |
+
response += token
|
73 |
+
yield token
|
74 |
+
|
75 |
def main():
|
76 |
+
st.title("Chatbot Prawny z AI")
|
77 |
|
78 |
# Load data and model
|
79 |
data_file = "processed_kodeksy.json"
|
|
|
99 |
with st.chat_message("user"):
|
100 |
st.markdown(prompt)
|
101 |
|
102 |
+
# Generate keywords and search for relevant chunks
|
103 |
+
with st.spinner("Analizuję pytanie i szukam odpowiednich informacji..."):
|
104 |
+
keywords = asyncio.run(generate_keywords(prompt))
|
105 |
+
relevant_chunks = search_relevant_chunks(keywords, chunks, model)
|
|
|
|
|
|
|
|
|
106 |
|
107 |
+
# Generate AI response
|
108 |
with st.chat_message("assistant"):
|
109 |
+
message_placeholder = st.empty()
|
110 |
+
full_response = ""
|
111 |
+
for chunk in asyncio.run(generate_ai_response(prompt, relevant_chunks)):
|
112 |
+
full_response += chunk
|
113 |
+
message_placeholder.markdown(full_response + "▌")
|
114 |
+
message_placeholder.markdown(full_response)
|
115 |
+
|
116 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
117 |
|
118 |
# Sidebar for additional options
|
119 |
with st.sidebar:
|