Update app.py
Browse files
app.py
CHANGED
@@ -1,144 +1,129 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
import os
|
3 |
-
import
|
4 |
-
import
|
5 |
-
from
|
6 |
-
import
|
7 |
-
from
|
8 |
-
from
|
9 |
-
from
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
# Avatars
|
15 |
-
USER_AVATAR = "
|
16 |
-
BOT_AVATAR = "
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
- π€ AI Practitioner Specialist
|
27 |
-
- π Founder of ValonyLabs (AI solutions for industrial corrosion, retail analytics, and KPI monitoring)
|
28 |
-
|
29 |
-
**Capabilities:**
|
30 |
-
- Technical document analysis
|
31 |
-
- Engineering insights
|
32 |
-
- AI-powered problem solving
|
33 |
-
- Cross-domain knowledge integration
|
34 |
-
|
35 |
-
Ask me about engineering challenges, AI applications, or industry best practices!
|
36 |
-
"""
|
37 |
-
|
38 |
-
# UI Setup
|
39 |
-
st.markdown("""
|
40 |
-
<style>
|
41 |
-
@import url('https://fonts.cdnfonts.com/css/tw-cen-mt');
|
42 |
-
* { font-family: 'Tw Cen MT', sans-serif; }
|
43 |
-
.st-emotion-cache-1y4p8pa { padding: 2rem 1rem; }
|
44 |
-
</style>
|
45 |
-
""", unsafe_allow_html=True)
|
46 |
-
|
47 |
-
st.title("π Ataliba o Agent Nerdx π")
|
48 |
-
|
49 |
-
# Sidebar
|
50 |
with st.sidebar:
|
51 |
-
st.header("
|
52 |
-
st.
|
53 |
-
|
54 |
-
|
55 |
-
# Session
|
56 |
-
if "
|
57 |
-
st.session_state.
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
# File parser
|
62 |
-
def parse_file(file):
|
63 |
-
try:
|
64 |
-
if file.type == "application/pdf":
|
65 |
-
reader = PyPDF2.PdfReader(file)
|
66 |
-
return "\n".join([page.extract_text() for page in reader.pages])
|
67 |
-
elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
68 |
-
doc = Document(file)
|
69 |
-
return "\n".join([para.text for para in doc.paragraphs])
|
70 |
-
elif file.type in ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.ms-excel"]:
|
71 |
-
df = pd.read_excel(file)
|
72 |
-
return df.to_string()
|
73 |
-
except Exception as e:
|
74 |
-
st.error(f"Error processing file: {str(e)}")
|
75 |
-
return None
|
76 |
-
|
77 |
-
# Process file
|
78 |
-
if uploaded_file and not st.session_state.file_context:
|
79 |
-
st.session_state.file_context = parse_file(uploaded_file)
|
80 |
-
if st.session_state.file_context:
|
81 |
-
st.sidebar.success("β
Document loaded successfully")
|
82 |
-
|
83 |
-
# Load model
|
84 |
@st.cache_resource
|
85 |
-
def
|
86 |
-
|
87 |
-
|
88 |
-
model
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
)
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
except Exception as e:
|
123 |
-
yield f"β οΈ Model Error: {str(e)}"
|
124 |
-
|
125 |
-
# Chat interface
|
126 |
-
for msg in st.session_state.chat_history:
|
127 |
with st.chat_message(msg["role"], avatar=USER_AVATAR if msg["role"] == "user" else BOT_AVATAR):
|
128 |
st.markdown(msg["content"])
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
138 |
|
139 |
-
|
140 |
-
full_response += chunk
|
141 |
-
response_placeholder.markdown(full_response + "β")
|
142 |
|
143 |
-
|
144 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import torch
|
3 |
import os
|
4 |
+
import tempfile
|
5 |
+
from threading import Thread
|
6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
7 |
+
from langchain_community.document_loaders import PyPDFLoader, TextLoader
|
8 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
9 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
10 |
+
from langchain.vectorstores import FAISS
|
11 |
+
from langchain.retrievers import BM25Retriever, EnsembleRetriever
|
12 |
+
from langchain.schema import Document
|
13 |
+
from langchain.docstore.document import Document as LangchainDocument
|
14 |
+
|
15 |
+
# --- Avatars ---
|
16 |
+
USER_AVATAR = "π€"
|
17 |
+
BOT_AVATAR = "π€"
|
18 |
+
|
19 |
+
# --- HF Token ---
|
20 |
+
HF_TOKEN = st.secrets["HF_TOKEN"]
|
21 |
+
|
22 |
+
# --- Page Config ---
|
23 |
+
st.set_page_config(page_title="Hybrid RAG with Streaming", page_icon="π", layout="centered")
|
24 |
+
st.title("π Hybrid Search + Streaming Chat")
|
25 |
+
|
26 |
+
# --- Sidebar Upload ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
with st.sidebar:
|
28 |
+
st.header("π€ Upload Documents")
|
29 |
+
uploaded_files = st.file_uploader("Upload PDFs or .txt files", type=["pdf", "txt"], accept_multiple_files=True)
|
30 |
+
clear_chat = st.button("π§Ή Clear Conversation")
|
31 |
+
|
32 |
+
# --- Session State ---
|
33 |
+
if "messages" not in st.session_state or clear_chat:
|
34 |
+
st.session_state.messages = []
|
35 |
+
|
36 |
+
# --- Load LLM ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
@st.cache_resource
|
38 |
+
def load_model():
|
39 |
+
model_id = "tiiuae/falcon-7b-instruct"
|
40 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=HF_TOKEN)
|
41 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto", token=HF_TOKEN)
|
42 |
+
return tokenizer, model
|
43 |
+
|
44 |
+
tokenizer, model = load_model()
|
45 |
+
|
46 |
+
# --- Load & Chunk Documents ---
|
47 |
+
def process_documents(files):
|
48 |
+
documents = []
|
49 |
+
for file in files:
|
50 |
+
suffix = ".pdf" if file.name.endswith(".pdf") else ".txt"
|
51 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp_file:
|
52 |
+
tmp_file.write(file.read())
|
53 |
+
tmp_file_path = tmp_file.name
|
54 |
+
loader = PyPDFLoader(tmp_file_path) if suffix == ".pdf" else TextLoader(tmp_file_path)
|
55 |
+
documents.extend(loader.load())
|
56 |
+
return documents
|
57 |
+
|
58 |
+
def chunk_documents(documents):
|
59 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
60 |
+
return splitter.split_documents(documents)
|
61 |
+
|
62 |
+
def build_hybrid_retriever(chunks):
|
63 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
64 |
+
faiss_store = FAISS.from_documents(chunks, embeddings)
|
65 |
+
faiss_retriever = faiss_store.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
66 |
+
|
67 |
+
bm25_retriever = BM25Retriever.from_documents([LangchainDocument(page_content=d.page_content) for d in chunks])
|
68 |
+
bm25_retriever.k = 5
|
69 |
+
|
70 |
+
hybrid = EnsembleRetriever(retrievers=[faiss_retriever, bm25_retriever], weights=[0.5, 0.5])
|
71 |
+
return hybrid
|
72 |
+
|
73 |
+
# --- Prompt Construction ---
|
74 |
+
def build_prompt(history, context=""):
|
75 |
+
prompt = (
|
76 |
+
"You are DigiTwin, an expert in reliability, inspection, and maintenance of piping, structures, vessels, and topside assets.\n"
|
77 |
+
f"Use the following context to help answer questions:\n\n{context}\n\n"
|
78 |
)
|
79 |
+
for turn in history:
|
80 |
+
role = "User" if turn["role"] == "user" else "Assistant"
|
81 |
+
prompt += f"{role}: {turn['content']}\n"
|
82 |
+
prompt += "Assistant:"
|
83 |
+
return prompt
|
84 |
+
|
85 |
+
# --- Generator for Streaming ---
|
86 |
+
def generate_streaming_response(prompt):
|
87 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
88 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
89 |
+
thread = Thread(target=model.generate, kwargs={**inputs, "streamer": streamer, "max_new_tokens": 300})
|
90 |
+
thread.start()
|
91 |
+
output = ""
|
92 |
+
for token in streamer:
|
93 |
+
output += token
|
94 |
+
yield output
|
95 |
+
|
96 |
+
# --- Run Document Processing and Retrieval ---
|
97 |
+
retriever = None
|
98 |
+
if uploaded_files:
|
99 |
+
with st.spinner("π Processing documents..."):
|
100 |
+
docs = process_documents(uploaded_files)
|
101 |
+
chunks = chunk_documents(docs)
|
102 |
+
retriever = build_hybrid_retriever(chunks)
|
103 |
+
st.success("β
Document processing complete.")
|
104 |
+
|
105 |
+
# --- Display Past Messages ---
|
106 |
+
for msg in st.session_state.messages:
|
|
|
|
|
|
|
|
|
|
|
107 |
with st.chat_message(msg["role"], avatar=USER_AVATAR if msg["role"] == "user" else BOT_AVATAR):
|
108 |
st.markdown(msg["content"])
|
109 |
|
110 |
+
# --- Main Chat Input ---
|
111 |
+
if prompt := st.chat_input("Ask a question..."):
|
112 |
+
st.chat_message("user", avatar=USER_AVATAR).markdown(prompt)
|
113 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
114 |
|
115 |
+
context = ""
|
116 |
+
if retriever:
|
117 |
+
docs = retriever.get_relevant_documents(prompt)
|
118 |
+
context = "\n\n".join([doc.page_content for doc in docs])
|
119 |
|
120 |
+
full_prompt = build_prompt(st.session_state.messages, context=context)
|
|
|
|
|
121 |
|
122 |
+
with st.chat_message("assistant", avatar=BOT_AVATAR):
|
123 |
+
response_container = st.empty()
|
124 |
+
answer = ""
|
125 |
+
for chunk in generate_streaming_response(full_prompt):
|
126 |
+
answer = chunk
|
127 |
+
response_container.markdown(answer + "β", unsafe_allow_html=True)
|
128 |
+
response_container.markdown(answer)
|
129 |
+
st.session_state.messages.append({"role": "assistant", "content": answer})
|