Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import faiss
|
|
| 3 |
import numpy as np
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
import openai
|
|
|
|
| 6 |
|
| 7 |
# Load FAISS index
|
| 8 |
index = faiss.read_index("faiss_index.bin")
|
|
@@ -10,28 +11,29 @@ index = faiss.read_index("faiss_index.bin")
|
|
| 10 |
# Load embedding model
|
| 11 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# OpenAI API Key (store it as a secret in Hugging Face)
|
| 14 |
openai.api_key = st.secrets["GROQ_API_KEY"]
|
| 15 |
|
| 16 |
-
# Load the preprocessed Pile Law dataset (replace with your dataset path)
|
| 17 |
-
law_data = ["Sample Legal Document 1...", "Sample Legal Document 2..."] # Replace with actual data loading
|
| 18 |
-
|
| 19 |
# Function to search relevant legal documents
|
| 20 |
def search_legal_docs(query, top_k=5):
|
| 21 |
query_embedding = model.encode([query])
|
| 22 |
_, idxs = index.search(query_embedding, top_k)
|
| 23 |
-
return [
|
| 24 |
|
| 25 |
# Streamlit UI
|
| 26 |
-
st.title("π Legal AI Assistant (
|
| 27 |
|
| 28 |
query = st.text_input("π Enter your legal query:")
|
| 29 |
|
| 30 |
if query:
|
| 31 |
results = search_legal_docs(query)
|
| 32 |
st.write("### π Relevant Legal Documents:")
|
| 33 |
-
for
|
| 34 |
-
st.write(f"
|
| 35 |
|
| 36 |
# Generate AI-based legal response
|
| 37 |
response = openai.ChatCompletion.create(
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
import openai
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
|
| 8 |
# Load FAISS index
|
| 9 |
index = faiss.read_index("faiss_index.bin")
|
|
|
|
| 11 |
# Load embedding model
|
| 12 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 13 |
|
| 14 |
+
# Load dataset (only titles for reference)
|
| 15 |
+
dataset = load_dataset("macadeliccc/US-LegalKit", split="train")
|
| 16 |
+
law_texts = [item['text'] for item in dataset if 'text' in item]
|
| 17 |
+
|
| 18 |
# OpenAI API Key (store it as a secret in Hugging Face)
|
| 19 |
openai.api_key = st.secrets["GROQ_API_KEY"]
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
# Function to search relevant legal documents
|
| 22 |
def search_legal_docs(query, top_k=5):
|
| 23 |
query_embedding = model.encode([query])
|
| 24 |
_, idxs = index.search(query_embedding, top_k)
|
| 25 |
+
return [law_texts[i] for i in idxs[0]] # Return matching legal documents
|
| 26 |
|
| 27 |
# Streamlit UI
|
| 28 |
+
st.title("π Legal AI Assistant (US-LegalKit)")
|
| 29 |
|
| 30 |
query = st.text_input("π Enter your legal query:")
|
| 31 |
|
| 32 |
if query:
|
| 33 |
results = search_legal_docs(query)
|
| 34 |
st.write("### π Relevant Legal Documents:")
|
| 35 |
+
for i, doc in enumerate(results, 1):
|
| 36 |
+
st.write(f"**{i}.** {doc[:500]}...") # Show preview of the document
|
| 37 |
|
| 38 |
# Generate AI-based legal response
|
| 39 |
response = openai.ChatCompletion.create(
|