jk12p commited on
Commit
31d7b80
Β·
verified Β·
1 Parent(s): da4682c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -9
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
- st.title("πŸ” RAG App using πŸ€– Phi-2")
26
-
27
- uploaded_file = st.file_uploader("πŸ“„ Upload a PDF or TXT file", type=["pdf", "txt"])
 
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
- st.success("βœ… File uploaded successfully!")
61
 
62
  raw_text = extract_text(uploaded_file)
63
  chunks = split_into_chunks(raw_text)
64
 
65
- st.info(f"πŸ“š Document split into {len(chunks)} chunks")
66
- st.text_area("πŸ“„ Extracted Document Text (for debugging)", raw_text, height=200)
67
 
68
  index, embeddings = create_faiss_index(chunks)
69
 
70
- user_question = st.text_input("πŸ’¬ Ask something about the document:")
 
 
 
 
 
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
- st.markdown("### 🧠 Answer:")
107
- st.success(answer)
 
 
 
 
 
 
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']}")