Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from groq import Groq
|
| 3 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
@@ -8,8 +105,7 @@ import streamlit as st
|
|
| 8 |
from tempfile import NamedTemporaryFile
|
| 9 |
|
| 10 |
# Initialize Groq client
|
| 11 |
-
client = Groq(api_key=os.
|
| 12 |
-
# client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 13 |
|
| 14 |
# Function to extract text from a PDF
|
| 15 |
def extract_text_from_pdf(pdf_file_path):
|
|
@@ -60,11 +156,12 @@ if uploaded_file:
|
|
| 60 |
pdf_path = temp_file.name
|
| 61 |
|
| 62 |
# Extract text, chunk it, and create embeddings
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
-
#
|
| 68 |
if "chat_history" not in st.session_state:
|
| 69 |
st.session_state.chat_history = []
|
| 70 |
|
|
@@ -75,21 +172,16 @@ if uploaded_file:
|
|
| 75 |
st.write("---")
|
| 76 |
|
| 77 |
# Add new query input dynamically
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
query_key = f"query_{st.session_state.query_count}"
|
| 82 |
-
user_query = st.text_input(f"Enter Query {st.session_state.query_count}:", key=query_key)
|
| 83 |
|
| 84 |
if user_query:
|
| 85 |
# Generate response
|
| 86 |
-
response = query_vector_db(user_query, vector_db)
|
| 87 |
|
| 88 |
# Append query and response to the chat history
|
| 89 |
st.session_state.chat_history.append({"query": user_query, "response": response})
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
st.
|
| 93 |
|
| 94 |
-
# Rerun to show the updated UI
|
| 95 |
-
st.experimental_rerun()
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# from groq import Groq
|
| 3 |
+
# from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 4 |
+
# from langchain_community.vectorstores import FAISS
|
| 5 |
+
# from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
# from PyPDF2 import PdfReader
|
| 7 |
+
# import streamlit as st
|
| 8 |
+
# from tempfile import NamedTemporaryFile
|
| 9 |
+
|
| 10 |
+
# # Initialize Groq client
|
| 11 |
+
# client = Groq(api_key=os.getenv("Groq_api_key"))
|
| 12 |
+
# # client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 13 |
+
|
| 14 |
+
# # Function to extract text from a PDF
|
| 15 |
+
# def extract_text_from_pdf(pdf_file_path):
|
| 16 |
+
# pdf_reader = PdfReader(pdf_file_path)
|
| 17 |
+
# text = ""
|
| 18 |
+
# for page in pdf_reader.pages:
|
| 19 |
+
# text += page.extract_text()
|
| 20 |
+
# return text
|
| 21 |
+
|
| 22 |
+
# # Function to split text into chunks
|
| 23 |
+
# def chunk_text(text, chunk_size=500, chunk_overlap=50):
|
| 24 |
+
# text_splitter = RecursiveCharacterTextSplitter(
|
| 25 |
+
# chunk_size=chunk_size, chunk_overlap=chunk_overlap
|
| 26 |
+
# )
|
| 27 |
+
# return text_splitter.split_text(text)
|
| 28 |
+
|
| 29 |
+
# # Function to create embeddings and store them in FAISS
|
| 30 |
+
# def create_embeddings_and_store(chunks):
|
| 31 |
+
# embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 32 |
+
# vector_db = FAISS.from_texts(chunks, embedding=embeddings)
|
| 33 |
+
# return vector_db
|
| 34 |
+
|
| 35 |
+
# # Function to query the vector database and interact with Groq
|
| 36 |
+
# def query_vector_db(query, vector_db):
|
| 37 |
+
# # Retrieve relevant documents
|
| 38 |
+
# docs = vector_db.similarity_search(query, k=3)
|
| 39 |
+
# context = "\n".join([doc.page_content for doc in docs])
|
| 40 |
+
|
| 41 |
+
# # Interact with Groq API
|
| 42 |
+
# chat_completion = client.chat.completions.create(
|
| 43 |
+
# messages=[
|
| 44 |
+
# {"role": "system", "content": f"Use the following context:\n{context}"},
|
| 45 |
+
# {"role": "user", "content": query},
|
| 46 |
+
# ],
|
| 47 |
+
# model="llama3-8b-8192",
|
| 48 |
+
# )
|
| 49 |
+
# return chat_completion.choices[0].message.content
|
| 50 |
+
|
| 51 |
+
# # Streamlit app
|
| 52 |
+
# st.title("Interactive PDF Reader and Chat")
|
| 53 |
+
|
| 54 |
+
# # Upload PDF
|
| 55 |
+
# uploaded_file = st.file_uploader("Upload a PDF document", type=["pdf"])
|
| 56 |
+
|
| 57 |
+
# if uploaded_file:
|
| 58 |
+
# with NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
| 59 |
+
# temp_file.write(uploaded_file.read())
|
| 60 |
+
# pdf_path = temp_file.name
|
| 61 |
+
|
| 62 |
+
# # Extract text, chunk it, and create embeddings
|
| 63 |
+
# text = extract_text_from_pdf(pdf_path)
|
| 64 |
+
# chunks = chunk_text(text)
|
| 65 |
+
# vector_db = create_embeddings_and_store(chunks)
|
| 66 |
+
|
| 67 |
+
# # State management for the chat
|
| 68 |
+
# if "chat_history" not in st.session_state:
|
| 69 |
+
# st.session_state.chat_history = []
|
| 70 |
+
|
| 71 |
+
# # Display chat history
|
| 72 |
+
# for i, chat in enumerate(st.session_state.chat_history):
|
| 73 |
+
# st.write(f"**Query {i+1}:** {chat['query']}")
|
| 74 |
+
# st.write(f"**Response:** {chat['response']}")
|
| 75 |
+
# st.write("---")
|
| 76 |
+
|
| 77 |
+
# # Add new query input dynamically
|
| 78 |
+
# if "query_count" not in st.session_state:
|
| 79 |
+
# st.session_state.query_count = 1
|
| 80 |
+
|
| 81 |
+
# query_key = f"query_{st.session_state.query_count}"
|
| 82 |
+
# user_query = st.text_input(f"Enter Query {st.session_state.query_count}:", key=query_key)
|
| 83 |
+
|
| 84 |
+
# if user_query:
|
| 85 |
+
# # Generate response
|
| 86 |
+
# response = query_vector_db(user_query, vector_db)
|
| 87 |
+
|
| 88 |
+
# # Append query and response to the chat history
|
| 89 |
+
# st.session_state.chat_history.append({"query": user_query, "response": response})
|
| 90 |
+
|
| 91 |
+
# # Increment query count for the next input box
|
| 92 |
+
# st.session_state.query_count += 1
|
| 93 |
+
|
| 94 |
+
# # Rerun to show the updated UI
|
| 95 |
+
# st.experimental_rerun()
|
| 96 |
+
|
| 97 |
+
|
| 98 |
import os
|
| 99 |
from groq import Groq
|
| 100 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
|
|
| 105 |
from tempfile import NamedTemporaryFile
|
| 106 |
|
| 107 |
# Initialize Groq client
|
| 108 |
+
client = Groq(api_key=os.environ.get("Goq_api_key"))
|
|
|
|
| 109 |
|
| 110 |
# Function to extract text from a PDF
|
| 111 |
def extract_text_from_pdf(pdf_file_path):
|
|
|
|
| 156 |
pdf_path = temp_file.name
|
| 157 |
|
| 158 |
# Extract text, chunk it, and create embeddings
|
| 159 |
+
if "vector_db" not in st.session_state:
|
| 160 |
+
text = extract_text_from_pdf(pdf_path)
|
| 161 |
+
chunks = chunk_text(text)
|
| 162 |
+
st.session_state.vector_db = create_embeddings_and_store(chunks)
|
| 163 |
|
| 164 |
+
# Initialize chat history if not already done
|
| 165 |
if "chat_history" not in st.session_state:
|
| 166 |
st.session_state.chat_history = []
|
| 167 |
|
|
|
|
| 172 |
st.write("---")
|
| 173 |
|
| 174 |
# Add new query input dynamically
|
| 175 |
+
query_key = f"query_{len(st.session_state.chat_history) + 1}"
|
| 176 |
+
user_query = st.text_input("Enter your query:", key=query_key)
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
if user_query:
|
| 179 |
# Generate response
|
| 180 |
+
response = query_vector_db(user_query, st.session_state.vector_db)
|
| 181 |
|
| 182 |
# Append query and response to the chat history
|
| 183 |
st.session_state.chat_history.append({"query": user_query, "response": response})
|
| 184 |
|
| 185 |
+
# Refresh the app without needing manual rerun
|
| 186 |
+
st.experimental_set_query_params(rerun="true")
|
| 187 |
|
|
|
|
|
|