Spaces:
Runtime error
Runtime error
File size: 6,807 Bytes
ccfb409 6ae72bf f4e447d 6ae72bf e63918a 5d3c81a 6ae72bf 5d3c81a 6ae72bf 5d3c81a 6ae72bf 44eb0ab 6ae72bf 44eb0ab 6ae72bf 21dcb99 05d137c 9504048 17e0bb7 fc5882b a060be9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
import streamlit as st
import langchain
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain import OpenAI, VectorDBQA
from langchain.chains import RetrievalQAWithSourcesChain
import PyPDF2
api_key = os.environ["OPENAI_API_KEY"]
#This function will go through pdf and extract and return list of page texts.
def read_and_textify(files):
text_list = []
sources_list = []
for file in files:
pdfReader = PyPDF2.PdfReader(file)
#print("Page Number:", len(pdfReader.pages))
for i in range(len(pdfReader.pages)):
pageObj = pdfReader.pages[i]
text = pageObj.extract_text()
pageObj.clear()
text_list.append(text)
sources_list.append(file.name + "_page_"+str(i))
return [text_list,sources_list]
st.set_page_config(layout="centered", page_title="Multidoc_QnA")
st.header("Multidoc_QnA")
st.write("---")
#file uploader
uploaded_files = st.file_uploader("Upload documents",accept_multiple_files=True, type=["txt","pdf"])
st.write("---")
if uploaded_files is None:
st.info(f"""Upload files to analyse""")
elif uploaded_files:
st.write(str(len(uploaded_files)) + " document(s) loaded..")
textify_output = read_and_textify(uploaded_files)
documents = textify_output[0]
sources = textify_output[1]
#extract embeddings
embeddings = OpenAIEmbeddings(openai_api_key = api_key)
#vstore with metadata. Here we will store page numbers.
vStore = Chroma.from_texts(documents, embeddings, metadatas=[{"source": s} for s in sources])
#deciding model
model_name = "gpt-3.5-turbo"
# model_name = "gpt-4"
retriever = vStore.as_retriever()
retriever.search_kwargs = {'k':2}
#initiate model
llm = OpenAI(model_name=model_name, openai_api_key = api_key, streaming=True)
model = RetrievalQAWithSourcesChain.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
st.header("Ask your data")
user_q = st.text_area("Enter your questions here")
if st.button("Get Response"):
try:
with st.spinner("Model is working on it..."):
result = model({"question":user_q}, return_only_outputs=True)
st.subheader('Your response:')
st.write(result['answer'])
st.subheader('Source pages:')
st.write(result['sources'])
except Exception as e:
st.error(f"An error occurred: {e}")
st.error('Oops, the GPT response resulted in an error :( Please try again with a different question.')
# import gradio as gr
# import streamlit as st
# from langchain.embeddings.openai import OpenAIEmbeddings
# from langchain.text_splitter import CharacterTextSplitter
# from langchain.vectorstores import Chroma
# from langchain.chains import ConversationalRetrievalChain
# from langchain.chat_models import ChatOpenAI
# from langchain.document_loaders import PyPDFLoader
# import os
# import fitz
# from PIL import Image
# # Global variables
# COUNT, N = 0, 0
# chat_history = []
# chain = None # Initialize chain as None
# # Function to set the OpenAI API key
# api_key = os.environ['OPENAI_API_KEY']
# st.write(api_key)
# # Function to enable the API key input box
# def enable_api_box():
# return enable_box
# # Function to add text to the chat history
# def add_text(history, text):
# if not text:
# raise gr.Error('Enter text')
# history = history + [(text, '')]
# return history
# # Function to process the PDF file and create a conversation chain
# def process_file(file):
# global chain
# if 'OPENAI_API_KEY' not in os.environ:
# raise gr.Error('Upload your OpenAI API key')
# # Replace with your actual PDF processing logic
# loader = PyPDFLoader(file.name)
# documents = loader.load()
# embeddings = OpenAIEmbeddings()
# pdfsearch = Chroma.from_documents(documents, embeddings)
# chain = ConversationalRetrievalChain.from_llm(ChatOpenAI(temperature=0.3),
# retriever=pdfsearch.as_retriever(search_kwargs={"k": 1}),
# return_source_documents=True)
# return chain
# # Function to generate a response based on the chat history and query
# def generate_response(history, query, pdf_upload):
# global COUNT, N, chat_history, chain
# if not pdf_upload:
# raise gr.Error(message='Upload a PDF')
# if COUNT == 0:
# chain = process_file(pdf_upload)
# COUNT += 1
# # Replace with your LangChain logic to generate a response
# result = chain({"question": query, 'chat_history': chat_history}, return_only_outputs=True)
# chat_history += [(query, result["answer"])]
# N = list(result['source_documents'][0])[1][1]['page'] # Adjust as needed
# for char in result['answer']:
# history[-1][-1] += char
# return history, ''
# # Function to render a specific page of a PDF file as an image
# def render_file(file):
# global N
# doc = fitz.open(file.name)
# page = doc[N]
# pix = page.get_pixmap(matrix=fitz.Matrix(300/72, 300/72))
# image = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
# return image
# # Function to render initial content from the PDF
# def render_first(pdf_file):
# # Replace with logic to process the PDF and generate an initial image
# image = Image.new('RGB', (600, 400), color = 'white') # Placeholder
# return image
# # Streamlit & Gradio Interface
# st.title("PDF-Powered Chatbot")
# with st.container():
# gr.Markdown("""
# <style>
# .image-container { height: 680px; }
# </style>
# """)
# with gr.Blocks() as demo:
# pdf_upload1 = gr.UploadButton("π Upload PDF 1", file_types=[".pdf"]) # Define pdf_upload1
# # ... (rest of your interface creation)
# txt = gr.Textbox(label="Enter your query", placeholder="Ask a question...")
# submit_btn = gr.Button('Submit')
# @submit_btn.click()
# def on_submit():
# add_text(chatbot, txt)
# generate_response(chatbot, txt, pdf_upload1) # Use pdf_upload1 here
# render_file(pdf_upload1) # Use pdf_upload1 here
# if __name__ == "__main__":
# gr.Interface(
# fn=generate_response,
# inputs=[
# "file", # Define pdf_upload1
# "text", # Define chatbot output
# "text" # Define txt
# ],
# outputs=[
# "image", # Define show_img
# "text", # Define chatbot output
# "text" # Define txt
# ],
# title="PDF-Powered Chatbot"
# ).launch()
|