Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
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")
|
9 |
+
|
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 [law_data[i] for i in idxs[0]]
|
24 |
+
|
25 |
+
# Streamlit UI
|
26 |
+
st.title("π Legal AI Assistant (Pile Law)")
|
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 res in results:
|
34 |
+
st.write(f"- {res}")
|
35 |
+
|
36 |
+
# Generate AI-based legal response
|
37 |
+
response = openai.ChatCompletion.create(
|
38 |
+
model="gpt-4",
|
39 |
+
messages=[{"role": "system", "content": "You are a legal assistant."},
|
40 |
+
{"role": "user", "content": query}]
|
41 |
+
)
|
42 |
+
st.write("### π§ββοΈ AI Response:")
|
43 |
+
st.write(response['choices'][0]['message']['content'])
|