Update app.py
Browse files
app.py
CHANGED
|
@@ -22,9 +22,10 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 22 |
# Load sentence transformer for embeddings
|
| 23 |
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
uploaded_file = st.file_uploader("
|
|
|
|
| 28 |
|
| 29 |
# Extract text from file
|
| 30 |
def extract_text(file):
|
|
@@ -56,18 +57,25 @@ def retrieve_chunks(query, chunks, index, embeddings, k=5):
|
|
| 56 |
return [chunks[i] for i in I[0]]
|
| 57 |
|
| 58 |
# --- MAIN LOGIC ---
|
|
|
|
|
|
|
| 59 |
if uploaded_file:
|
| 60 |
-
|
| 61 |
|
| 62 |
raw_text = extract_text(uploaded_file)
|
| 63 |
chunks = split_into_chunks(raw_text)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
index, embeddings = create_faiss_index(chunks)
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
if user_question:
|
| 73 |
with st.spinner("Thinking..."):
|
|
@@ -103,5 +111,10 @@ if uploaded_file:
|
|
| 103 |
else:
|
| 104 |
answer = generated_text.replace(prompt, "").strip()
|
| 105 |
|
| 106 |
-
|
| 107 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Load sentence transformer for embeddings
|
| 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):
|
|
|
|
| 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 |
+
file_display_area.success("β
File uploaded successfully!")
|
| 64 |
|
| 65 |
raw_text = extract_text(uploaded_file)
|
| 66 |
chunks = split_into_chunks(raw_text)
|
| 67 |
|
| 68 |
+
file_display_area.info(f"π Document split into {len(chunks)} chunks")
|
| 69 |
+
file_display_area.text_area("π Extracted Document Text", raw_text, height=200)
|
| 70 |
|
| 71 |
index, embeddings = create_faiss_index(chunks)
|
| 72 |
|
| 73 |
+
# Chat-like interface
|
| 74 |
+
st.markdown("### π¬ Chat with the Document")
|
| 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..."):
|
|
|
|
| 111 |
else:
|
| 112 |
answer = generated_text.replace(prompt, "").strip()
|
| 113 |
|
| 114 |
+
# Append interaction to chat history
|
| 115 |
+
st.session_state.chat_history.append({"question": user_question, "answer": answer})
|
| 116 |
+
|
| 117 |
+
# Display chat history
|
| 118 |
+
for chat in st.session_state.chat_history:
|
| 119 |
+
st.markdown(f"**You:** {chat['question']}")
|
| 120 |
+
st.markdown(f"**Phi-2:** {chat['answer']}")
|