MBilal-72 commited on
Commit
837c8fa
Β·
verified Β·
1 Parent(s): 2d6ed01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -61
app.py CHANGED
@@ -1,76 +1,76 @@
1
  import os
 
2
  import streamlit as st
3
- from groq import Groq
4
- from langchain_community.embeddings import HuggingFaceEmbeddings
5
  from langchain_community.vectorstores import FAISS
6
- from langchain.text_splitter import CharacterTextSplitter
7
- from langchain_community.document_loaders import TextLoader, PyPDFLoader
8
  from langchain.chains import RetrievalQA
9
- from langchain.llms.base import LLM
10
- from tempfile import NamedTemporaryFile
11
 
12
- # Load Groq API key from environment variable
13
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
14
- if not GROQ_API_KEY:
15
- st.error("❌ GROQ_API_KEY is not set in environment variables.")
16
- st.stop()
17
-
18
- # Initialize Groq client
19
- groq_client = Groq(api_key=GROQ_API_KEY)
20
-
21
- # Define a basic LLM wrapper for Groq
22
- class GroqLLM(LLM):
23
- def __init__(self, model_name="llama3-8b-8192"):
24
- self.model_name = model_name
25
-
26
- def _call(self, prompt, stop=None):
27
- response = groq_client.chat.completions.create(
28
- model=self.model_name,
29
- messages=[{"role": "user", "content": prompt}],
30
- )
31
- return response.choices[0].message.content.strip()
32
-
33
- @property
34
- def _llm_type(self):
35
- return "groq_llm"
36
-
37
- # Streamlit UI
38
- st.set_page_config(page_title="Groq RAG App", layout="centered")
39
- st.title("πŸ“š RAG App with Groq + LangChain + FAISS")
40
- st.write("Upload a PDF or TXT file, ask a question, and get smart answers.")
41
-
42
- uploaded_file = st.file_uploader("Upload your document", type=["pdf", "txt"])
43
-
44
- if uploaded_file:
45
- with NamedTemporaryFile(delete=False) as tmp_file:
46
  tmp_file.write(uploaded_file.read())
47
  tmp_path = tmp_file.name
48
 
49
- # Load document based on file type
50
- if uploaded_file.type == "application/pdf":
51
- loader = PyPDFLoader(tmp_path)
52
- else:
53
- loader = TextLoader(tmp_path)
 
 
54
 
55
- docs = loader.load()
 
 
 
 
56
 
57
- # Split into chunks
58
- splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
59
- texts = splitter.split_documents(docs)
60
 
61
- # Create embeddings and FAISS index
62
- embeddings = HuggingFaceEmbeddings()
63
- db = FAISS.from_documents(texts, embeddings)
64
 
65
- # Set up RAG chain
66
- retriever = db.as_retriever()
67
- qa_chain = RetrievalQA.from_chain_type(llm=GroqLLM(), retriever=retriever)
 
 
 
 
68
 
69
- # Input box for user question
70
- query = st.text_input("πŸ” Ask a question about the document:")
 
 
71
 
72
- if query:
73
- with st.spinner("Thinking..."):
74
- result = qa_chain.run(query)
75
- st.markdown("### 🧠 Answer:")
76
- st.success(result)
 
1
  import os
2
+ import tempfile
3
  import streamlit as st
4
+
5
+ from langchain_community.document_loaders import PyPDFLoader
6
  from langchain_community.vectorstores import FAISS
7
+ from langchain_community.embeddings import HuggingFaceEmbeddings
 
8
  from langchain.chains import RetrievalQA
9
+ from langchain.prompts import PromptTemplate
10
+ from langchain_groq import GroqLLM
11
 
12
+ # Set environment variables (You can also use os.environ or Streamlit secrets)
13
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
14
+ HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
15
+
16
+ # Initialize Groq LLM
17
+ llm = GroqLLM(
18
+ api_key=GROQ_API_KEY,
19
+ model="llama3-8b-8192", # <- correct param
20
+ temperature=0.1
21
+ )
22
+
23
+ # HuggingFace Embeddings
24
+ embedding = HuggingFaceEmbeddings()
25
+
26
+ st.title("πŸ“„ RAG Chat with Groq + HuggingFace")
27
+
28
+ # Upload PDF
29
+ uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
30
+
31
+ user_query = st.text_input("Ask something about the document")
32
+ submit_button = st.button("Submit")
33
+
34
+ if uploaded_file and submit_button:
35
+ # Save PDF temporarily
36
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
 
 
 
 
 
 
 
 
 
37
  tmp_file.write(uploaded_file.read())
38
  tmp_path = tmp_file.name
39
 
40
+ # Load and split
41
+ loader = PyPDFLoader(tmp_path)
42
+ pages = loader.load_and_split()
43
+
44
+ # Create FAISS vectorstore
45
+ vectorstore = FAISS.from_documents(pages, embedding)
46
+ retriever = vectorstore.as_retriever()
47
 
48
+ # Custom prompt (optional)
49
+ prompt_template = PromptTemplate(
50
+ input_variables=["context", "question"],
51
+ template="""
52
+ Use the following context to answer the question. Be concise and accurate.
53
 
54
+ Context: {context}
 
 
55
 
56
+ Question: {question}
57
+ """
58
+ )
59
 
60
+ # Create QA chain
61
+ qa_chain = RetrievalQA.from_chain_type(
62
+ llm=llm,
63
+ retriever=retriever,
64
+ return_source_documents=True,
65
+ chain_type_kwargs={"prompt": prompt_template}
66
+ )
67
 
68
+ # Run QA
69
+ result = qa_chain({"query": user_query})
70
+ st.markdown("### πŸ’¬ Answer")
71
+ st.write(result["result"])
72
 
73
+ # Optional: Show sources
74
+ with st.expander("πŸ“„ Sources"):
75
+ for doc in result["source_documents"]:
76
+ st.write(doc.metadata["source"])