Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,44 @@
|
|
|
|
|
|
|
|
| 1 |
from pinecone import Pinecone, ServerlessSpec
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
pc = Pinecone(api_key=pinecone_api_key)
|
| 5 |
|
| 6 |
-
# Connect to your existing index
|
| 7 |
index_name = "legal-docs-index-dji2ip8"
|
| 8 |
index = pc.Index(index_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
from pinecone import Pinecone, ServerlessSpec
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
import numpy as np
|
| 6 |
+
from datasets import load_dataset
|
| 7 |
|
| 8 |
+
# β
Step 1: Fetch Pinecone API key from Hugging Face secrets
|
| 9 |
+
if "PINECONE_API_KEY" not in st.secrets:
|
| 10 |
+
st.error("π¨ Pinecone API key not found! Please set it in Hugging Face secrets.")
|
| 11 |
+
st.stop()
|
| 12 |
+
|
| 13 |
+
pinecone_api_key = st.secrets["PINECONE_API_KEY"] # β
Now it's properly defined
|
| 14 |
+
|
| 15 |
+
# β
Step 2: Initialize Pinecone client
|
| 16 |
pc = Pinecone(api_key=pinecone_api_key)
|
| 17 |
|
| 18 |
+
# β
Step 3: Connect to your existing Pinecone index
|
| 19 |
index_name = "legal-docs-index-dji2ip8"
|
| 20 |
index = pc.Index(index_name)
|
| 21 |
+
|
| 22 |
+
# β
Step 4: Load embedding model
|
| 23 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 24 |
+
|
| 25 |
+
# β
Step 5: Load dataset (for reference)
|
| 26 |
+
dataset = load_dataset("macadeliccc/US-LegalKit", split="train")
|
| 27 |
+
law_texts = [item['text'] for item in dataset if 'text' in item]
|
| 28 |
+
|
| 29 |
+
# β
Step 6: Function to search Pinecone index
|
| 30 |
+
def search_pinecone(query, top_k=5):
|
| 31 |
+
query_embedding = model.encode([query]).tolist()
|
| 32 |
+
results = index.query(query_embedding, top_k=top_k, include_metadata=True)
|
| 33 |
+
return [match['metadata']['text'] for match in results['matches']]
|
| 34 |
+
|
| 35 |
+
# β
Step 7: Streamlit UI
|
| 36 |
+
st.title("π Legal AI Assistant (US-LegalKit)")
|
| 37 |
+
|
| 38 |
+
query = st.text_input("π Enter your legal query:")
|
| 39 |
+
|
| 40 |
+
if query:
|
| 41 |
+
results = search_pinecone(query)
|
| 42 |
+
st.write("### π Relevant Legal Documents:")
|
| 43 |
+
for i, doc in enumerate(results, 1):
|
| 44 |
+
st.write(f"**{i}.** {doc[:500]}...")
|