Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,87 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import PyPDF2
|
| 3 |
-
from langchain.llms import HuggingFaceHub
|
| 4 |
-
import pptx
|
| 5 |
-
import os
|
| 6 |
-
from langchain.vectorstores.cassandra import Cassandra
|
| 7 |
-
from langchain.indexes.vectorstore import VectorStoreIndexWrapper
|
| 8 |
-
from langchain.embeddings import OpenAIEmbeddings
|
| 9 |
-
import cassio
|
| 10 |
-
from langchain.text_splitter import CharacterTextSplitter
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# Initialize
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
text
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import PyPDF2
|
| 3 |
+
from langchain.llms import HuggingFaceHub
|
| 4 |
+
import pptx
|
| 5 |
+
import os
|
| 6 |
+
from langchain.vectorstores.cassandra import Cassandra
|
| 7 |
+
from langchain.indexes.vectorstore import VectorStoreIndexWrapper
|
| 8 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 9 |
+
import cassio
|
| 10 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 11 |
+
from huggingface_hub import login
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Secure API keys (replace with environment variables in deployment)
|
| 20 |
+
ASTRA_DB_APPLICATION_TOKEN = os.getenv("ASTRA_DB_APPLICATION_TOKEN")
|
| 21 |
+
ASTRA_DB_ID = os.getenv("ASTRA_DB_ID")
|
| 22 |
+
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
| 23 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 24 |
+
login(token=HUGGINGFACE_API_KEY)
|
| 25 |
+
|
| 26 |
+
# Initialize Astra DB connection
|
| 27 |
+
cassio.init(token=ASTRA_DB_APPLICATION_TOKEN, database_id=ASTRA_DB_ID)
|
| 28 |
+
|
| 29 |
+
# Initialize LLM & Embeddings
|
| 30 |
+
hf_llm = HuggingFaceHub(repo_id="google/flan-t5-large", model_kwargs={"temperature": 0, "max_length": 64})
|
| 31 |
+
embedding =OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)
|
| 32 |
+
|
| 33 |
+
# Initialize vector store
|
| 34 |
+
astra_vector_store = Cassandra(embedding=embedding, table_name="qa_mini_demo")
|
| 35 |
+
|
| 36 |
+
def extract_text_from_pdf(uploaded_file):
|
| 37 |
+
"""Extract text from a PDF file."""
|
| 38 |
+
text = ""
|
| 39 |
+
pdf_reader = PyPDF2.PdfReader(uploaded_file)
|
| 40 |
+
for page in pdf_reader.pages:
|
| 41 |
+
page_text = page.extract_text()
|
| 42 |
+
if page_text: # Avoid NoneType error
|
| 43 |
+
text += page_text + "\n"
|
| 44 |
+
return text
|
| 45 |
+
|
| 46 |
+
def extract_text_from_ppt(uploaded_file):
|
| 47 |
+
"""Extract text from a PowerPoint file."""
|
| 48 |
+
text = ""
|
| 49 |
+
presentation = pptx.Presentation(uploaded_file)
|
| 50 |
+
for slide in presentation.slides:
|
| 51 |
+
for shape in slide.shapes:
|
| 52 |
+
if hasattr(shape, "text"):
|
| 53 |
+
text += shape.text + "\n"
|
| 54 |
+
return text
|
| 55 |
+
|
| 56 |
+
def main():
|
| 57 |
+
st.title("Chat with Documents")
|
| 58 |
+
|
| 59 |
+
uploaded_file = st.file_uploader("Upload a PDF or PPT file", type=["pdf", "pptx"])
|
| 60 |
+
extract_button = st.button("Extract Text")
|
| 61 |
+
|
| 62 |
+
extracted_text = ""
|
| 63 |
+
if extract_button and uploaded_file is not None:
|
| 64 |
+
if uploaded_file.name.endswith(".pdf"):
|
| 65 |
+
extracted_text = extract_text_from_pdf(uploaded_file)
|
| 66 |
+
elif uploaded_file.name.endswith(".pptx"):
|
| 67 |
+
extracted_text = extract_text_from_ppt(uploaded_file)
|
| 68 |
+
|
| 69 |
+
if extracted_text:
|
| 70 |
+
text_splitter = CharacterTextSplitter(separator="\n", chunk_size=800, chunk_overlap=200, length_function=len)
|
| 71 |
+
texts = text_splitter.split_text(extracted_text)
|
| 72 |
+
astra_vector_store.add_texts(texts)
|
| 73 |
+
|
| 74 |
+
# Ensure the vector store index is initialized properly
|
| 75 |
+
astra_vector_index = VectorStoreIndexWrapper(vectorstore=astra_vector_store)
|
| 76 |
+
|
| 77 |
+
query = st.text_input("Enter your query")
|
| 78 |
+
submit_query = st.button("Submit Query")
|
| 79 |
+
if submit_query:
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
value = astra_vector_index.query(query, llm=hf_llm)
|
| 83 |
+
|
| 84 |
+
st.write(f"Response: {value}")
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
main()
|