Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from langchain.document_loaders import PDFLoader
|
4 |
+
from langchain.embeddings import OpenAIEmbeddings
|
5 |
+
from langchain.vectorstores import Pinecone
|
6 |
+
from langchain.llms import OpenAI
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
import pinecone
|
9 |
+
|
10 |
+
# Load the environment variables from the .env file
|
11 |
+
load_dotenv()
|
12 |
+
openai_api_key = os.getenv('OPENAI_API_KEY')
|
13 |
+
pinecone_api_key = os.getenv('PINECONE_API_KEY')
|
14 |
+
pinecone_environment = os.getenv('PINECONE_ENVIRONMENT')
|
15 |
+
|
16 |
+
# Initialize Pinecone
|
17 |
+
pinecone.init(api_key=pinecone_api_key, environment=pinecone_environment)
|
18 |
+
|
19 |
+
# Streamlit app
|
20 |
+
st.title("Chat with Your Document")
|
21 |
+
st.write("Upload a PDF file to chat with its content using LangChain, Pinecone, and OpenAI.")
|
22 |
+
|
23 |
+
# File upload
|
24 |
+
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
|
25 |
+
|
26 |
+
if uploaded_file is not None:
|
27 |
+
# Load the PDF file
|
28 |
+
pdf_loader = PDFLoader(file_path=uploaded_file)
|
29 |
+
documents = pdf_loader.load()
|
30 |
+
|
31 |
+
# Extract text from the PDF
|
32 |
+
pdf_text = ""
|
33 |
+
for doc in documents:
|
34 |
+
pdf_text += doc.text
|
35 |
+
|
36 |
+
# Initialize OpenAI embeddings
|
37 |
+
embeddings = OpenAIEmbeddings(api_key=openai_api_key)
|
38 |
+
|
39 |
+
# Create a Pinecone vector store
|
40 |
+
index_name = "pdf-analysis"
|
41 |
+
if index_name not in pinecone.list_indexes():
|
42 |
+
pinecone.create_index(index_name, dimension=embeddings.dimension)
|
43 |
+
vector_store = Pinecone(index_name=index_name, embeddings=embeddings)
|
44 |
+
|
45 |
+
# Add the PDF text to the vector store
|
46 |
+
vector_store.add_texts([pdf_text])
|
47 |
+
|
48 |
+
# Initialize OpenAI LLM
|
49 |
+
llm = OpenAI(api_key=openai_api_key)
|
50 |
+
|
51 |
+
# Chat with the document
|
52 |
+
user_input = st.text_input("Ask a question about the document:")
|
53 |
+
if st.button("Ask"):
|
54 |
+
if user_input:
|
55 |
+
response = llm.generate(prompt=f"Analyze the following text and answer the question: {pdf_text}\n\nQuestion: {user_input}")
|
56 |
+
st.write(response)
|
57 |
+
else:
|
58 |
+
st.write("Please enter a question to ask.")
|
59 |
+
|
60 |
+
# Display the PDF text
|
61 |
+
st.write("Extracted Text from PDF:")
|
62 |
+
st.write(pdf_text)
|