modified UI
Browse files
app.py
CHANGED
|
@@ -8,24 +8,34 @@ import numpy as np
|
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
|
| 10 |
# --- CONFIG ---
|
| 11 |
-
HF_TOKEN = os.environ["HF_TOKEN"]
|
|
|
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", token=HF_TOKEN)
|
| 15 |
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
"microsoft/phi-2",
|
| 17 |
-
torch_dtype=torch.float16,
|
| 18 |
-
|
| 19 |
-
token=HF_TOKEN
|
| 20 |
-
)
|
| 21 |
|
| 22 |
-
# Load sentence transformer
|
| 23 |
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# Sidebar for file upload and display
|
| 26 |
-
st.sidebar.title("π File Upload")
|
| 27 |
-
uploaded_file = st.sidebar.file_uploader("Upload a PDF or TXT file", type=["pdf", "txt"])
|
| 28 |
-
file_display_area = st.sidebar.empty()
|
| 29 |
|
| 30 |
# Extract text from file
|
| 31 |
def extract_text(file):
|
|
@@ -38,83 +48,103 @@ def extract_text(file):
|
|
| 38 |
text = file.read().decode("utf-8")
|
| 39 |
return text
|
| 40 |
|
|
|
|
| 41 |
# Split into chunks
|
| 42 |
def split_into_chunks(text, chunk_size=500):
|
| 43 |
return [text[i:i + chunk_size] for i in range(0, len(text), chunk_size)]
|
| 44 |
|
|
|
|
| 45 |
# Create FAISS index
|
| 46 |
def create_faiss_index(chunks):
|
| 47 |
-
embeddings = embedder.encode(chunks)
|
| 48 |
-
|
|
|
|
| 49 |
index = faiss.IndexFlatL2(dim)
|
| 50 |
-
index.add(
|
| 51 |
-
return index,
|
|
|
|
| 52 |
|
| 53 |
# Retrieve top-k chunks
|
| 54 |
-
def retrieve_chunks(query, chunks, index,
|
| 55 |
-
query_embedding = embedder.encode([query])
|
| 56 |
-
|
|
|
|
| 57 |
return [chunks[i] for i in I[0]]
|
| 58 |
|
| 59 |
-
# --- MAIN LOGIC ---
|
| 60 |
-
st.title("π RAG App using π€ Phi-2")
|
| 61 |
|
|
|
|
| 62 |
if uploaded_file:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
index, embeddings = create_faiss_index(chunks)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
st.markdown("### π¬ Chat with
|
| 75 |
-
if "chat_history" not in st.session_state:
|
| 76 |
-
st.session_state.chat_history = []
|
| 77 |
-
|
| 78 |
-
user_question = st.text_input("Ask something about the document:")
|
| 79 |
-
|
| 80 |
-
if user_question:
|
| 81 |
-
with st.spinner("Thinking..."):
|
| 82 |
-
context = "\n".join(retrieve_chunks(user_question, chunks, index, embeddings))
|
| 83 |
-
|
| 84 |
-
# Updated prompt for Phi-2's instruction style
|
| 85 |
-
prompt = (
|
| 86 |
-
f"Instruction: Answer the following question using only the context provided. "
|
| 87 |
-
f"Extract specific information directly from the context when available. "
|
| 88 |
-
f"If the answer is not in the context, respond with 'Information not found.'\n\n"
|
| 89 |
-
f"Context:\n{context}\n\n"
|
| 90 |
-
f"Question: {user_question}\n\n"
|
| 91 |
-
f"Answer: "
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
| 95 |
-
|
| 96 |
-
with torch.no_grad():
|
| 97 |
-
outputs = model.generate(
|
| 98 |
-
input_ids,
|
| 99 |
-
max_new_tokens=512, # Keep using max_new_tokens as fixed before
|
| 100 |
-
num_return_sequences=1,
|
| 101 |
-
temperature=0.2, # Lower temperature for more focused answers
|
| 102 |
-
do_sample=True, # Enable sampling for more natural responses
|
| 103 |
-
top_p=0.9, # Add top_p sampling for better quality
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 107 |
-
|
| 108 |
-
# Extract the answer part - adapt based on Phi-2's output format
|
| 109 |
-
if "Answer:" in generated_text:
|
| 110 |
-
answer = generated_text.split("Answer:")[-1].strip()
|
| 111 |
-
else:
|
| 112 |
-
answer = generated_text.replace(prompt, "").strip()
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
|
|
|
| 116 |
|
| 117 |
# Display chat history
|
| 118 |
-
for
|
| 119 |
-
st.
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
|
| 10 |
# --- CONFIG ---
|
| 11 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
| 12 |
+
# Your Hugging Face token
|
| 13 |
|
| 14 |
+
# Check if CUDA (GPU) is available
|
| 15 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
+
|
| 17 |
+
# Move device info to sidebar
|
| 18 |
+
with st.sidebar:
|
| 19 |
+
st.text(f"π₯οΈ Using device: {DEVICE}")
|
| 20 |
+
|
| 21 |
+
# Load tokenizer and model
|
| 22 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", token=HF_TOKEN)
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
"microsoft/phi-2",
|
| 25 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 26 |
+
).to(DEVICE)
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# Load sentence transformer and move to device
|
| 29 |
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 30 |
+
embedder = embedder.to(DEVICE)
|
| 31 |
+
|
| 32 |
+
st.title("π RAG App using π€ Phi-2")
|
| 33 |
+
|
| 34 |
+
# Move file upload and processing to sidebar
|
| 35 |
+
with st.sidebar:
|
| 36 |
+
st.header("π Document Upload")
|
| 37 |
+
uploaded_file = st.file_uploader("π Upload a PDF or TXT file", type=["pdf", "txt"])
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# Extract text from file
|
| 41 |
def extract_text(file):
|
|
|
|
| 48 |
text = file.read().decode("utf-8")
|
| 49 |
return text
|
| 50 |
|
| 51 |
+
|
| 52 |
# Split into chunks
|
| 53 |
def split_into_chunks(text, chunk_size=500):
|
| 54 |
return [text[i:i + chunk_size] for i in range(0, len(text), chunk_size)]
|
| 55 |
|
| 56 |
+
|
| 57 |
# Create FAISS index
|
| 58 |
def create_faiss_index(chunks):
|
| 59 |
+
embeddings = embedder.encode(chunks, convert_to_tensor=True, device=DEVICE)
|
| 60 |
+
embeddings_np = embeddings.cpu().detach().numpy()
|
| 61 |
+
dim = embeddings_np.shape[1]
|
| 62 |
index = faiss.IndexFlatL2(dim)
|
| 63 |
+
index.add(embeddings_np)
|
| 64 |
+
return index, embeddings_np
|
| 65 |
+
|
| 66 |
|
| 67 |
# Retrieve top-k chunks
|
| 68 |
+
def retrieve_chunks(query, chunks, index, k=5):
|
| 69 |
+
query_embedding = embedder.encode([query], convert_to_tensor=True, device=DEVICE)
|
| 70 |
+
query_embedding_np = query_embedding.cpu().detach().numpy()
|
| 71 |
+
D, I = index.search(query_embedding_np, k)
|
| 72 |
return [chunks[i] for i in I[0]]
|
| 73 |
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# --- MAIN LOGIC ---
|
| 76 |
if uploaded_file:
|
| 77 |
+
with st.sidebar:
|
| 78 |
+
st.success("β
File uploaded successfully!")
|
| 79 |
+
raw_text = extract_text(uploaded_file)
|
| 80 |
+
chunks = split_into_chunks(raw_text)
|
| 81 |
+
st.info(f"π Document split into {len(chunks)} chunks")
|
| 82 |
|
| 83 |
+
# Add expander for debugging text in sidebar
|
| 84 |
+
with st.expander("π View Document Text"):
|
| 85 |
+
st.text_area("Extracted text", raw_text, height=200)
|
| 86 |
|
| 87 |
index, embeddings = create_faiss_index(chunks)
|
| 88 |
|
| 89 |
+
# Main chat interface
|
| 90 |
+
st.markdown("### π¬ Chat with your Document")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
# Initialize chat history
|
| 93 |
+
if "messages" not in st.session_state:
|
| 94 |
+
st.session_state.messages = []
|
| 95 |
|
| 96 |
# Display chat history
|
| 97 |
+
for message in st.session_state.messages:
|
| 98 |
+
with st.chat_message(message["role"]):
|
| 99 |
+
st.markdown(message["content"])
|
| 100 |
+
|
| 101 |
+
# Chat input
|
| 102 |
+
if prompt := st.chat_input("Ask something about the document"):
|
| 103 |
+
# Display user message
|
| 104 |
+
with st.chat_message("user"):
|
| 105 |
+
st.markdown(prompt)
|
| 106 |
+
# Add user message to chat history
|
| 107 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 108 |
+
|
| 109 |
+
# Generate response
|
| 110 |
+
with st.chat_message("assistant"):
|
| 111 |
+
with st.spinner("Thinking..."):
|
| 112 |
+
context = "\n".join(retrieve_chunks(prompt, chunks, index))
|
| 113 |
+
|
| 114 |
+
# Updated prompt for Phi-2's instruction style
|
| 115 |
+
full_prompt = (
|
| 116 |
+
f"Instruction: Answer the following question using only the context provided. "
|
| 117 |
+
f"Extract specific information directly from the context when available. "
|
| 118 |
+
f"If the answer is not in the context, respond with 'Information not found.'\n\n"
|
| 119 |
+
f"Context:\n{context}\n\n"
|
| 120 |
+
f"Question: {prompt}\n\n"
|
| 121 |
+
f"Answer: "
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
input_ids = tokenizer.encode(full_prompt, return_tensors="pt").to(DEVICE)
|
| 125 |
+
|
| 126 |
+
with torch.no_grad():
|
| 127 |
+
outputs = model.generate(
|
| 128 |
+
input_ids,
|
| 129 |
+
max_new_tokens=256,
|
| 130 |
+
num_return_sequences=1,
|
| 131 |
+
temperature=0.2,
|
| 132 |
+
do_sample=True,
|
| 133 |
+
top_p=0.9,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 137 |
+
|
| 138 |
+
# Extract the answer part
|
| 139 |
+
if "Answer:" in generated_text:
|
| 140 |
+
answer = generated_text.split("Answer:")[-1].strip()
|
| 141 |
+
else:
|
| 142 |
+
answer = generated_text.replace(full_prompt, "").strip()
|
| 143 |
+
|
| 144 |
+
st.markdown(answer)
|
| 145 |
+
# Add assistant response to chat history
|
| 146 |
+
st.session_state.messages.append({"role": "assistant", "content": answer})
|
| 147 |
+
|
| 148 |
+
else:
|
| 149 |
+
# Display message when no file is uploaded
|
| 150 |
+
st.info("π Please upload a document using the sidebar to start chatting!")
|