Files changed (1) hide show
  1. app.py +26 -15
app.py CHANGED
@@ -1,31 +1,40 @@
1
  import streamlit as st
 
 
2
 
3
- # Correct imports (new structure)
4
  from langchain_community.document_loaders import PyPDFLoader, TextLoader
5
  from langchain_text_splitters import RecursiveCharacterTextSplitter
6
  from langchain_community.embeddings import HuggingFaceEmbeddings
7
  from langchain_community.vectorstores import FAISS
8
  from langchain_community.llms import HuggingFacePipeline
9
-
10
  from langchain.chains import RetrievalQA
 
11
  from transformers import pipeline
12
 
13
 
14
  # -------------------------------
15
- # Load Documents
16
  # -------------------------------
17
  def load_documents(uploaded_files):
18
  documents = []
 
19
  for file in uploaded_files:
20
- with open(file.name, "wb") as f:
21
- f.write(file.getbuffer())
22
 
23
- if file.name.endswith(".pdf"):
24
- loader = PyPDFLoader(file.name)
 
 
 
 
25
  else:
26
- loader = TextLoader(file.name)
27
 
28
  documents.extend(loader.load())
 
 
 
29
  return documents
30
 
31
 
@@ -51,19 +60,21 @@ def create_vectorstore(chunks):
51
 
52
 
53
  # -------------------------------
54
- # Load Local LLM (FREE)
55
  # -------------------------------
 
56
  def load_llm():
57
  pipe = pipeline(
58
- "text2text-generation", # ✅ FIXED
59
  model="google/flan-t5-base",
60
- max_length=512
 
61
  )
62
  return HuggingFacePipeline(pipeline=pipe)
63
 
64
 
65
  # -------------------------------
66
- # Build QA Chain (with strict prompt)
67
  # -------------------------------
68
  def build_qa(vectorstore):
69
  llm = load_llm()
@@ -94,12 +105,12 @@ if uploaded_files:
94
  vectorstore = create_vectorstore(chunks)
95
  qa_chain = build_qa(vectorstore)
96
 
97
- st.success("Documents ready!")
98
 
99
  query = st.text_input("Ask a question from your documents")
100
 
101
  if query:
102
  with st.spinner("Generating answer..."):
103
- result = qa_chain.run(query)
104
  st.write("### Answer:")
105
- st.write(result)
 
1
  import streamlit as st
2
+ import tempfile
3
+ import os
4
 
5
+ # LangChain imports (new structure)
6
  from langchain_community.document_loaders import PyPDFLoader, TextLoader
7
  from langchain_text_splitters import RecursiveCharacterTextSplitter
8
  from langchain_community.embeddings import HuggingFaceEmbeddings
9
  from langchain_community.vectorstores import FAISS
10
  from langchain_community.llms import HuggingFacePipeline
 
11
  from langchain.chains import RetrievalQA
12
+
13
  from transformers import pipeline
14
 
15
 
16
  # -------------------------------
17
+ # Load Documents (FIXED temp file handling)
18
  # -------------------------------
19
  def load_documents(uploaded_files):
20
  documents = []
21
+
22
  for file in uploaded_files:
23
+ suffix = file.name.split(".")[-1]
 
24
 
25
+ with tempfile.NamedTemporaryFile(delete=False, suffix=f".{suffix}") as tmp:
26
+ tmp.write(file.getbuffer())
27
+ tmp_path = tmp.name
28
+
29
+ if suffix == "pdf":
30
+ loader = PyPDFLoader(tmp_path)
31
  else:
32
+ loader = TextLoader(tmp_path)
33
 
34
  documents.extend(loader.load())
35
+
36
+ os.remove(tmp_path) # cleanup
37
+
38
  return documents
39
 
40
 
 
60
 
61
 
62
  # -------------------------------
63
+ # Load Local LLM (STABLE VERSION)
64
  # -------------------------------
65
+ @st.cache_resource
66
  def load_llm():
67
  pipe = pipeline(
68
+ task="text2text-generation",
69
  model="google/flan-t5-base",
70
+ max_length=512,
71
+ do_sample=False
72
  )
73
  return HuggingFacePipeline(pipeline=pipe)
74
 
75
 
76
  # -------------------------------
77
+ # Build QA Chain
78
  # -------------------------------
79
  def build_qa(vectorstore):
80
  llm = load_llm()
 
105
  vectorstore = create_vectorstore(chunks)
106
  qa_chain = build_qa(vectorstore)
107
 
108
+ st.success("Documents ready!")
109
 
110
  query = st.text_input("Ask a question from your documents")
111
 
112
  if query:
113
  with st.spinner("Generating answer..."):
114
+ result = qa_chain.invoke({"query": query})
115
  st.write("### Answer:")
116
+ st.write(result["result"])