Hidayatmahar commited on
Commit
255ab1b
Β·
verified Β·
1 Parent(s): ef7a3be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -2
app.py CHANGED
@@ -1,8 +1,44 @@
 
 
1
  from pinecone import Pinecone, ServerlessSpec
 
 
 
2
 
3
- # Initialize Pinecone
 
 
 
 
 
 
 
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]}...")