Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
try:
|
| 5 |
+
asyncio.get_running_loop()
|
| 6 |
+
except RuntimeError:
|
| 7 |
+
loop = asyncio.new_event_loop()
|
| 8 |
+
asyncio.set_event_loop(loop)
|
| 9 |
+
|
| 10 |
+
import streamlit as st
|
| 11 |
+
from langchain_community.vectorstores import FAISS
|
| 12 |
+
from langchain_community.document_loaders import PyMuPDFLoader, Docx2txtLoader
|
| 13 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 14 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 15 |
+
from langchain_ollama import OllamaLLM
|
| 16 |
+
from langchain.docstore.document import Document
|
| 17 |
+
|
| 18 |
+
os.environ["STREAMLIT_WATCHER_TYPE"] = "none"
|
| 19 |
+
|
| 20 |
+
# π§ Cache Ollama model
|
| 21 |
+
@st.cache_resource
|
| 22 |
+
def load_llm():
|
| 23 |
+
return OllamaLLM(model="phi3:mini",
|
| 24 |
+
temperature=0.2,
|
| 25 |
+
top_p=0.95,
|
| 26 |
+
num_ctx=2048,
|
| 27 |
+
max_tokens=200)
|
| 28 |
+
|
| 29 |
+
# π§ Cache embedder
|
| 30 |
+
@st.cache_resource
|
| 31 |
+
def load_embedder():
|
| 32 |
+
return HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 33 |
+
|
| 34 |
+
llm = load_llm()
|
| 35 |
+
embedder = load_embedder()
|
| 36 |
+
|
| 37 |
+
# Sidebar Upload
|
| 38 |
+
st.sidebar.title("π Upload Terms & Conditions")
|
| 39 |
+
input_mode = st.sidebar.radio("Choose Input Method", ["π Paste Text", "π Upload File"])
|
| 40 |
+
|
| 41 |
+
uploaded_text = ""
|
| 42 |
+
if input_mode == "π Paste Text":
|
| 43 |
+
uploaded_text = st.sidebar.text_area("Paste your T&C text here")
|
| 44 |
+
elif input_mode == "π Upload File":
|
| 45 |
+
uploaded_file = st.sidebar.file_uploader("Upload a .txt, .pdf, or .docx file", type=["txt", "pdf", "docx"])
|
| 46 |
+
if uploaded_file:
|
| 47 |
+
if uploaded_file.type == "text/plain":
|
| 48 |
+
uploaded_text = uploaded_file.read().decode("utf-8")
|
| 49 |
+
elif uploaded_file.type == "application/pdf":
|
| 50 |
+
with open("temp.pdf", "wb") as f:
|
| 51 |
+
f.write(uploaded_file.read())
|
| 52 |
+
docs = PyMuPDFLoader("temp.pdf").load()
|
| 53 |
+
uploaded_text = "\n".join([d.page_content for d in docs])
|
| 54 |
+
elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
| 55 |
+
with open("temp.docx", "wb") as f:
|
| 56 |
+
f.write(uploaded_file.read())
|
| 57 |
+
docs = Docx2txtLoader("temp.docx").load()
|
| 58 |
+
uploaded_text = "\n".join([d.page_content for d in docs])
|
| 59 |
+
|
| 60 |
+
# β
Vectorstore setup
|
| 61 |
+
if uploaded_text:
|
| 62 |
+
st.success("β
Document loaded and processed!")
|
| 63 |
+
|
| 64 |
+
if "db" not in st.session_state:
|
| 65 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 66 |
+
documents = text_splitter.create_documents([uploaded_text])
|
| 67 |
+
db = FAISS.from_documents(documents, embedder)
|
| 68 |
+
st.session_state.db = db
|
| 69 |
+
st.session_state.chat_history = []
|
| 70 |
+
|
| 71 |
+
# π¬ Chat section
|
| 72 |
+
if "db" in st.session_state:
|
| 73 |
+
st.title("π§Ύ Legal Assistant Chat")
|
| 74 |
+
st.markdown("Ask anything about the uploaded document.")
|
| 75 |
+
|
| 76 |
+
user_input = st.chat_input("Type your question here...")
|
| 77 |
+
|
| 78 |
+
if user_input:
|
| 79 |
+
with st.spinner("π€ Thinking..."):
|
| 80 |
+
retriever = st.session_state.db.as_retriever(search_kwargs={"k": 3})
|
| 81 |
+
docs = retriever.invoke(user_input)
|
| 82 |
+
context = "\n\n".join([doc.page_content for doc in docs])
|
| 83 |
+
|
| 84 |
+
prompt = f"""You are a helpful legal assistant.
|
| 85 |
+
Based on the following contract, answer the user's question, This application built by Vighnesh.
|
| 86 |
+
|
| 87 |
+
Context:
|
| 88 |
+
{context}
|
| 89 |
+
|
| 90 |
+
Question:
|
| 91 |
+
{user_input}
|
| 92 |
+
|
| 93 |
+
Answer:"""
|
| 94 |
+
|
| 95 |
+
answer = llm.invoke(prompt)
|
| 96 |
+
|
| 97 |
+
# Save chat history
|
| 98 |
+
st.session_state.chat_history.append(("user", user_input))
|
| 99 |
+
st.session_state.chat_history.append(("assistant", answer))
|
| 100 |
+
|
| 101 |
+
# Display chat history
|
| 102 |
+
for role, message in st.session_state.chat_history:
|
| 103 |
+
if role == "user":
|
| 104 |
+
st.chat_message("user").write(message)
|
| 105 |
+
else:
|
| 106 |
+
st.chat_message("assistant").write(message)
|