Ahmadkhan12 commited on
Commit
250d534
·
verified ·
1 Parent(s): f56a3e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -3,16 +3,21 @@ import os
3
  from langchain.vectorstores import FAISS
4
  from langchain.embeddings import OpenAIEmbeddings
5
  from langchain.document_loaders import PyPDFLoader
6
- from langchain.chains import RetrievalQA
7
  from groq import Groq
8
 
9
- # Set the API Key directly (Not recommended for production)
10
  GROQ_API_KEY = "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"
 
 
 
 
 
 
 
11
 
12
  # Initialize Groq client
13
  def initialize_groq_client():
14
  """Initialize the Groq client with the API key."""
15
- os.environ["GROQ_API_KEY"] = GROQ_API_KEY
16
  return Groq(api_key=GROQ_API_KEY)
17
 
18
  # Generate response using Groq API
@@ -39,7 +44,7 @@ def process_pdf(pdf_path):
39
  # Create FAISS vector database
40
  def create_vector_db(documents):
41
  """Create a FAISS vector database from documents."""
42
- embeddings = OpenAIEmbeddings() # Use OpenAI embeddings for vectorization
43
  vector_db = FAISS.from_documents(documents, embeddings)
44
  return vector_db
45
 
@@ -62,25 +67,24 @@ def main():
62
  documents = process_pdf("uploaded_act.pdf")
63
  st.success("PDF Loaded and Processed Successfully!")
64
 
65
- # Initialize Groq Client
66
- try:
67
- groq_client = initialize_groq_client()
68
- st.success("Groq Client Initialized Successfully!")
69
-
70
- # Build Vector DB and QA Chain
71
- vector_db = create_vector_db(documents)
72
- retriever, client = build_rag_pipeline(vector_db, groq_client)
73
 
74
- # Step 3: Ask Questions
75
- query = st.text_input("Ask a question:")
76
- if query:
77
- with st.spinner("Fetching Answer..."):
78
- response = generate_response(client, query)
79
- st.write("### Answer:")
80
- st.write(response)
81
 
82
- except Exception as e:
83
- st.error(f"Error loading client or processing query: {e}")
 
 
 
 
 
 
 
 
84
 
85
  if __name__ == "__main__":
86
  main()
 
3
  from langchain.vectorstores import FAISS
4
  from langchain.embeddings import OpenAIEmbeddings
5
  from langchain.document_loaders import PyPDFLoader
 
6
  from groq import Groq
7
 
8
+ # Set API Keys (Use your provided keys)
9
  GROQ_API_KEY = "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"
10
+ OPENAI_API_KEY = "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"
11
+
12
+ # Initialize API Keys
13
+ def initialize_keys():
14
+ """Set environment variables for API keys."""
15
+ os.environ["GROQ_API_KEY"] = GROQ_API_KEY
16
+ os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
17
 
18
  # Initialize Groq client
19
  def initialize_groq_client():
20
  """Initialize the Groq client with the API key."""
 
21
  return Groq(api_key=GROQ_API_KEY)
22
 
23
  # Generate response using Groq API
 
44
  # Create FAISS vector database
45
  def create_vector_db(documents):
46
  """Create a FAISS vector database from documents."""
47
+ embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY) # Use OpenAI API key
48
  vector_db = FAISS.from_documents(documents, embeddings)
49
  return vector_db
50
 
 
67
  documents = process_pdf("uploaded_act.pdf")
68
  st.success("PDF Loaded and Processed Successfully!")
69
 
70
+ # Initialize Groq client
71
+ initialize_keys()
72
+ groq_client = initialize_groq_client()
 
 
 
 
 
73
 
74
+ # Step 2: Build Vector DB and QA Chain
75
+ vector_db = create_vector_db(documents)
76
+ retriever, groq_client = build_rag_pipeline(vector_db, groq_client)
 
 
 
 
77
 
78
+ # Step 3: Ask Questions
79
+ query = st.text_input("Ask a question:")
80
+ if query:
81
+ with st.spinner("Fetching Answer..."):
82
+ # Use Groq API to generate answer
83
+ answer = generate_response(groq_client, query)
84
+
85
+ # Display Answer
86
+ st.write("### Answer:")
87
+ st.write(answer)
88
 
89
  if __name__ == "__main__":
90
  main()