Spaces:
Runtime error
Runtime error
| import os | |
| import gradio as gr | |
| from langchain_community.document_loaders import PyPDFLoader | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| from langchain_community.embeddings import OpenAIEmbeddings | |
| from langchain_community.vectorstores import FAISS | |
| from langchain.chains import ConversationalRetrievalChain | |
| from langchain.memory import ConversationBufferMemory | |
| from langchain_openai.chat_models import ChatOpenAI | |
| class AuditCopilot: | |
| def __init__(self): | |
| self.openai_api_key = os.getenv('OPENAI_API_KEY') | |
| if not self.openai_api_key: | |
| raise ValueError("OPENAI_API_KEY environment variable is not set") | |
| self.vector_store = None | |
| self.chain = None | |
| self.chat_history = [] | |
| self.initialize_system() | |
| def initialize_system(self): | |
| """Initialize the system with the pre-loaded PDF""" | |
| try: | |
| # Make sure to include the .pdf extension in the filename | |
| pdf_path = "IAASB-Drafting-Principles-Guidelines.pdf" | |
| if not os.path.exists(pdf_path): | |
| raise FileNotFoundError(f"PDF file not found at path: {pdf_path}") | |
| # Load and split document | |
| loader = PyPDFLoader(pdf_path) | |
| documents = loader.load() | |
| if not documents: | |
| raise ValueError("No content loaded from PDF") | |
| text_splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=1000, | |
| chunk_overlap=200 | |
| ) | |
| splits = text_splitter.split_documents(documents) | |
| if not splits: | |
| raise ValueError("No text splits created from documents") | |
| # Create vector store | |
| embeddings = OpenAIEmbeddings(openai_api_key=self.openai_api_key) | |
| self.vector_store = FAISS.from_documents(splits, embeddings) | |
| # Initialize conversation chain | |
| llm = ChatOpenAI( | |
| model_name="gpt-3.5-turbo", | |
| temperature=0, | |
| openai_api_key=self.openai_api_key | |
| ) | |
| memory = ConversationBufferMemory( | |
| memory_key="chat_history", | |
| return_messages=True | |
| ) | |
| self.chain = ConversationalRetrievalChain.from_llm( | |
| llm=llm, | |
| retriever=self.vector_store.as_retriever(), | |
| memory=memory, | |
| verbose=True # Added for debugging | |
| ) | |
| print("System initialized successfully!") | |
| except FileNotFoundError as e: | |
| print(f"PDF File Error: {str(e)}") | |
| raise | |
| except Exception as e: | |
| print(f"Initialization Error: {str(e)}") | |
| raise | |
| def get_response(self, question): | |
| """Get response from the chain""" | |
| if not self.chain: | |
| return "Error: System not properly initialized. Please check the PDF file and try again." | |
| try: | |
| if not question or not isinstance(question, str): | |
| return "Please provide a valid question." | |
| response = self.chain({"question": question}) | |
| if not response or 'answer' not in response: | |
| return "Error: Unable to generate a response. Please try again." | |
| self.chat_history.append((question, response['answer'])) | |
| return response['answer'] | |
| except Exception as e: | |
| error_msg = f"Error generating response: {str(e)}" | |
| print(error_msg) # For logging | |
| return error_msg | |
| def create_gradio_interface(): | |
| """Create Gradio interface""" | |
| try: | |
| copilot = AuditCopilot() | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Audit Copilot") | |
| gr.Markdown("Ask questions about the IAASB Drafting Principles Guidelines!") | |
| # Chat section | |
| chatbot = gr.Chatbot(label="Conversation") | |
| msg = gr.Textbox(label="Ask a question", placeholder="Type your question here...") | |
| clear = gr.Button("Clear Chat") | |
| def respond(message, chat_history): | |
| if not message.strip(): | |
| return "", chat_history | |
| bot_message = copilot.get_response(message) | |
| chat_history.append((message, bot_message)) | |
| return "", chat_history | |
| msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
| clear.click(lambda: None, None, chatbot, queue=False) | |
| return demo | |
| except Exception as e: | |
| print(f"Error creating interface: {str(e)}") | |
| raise | |
| if __name__ == "__main__": | |
| try: | |
| demo = create_gradio_interface() | |
| demo.launch(share=True) | |
| except Exception as e: | |
| print(f"Error launching application: {str(e)}") |