Spaces:
Sleeping
Sleeping
Engr-Saeed
commited on
Commit
β’
dc28374
1
Parent(s):
c019b37
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
from PyPDF2 import PdfReader
|
3 |
import docx2txt
|
@@ -5,20 +6,19 @@ import json
|
|
5 |
import pandas as pd
|
6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
import os
|
8 |
-
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
9 |
-
import google.generativeai as genai
|
10 |
from langchain.vectorstores import FAISS
|
11 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
12 |
from langchain.chains.question_answering import load_qa_chain
|
13 |
from langchain.prompts import PromptTemplate
|
|
|
|
|
14 |
from dotenv import load_dotenv
|
15 |
|
16 |
# Step 2: Load environment variable
|
17 |
load_dotenv()
|
18 |
-
|
19 |
|
20 |
-
# Step 3:
|
21 |
-
|
22 |
|
23 |
# Step 4: Function to read files and extract text
|
24 |
def extract_text(file):
|
@@ -59,7 +59,7 @@ def get_text_chunks(text):
|
|
59 |
|
60 |
# Step 6: Function for converting chunks into embeddings and saving the FAISS index
|
61 |
def get_vector_store(text_chunks):
|
62 |
-
embeddings =
|
63 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
64 |
|
65 |
# Ensure the directory exists
|
@@ -69,7 +69,7 @@ def get_vector_store(text_chunks):
|
|
69 |
vector_store.save_local("faiss_index")
|
70 |
print("FAISS index saved successfully.")
|
71 |
|
72 |
-
# Step 7: Function to implement
|
73 |
def get_conversational_chain():
|
74 |
prompt_template = """
|
75 |
Answer the question as detailed as possible from the provided context. If the answer is not in
|
@@ -79,14 +79,14 @@ def get_conversational_chain():
|
|
79 |
|
80 |
Answer:
|
81 |
"""
|
82 |
-
model =
|
83 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
84 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
85 |
return chain
|
86 |
|
87 |
# Step 8: Function to take inputs from user and generate response
|
88 |
def user_input(user_question):
|
89 |
-
embeddings =
|
90 |
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
91 |
docs = new_db.similarity_search(user_question)
|
92 |
chain = get_conversational_chain()
|
@@ -96,7 +96,7 @@ def user_input(user_question):
|
|
96 |
# Step 9: Streamlit App
|
97 |
def main():
|
98 |
st.set_page_config(page_title="RAG Chatbot")
|
99 |
-
st.header("Chat with Multiple Files using RAG and
|
100 |
|
101 |
user_question = st.text_input("Ask a Question")
|
102 |
|
|
|
1 |
+
# Step 1: Import required modules
|
2 |
import streamlit as st
|
3 |
from PyPDF2 import PdfReader
|
4 |
import docx2txt
|
|
|
6 |
import pandas as pd
|
7 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
8 |
import os
|
|
|
|
|
9 |
from langchain.vectorstores import FAISS
|
|
|
10 |
from langchain.chains.question_answering import load_qa_chain
|
11 |
from langchain.prompts import PromptTemplate
|
12 |
+
import whisper
|
13 |
+
from groq import GroqAPI
|
14 |
from dotenv import load_dotenv
|
15 |
|
16 |
# Step 2: Load environment variable
|
17 |
load_dotenv()
|
18 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
19 |
|
20 |
+
# Step 3: Initialize Groq API
|
21 |
+
groq = GroqAPI(api_key=groq_api_key)
|
22 |
|
23 |
# Step 4: Function to read files and extract text
|
24 |
def extract_text(file):
|
|
|
59 |
|
60 |
# Step 6: Function for converting chunks into embeddings and saving the FAISS index
|
61 |
def get_vector_store(text_chunks):
|
62 |
+
embeddings = groq.get_embeddings(text_chunks)
|
63 |
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
64 |
|
65 |
# Ensure the directory exists
|
|
|
69 |
vector_store.save_local("faiss_index")
|
70 |
print("FAISS index saved successfully.")
|
71 |
|
72 |
+
# Step 7: Function to implement the Groq Model
|
73 |
def get_conversational_chain():
|
74 |
prompt_template = """
|
75 |
Answer the question as detailed as possible from the provided context. If the answer is not in
|
|
|
79 |
|
80 |
Answer:
|
81 |
"""
|
82 |
+
model = groq.get_chat_model("llama3-8b-8192") # Replace with your Groq model ID
|
83 |
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
84 |
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
85 |
return chain
|
86 |
|
87 |
# Step 8: Function to take inputs from user and generate response
|
88 |
def user_input(user_question):
|
89 |
+
embeddings = groq.get_embeddings([user_question])
|
90 |
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
91 |
docs = new_db.similarity_search(user_question)
|
92 |
chain = get_conversational_chain()
|
|
|
96 |
# Step 9: Streamlit App
|
97 |
def main():
|
98 |
st.set_page_config(page_title="RAG Chatbot")
|
99 |
+
st.header("Chat with Multiple Files using RAG and Groq π")
|
100 |
|
101 |
user_question = st.text_input("Ask a Question")
|
102 |
|