Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,132 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
from langchain.chains import ConversationalRetrievalChain
|
| 8 |
from langchain.memory import ConversationBufferMemory
|
| 9 |
-
from
|
| 10 |
|
| 11 |
class AuditCopilot:
|
| 12 |
def __init__(self):
|
| 13 |
-
# Hardcoded OpenAI API key
|
| 14 |
self.openai_api_key = os.getenv('OPENAI_API_KEY')
|
| 15 |
if not self.openai_api_key:
|
| 16 |
-
raise ValueError("OPENAI_API_KEY environment variable is not set")
|
|
|
|
| 17 |
self.vector_store = None
|
| 18 |
self.chain = None
|
| 19 |
self.chat_history = []
|
| 20 |
-
# Initialize the system with the PDF
|
| 21 |
self.initialize_system()
|
| 22 |
|
| 23 |
def initialize_system(self):
|
| 24 |
"""Initialize the system with the pre-loaded PDF"""
|
| 25 |
try:
|
| 26 |
-
#
|
| 27 |
-
pdf_path = "IAASB-Drafting-Principles-Guidelines"
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Load and split document
|
| 30 |
loader = PyPDFLoader(pdf_path)
|
| 31 |
documents = loader.load()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 33 |
chunk_size=1000,
|
| 34 |
chunk_overlap=200
|
| 35 |
)
|
| 36 |
splits = text_splitter.split_documents(documents)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
# Create vector store
|
| 39 |
embeddings = OpenAIEmbeddings(openai_api_key=self.openai_api_key)
|
| 40 |
self.vector_store = FAISS.from_documents(splits, embeddings)
|
| 41 |
|
| 42 |
-
# Initialize conversation chain
|
| 43 |
llm = ChatOpenAI(
|
| 44 |
model_name="gpt-3.5-turbo",
|
| 45 |
temperature=0,
|
| 46 |
openai_api_key=self.openai_api_key
|
| 47 |
)
|
|
|
|
| 48 |
memory = ConversationBufferMemory(
|
| 49 |
memory_key="chat_history",
|
| 50 |
return_messages=True
|
| 51 |
)
|
|
|
|
| 52 |
self.chain = ConversationalRetrievalChain.from_llm(
|
| 53 |
llm=llm,
|
| 54 |
retriever=self.vector_store.as_retriever(),
|
| 55 |
-
memory=memory
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
print("System initialized successfully!")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
-
print(f"Error
|
|
|
|
| 62 |
|
| 63 |
def get_response(self, question):
|
| 64 |
"""Get response from the chain"""
|
|
|
|
|
|
|
|
|
|
| 65 |
try:
|
|
|
|
|
|
|
|
|
|
| 66 |
response = self.chain({"question": question})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
self.chat_history.append((question, response['answer']))
|
| 68 |
return response['answer']
|
|
|
|
| 69 |
except Exception as e:
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
def create_gradio_interface():
|
| 73 |
"""Create Gradio interface"""
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
with gr.Blocks() as demo:
|
| 77 |
-
gr.Markdown("# Audit Copilot")
|
| 78 |
-
gr.Markdown("Ask questions about the audit guidelines!")
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
bot_message = copilot.get_response(message)
|
| 87 |
-
chat_history.append((message, bot_message))
|
| 88 |
-
return "", chat_history
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
return demo
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
try:
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_community.embeddings import OpenAIEmbeddings
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
from langchain.chains import ConversationalRetrievalChain
|
| 8 |
from langchain.memory import ConversationBufferMemory
|
| 9 |
+
from langchain_openai import ChatOpenAI
|
| 10 |
|
| 11 |
class AuditCopilot:
|
| 12 |
def __init__(self):
|
|
|
|
| 13 |
self.openai_api_key = os.getenv('OPENAI_API_KEY')
|
| 14 |
if not self.openai_api_key:
|
| 15 |
+
raise ValueError("OPENAI_API_KEY environment variable is not set")
|
| 16 |
+
|
| 17 |
self.vector_store = None
|
| 18 |
self.chain = None
|
| 19 |
self.chat_history = []
|
|
|
|
| 20 |
self.initialize_system()
|
| 21 |
|
| 22 |
def initialize_system(self):
|
| 23 |
"""Initialize the system with the pre-loaded PDF"""
|
| 24 |
try:
|
| 25 |
+
# Make sure to include the .pdf extension in the filename
|
| 26 |
+
pdf_path = "IAASB-Drafting-Principles-Guidelines.pdf"
|
| 27 |
+
|
| 28 |
+
if not os.path.exists(pdf_path):
|
| 29 |
+
raise FileNotFoundError(f"PDF file not found at path: {pdf_path}")
|
| 30 |
|
| 31 |
# Load and split document
|
| 32 |
loader = PyPDFLoader(pdf_path)
|
| 33 |
documents = loader.load()
|
| 34 |
+
|
| 35 |
+
if not documents:
|
| 36 |
+
raise ValueError("No content loaded from PDF")
|
| 37 |
+
|
| 38 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 39 |
chunk_size=1000,
|
| 40 |
chunk_overlap=200
|
| 41 |
)
|
| 42 |
splits = text_splitter.split_documents(documents)
|
| 43 |
|
| 44 |
+
if not splits:
|
| 45 |
+
raise ValueError("No text splits created from documents")
|
| 46 |
+
|
| 47 |
# Create vector store
|
| 48 |
embeddings = OpenAIEmbeddings(openai_api_key=self.openai_api_key)
|
| 49 |
self.vector_store = FAISS.from_documents(splits, embeddings)
|
| 50 |
|
| 51 |
+
# Initialize conversation chain
|
| 52 |
llm = ChatOpenAI(
|
| 53 |
model_name="gpt-3.5-turbo",
|
| 54 |
temperature=0,
|
| 55 |
openai_api_key=self.openai_api_key
|
| 56 |
)
|
| 57 |
+
|
| 58 |
memory = ConversationBufferMemory(
|
| 59 |
memory_key="chat_history",
|
| 60 |
return_messages=True
|
| 61 |
)
|
| 62 |
+
|
| 63 |
self.chain = ConversationalRetrievalChain.from_llm(
|
| 64 |
llm=llm,
|
| 65 |
retriever=self.vector_store.as_retriever(),
|
| 66 |
+
memory=memory,
|
| 67 |
+
verbose=True # Added for debugging
|
| 68 |
)
|
| 69 |
|
| 70 |
print("System initialized successfully!")
|
| 71 |
|
| 72 |
+
except FileNotFoundError as e:
|
| 73 |
+
print(f"PDF File Error: {str(e)}")
|
| 74 |
+
raise
|
| 75 |
except Exception as e:
|
| 76 |
+
print(f"Initialization Error: {str(e)}")
|
| 77 |
+
raise
|
| 78 |
|
| 79 |
def get_response(self, question):
|
| 80 |
"""Get response from the chain"""
|
| 81 |
+
if not self.chain:
|
| 82 |
+
return "Error: System not properly initialized. Please check the PDF file and try again."
|
| 83 |
+
|
| 84 |
try:
|
| 85 |
+
if not question or not isinstance(question, str):
|
| 86 |
+
return "Please provide a valid question."
|
| 87 |
+
|
| 88 |
response = self.chain({"question": question})
|
| 89 |
+
|
| 90 |
+
if not response or 'answer' not in response:
|
| 91 |
+
return "Error: Unable to generate a response. Please try again."
|
| 92 |
+
|
| 93 |
self.chat_history.append((question, response['answer']))
|
| 94 |
return response['answer']
|
| 95 |
+
|
| 96 |
except Exception as e:
|
| 97 |
+
error_msg = f"Error generating response: {str(e)}"
|
| 98 |
+
print(error_msg) # For logging
|
| 99 |
+
return error_msg
|
| 100 |
|
| 101 |
def create_gradio_interface():
|
| 102 |
"""Create Gradio interface"""
|
| 103 |
+
try:
|
| 104 |
+
copilot = AuditCopilot()
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
with gr.Blocks() as demo:
|
| 107 |
+
gr.Markdown("# Audit Copilot")
|
| 108 |
+
gr.Markdown("Ask questions about the IAASB Drafting Principles Guidelines!")
|
| 109 |
+
|
| 110 |
+
# Chat section
|
| 111 |
+
chatbot = gr.Chatbot(label="Conversation")
|
| 112 |
+
msg = gr.Textbox(label="Ask a question", placeholder="Type your question here...")
|
| 113 |
+
clear = gr.Button("Clear Chat")
|
| 114 |
+
|
| 115 |
+
def respond(message, chat_history):
|
| 116 |
+
if not message.strip():
|
| 117 |
+
return "", chat_history
|
| 118 |
+
bot_message = copilot.get_response(message)
|
| 119 |
+
chat_history.append((message, bot_message))
|
| 120 |
+
return "", chat_history
|
| 121 |
+
|
| 122 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 123 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 124 |
|
| 125 |
+
return demo
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
except Exception as e:
|
| 128 |
+
print(f"Error creating interface: {str(e)}")
|
| 129 |
+
raise
|
|
|
|
|
|
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
try:
|