File size: 5,050 Bytes
8eb9de0
 
eed634c
8eb9de0
eed634c
 
8eb9de0
 
5a1ad7f
8eb9de0
 
 
986c404
 
eed634c
 
8eb9de0
 
 
 
 
 
 
 
eed634c
 
 
 
 
8eb9de0
 
 
 
eed634c
 
 
 
8eb9de0
 
 
 
 
 
eed634c
 
 
8eb9de0
 
 
 
eed634c
8eb9de0
 
 
 
 
eed634c
8eb9de0
 
 
 
eed634c
8eb9de0
 
 
eed634c
 
8eb9de0
 
 
 
eed634c
 
 
8eb9de0
eed634c
 
8eb9de0
 
 
eed634c
 
 
8eb9de0
eed634c
 
 
8eb9de0
eed634c
 
 
 
8eb9de0
 
eed634c
8eb9de0
eed634c
 
 
8eb9de0
 
 
eed634c
 
8eb9de0
eed634c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8eb9de0
eed634c
8eb9de0
eed634c
 
 
8eb9de0
 
 
 
f88e930
8eb9de0
 
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
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)}")