Spaces:
Sleeping
Sleeping
Delete app.py
#4
by
Waseemhassan771
- opened
app.py
DELETED
|
@@ -1,65 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import fitz # PyMuPDF
|
| 4 |
-
import openai
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
-
from pinecone import Pinecone, ServerlessSpec
|
| 7 |
-
|
| 8 |
-
# Load the environment variables from the .env file
|
| 9 |
-
load_dotenv()
|
| 10 |
-
openai_api_key = os.getenv('OPENAI_API_KEY')
|
| 11 |
-
pinecone_api_key = os.getenv('PINECONE_API_KEY')
|
| 12 |
-
pinecone_environment = os.getenv('PINECONE_ENVIRONMENT')
|
| 13 |
-
|
| 14 |
-
# Initialize Pinecone
|
| 15 |
-
pc = Pinecone(api_key=pinecone_api_key)
|
| 16 |
-
|
| 17 |
-
# Streamlit app
|
| 18 |
-
st.title("Chat with Your Document")
|
| 19 |
-
st.write("Upload a PDF file to chat with its content using Pinecone and OpenAI.")
|
| 20 |
-
|
| 21 |
-
# File upload
|
| 22 |
-
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
|
| 23 |
-
|
| 24 |
-
if uploaded_file is not None:
|
| 25 |
-
# Load the PDF file
|
| 26 |
-
pdf_document = fitz.open(stream=uploaded_file.read(), filetype="pdf")
|
| 27 |
-
pdf_text = ""
|
| 28 |
-
for page_num in range(pdf_document.page_count):
|
| 29 |
-
page = pdf_document.load_page(page_num)
|
| 30 |
-
pdf_text += page.get_text()
|
| 31 |
-
|
| 32 |
-
# Initialize OpenAI embeddings
|
| 33 |
-
openai.api_key = openai_api_key
|
| 34 |
-
|
| 35 |
-
# Create a Pinecone vector store
|
| 36 |
-
index_name = "pdf-analysis"
|
| 37 |
-
if index_name not in pc.list_indexes().names():
|
| 38 |
-
pc.create_index(
|
| 39 |
-
name=index_name,
|
| 40 |
-
dimension=512,
|
| 41 |
-
metric='euclidean',
|
| 42 |
-
spec=ServerlessSpec(cloud='aws', region=pinecone_environment)
|
| 43 |
-
)
|
| 44 |
-
vector_store = pc.Index(index_name)
|
| 45 |
-
|
| 46 |
-
# Add the PDF text to the vector store
|
| 47 |
-
vector_store.upsert([(str(i), openai.Embedding.create(input=pdf_text)["data"][0]["embedding"]) for i in range(len(pdf_text))])
|
| 48 |
-
|
| 49 |
-
# Chat with the document
|
| 50 |
-
user_input = st.text_input("Ask a question about the document:")
|
| 51 |
-
if st.button("Ask"):
|
| 52 |
-
if user_input:
|
| 53 |
-
response = openai.Completion.create(
|
| 54 |
-
engine="davinci",
|
| 55 |
-
prompt=f"Analyze the following text and answer the question: {pdf_text}\n\nQuestion: {user_input}",
|
| 56 |
-
max_tokens=150
|
| 57 |
-
)
|
| 58 |
-
st.write(response.choices[0].text.strip())
|
| 59 |
-
else:
|
| 60 |
-
st.write("Please enter a question to ask.")
|
| 61 |
-
|
| 62 |
-
# Display the PDF text
|
| 63 |
-
st.write("Extracted Text from PDF:")
|
| 64 |
-
st.write(pdf_text)
|
| 65 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|